Java MagicEightBall阵列

Java MagicEightBall阵列,java,Java,我有一个关于将字符串分配到数组编号的问题 我已经声明了字符串数组,例如 String[] answer = {"yes", "no", "maybe"}; 如何在不使用这种方法的情况下将每个字符串设置为整数数组。例如: String answer1 = yes; int answerList[0] = answer1 我还说了一句错误的话 C:\temp folder\MagicEightBall.java:50:错误:不兼容的类型:int[]无法转换为intSystem.out.print

我有一个关于将字符串分配到数组编号的问题

我已经声明了字符串数组,例如

String[] answer = {"yes", "no", "maybe"};
如何在不使用这种方法的情况下将每个字符串设置为整数数组。例如:

String answer1 = yes;
int answerList[0] = answer1
我还说了一句错误的话

C:\temp folder\MagicEightBall.java:50:错误:不兼容的类型:int[]无法转换为intSystem.out.println(答案[Random.nextInt(answerList)])

这是我的密码:

import java.util.*;

public class MagicEightBall
{
    public static void main( String[] args)
    {//start main
        final int Array_Length = 20;

        String[] answer = {"It is certain", "Yes - definitely", "Most likely", "Yes", "Better not tell you now", "Con't count on it", 
                        "Outlook not so good", "It is decidedly so", "You may rely on it", "Outlook good", "Reply hazy, try again", 
                        "Cannot predict now", "My reply is no", "Very doubtful", "Without a doubt", "As I see it, yes", 
                        "Signs to point yes", "Ask again later", "Concentrate and ask again", "My source say no"};

        int answerList[] = new int[20];


        int userInput;
        String questions;

        Scanner input = new Scanner (System.in);
        Random generator = new Random();

        do
        {
            System.out.println("Please enter 1 to keep playing or 2 to exit:");
            userInput = input.nextInt();

        } while (userInput < 1 & userInput != 2 || userInput >= 2);

        while (userInput != 2);
        {
            switch (userInput)
            {
                case 1:
                    System.out.println("Please enter your questions below:");
                    questions = input.nextLine();

                    System.out.println("Hmmmm....");
                    System.out.println(answer[Random.nextInt(answerList)]); 
                    break;

                case 2:
                    break;

            }
        }

        do
        {
            System.out.println("Please enter 1 to keep playing or 2 to exit:");
            userInput = input.nextInt();

        } while (userInput < 1 & userInput != 2 || userInput >= 2);

        System.out.println("Written by blabla");    
    }
}
import java.util.*;
公共级魔法球
{
公共静态void main(字符串[]args)
{//启动主
最终整数数组长度=20;
String[]answer={“这是肯定的”、“是-肯定的”、“最有可能”、“是的”、“最好现在不要告诉你”、“不要指望它”,
“前景不太好”、“肯定是这样”、“你可以信赖它”、“前景很好”、“回复模糊,再试一次”,
“现在无法预测”,“我的回答是否定的”,“非常可疑”,“毫无疑问”,“在我看来,是的”,
“表示同意”、“稍后再问”、“集中精力再问”、“我的消息来源说不”};
int answerList[]=新int[20];
int用户输入;
串问题;
扫描仪输入=新扫描仪(System.in);
随机生成器=新随机();
做
{
System.out.println(“请输入1继续播放,或输入2退出:”;
userInput=input.nextInt();
}而(userInput<1&userInput!=2 | | userInput>=2);
while(userInput!=2);
{
开关(用户输入)
{
案例1:
System.out.println(“请在下面输入您的问题:”);
问题=输入。nextLine();
System.out.println(“Hmmmm…”);
System.out.println(答案[Random.nextInt(答案列表)]);
打破
案例2:
打破
}
}
做
{
System.out.println(“请输入1继续播放,或输入2退出:”;
userInput=input.nextInt();
}而(userInput<1&userInput!=2 | | userInput>=2);
System.out.println(“由blabla编写”);
}
}

通过编辑,这个问题更有意义。但答案是一样的:通常的做法是只使用您拥有的字符串数组。您不需要第二个数组

    // good!
    String[] answer = {"It is certain", "Yes - definitely", "Most likely", "Yes", "Better not tell you now", "Con't count on it", 
                    "Outlook not so good", "It is decidedly so", "You may rely on it", "Outlook good", "Reply hazy, try again", 
                    "Cannot predict now", "My reply is no", "Very doubtful", "Without a doubt", "As I see it, yes", 
                    "Signs to point yes", "Ask again later", "Concentrate and ask again", "My source say no"};

    // nope, lose it
    // int answerList[] = new int[20];

    // skip a bit...

    // just use the length of the answer array
                System.out.println(answer[generator.nextInt( answer.length )]); 

就这样。

通过您的编辑,这个问题更有意义。但答案是一样的:通常的做法是只使用您拥有的字符串数组。您不需要第二个数组

    // good!
    String[] answer = {"It is certain", "Yes - definitely", "Most likely", "Yes", "Better not tell you now", "Con't count on it", 
                    "Outlook not so good", "It is decidedly so", "You may rely on it", "Outlook good", "Reply hazy, try again", 
                    "Cannot predict now", "My reply is no", "Very doubtful", "Without a doubt", "As I see it, yes", 
                    "Signs to point yes", "Ask again later", "Concentrate and ask again", "My source say no"};

    // nope, lose it
    // int answerList[] = new int[20];

    // skip a bit...

    // just use the length of the answer array
                System.out.println(answer[generator.nextInt( answer.length )]); 

就是这样。

您试图将数组作为“nextInt”方法的参数传递,该方法只接受一个数字。
如果我明白你想做什么,你可以这样做:

answer[Random.nextInt(answer.length)]

answer.length返回数组的大小,这将允许您在数组范围内弹出一个随机数。

您试图将数组作为“nextInt”方法的参数传递,该方法只接受一个数字。
如果我明白你想做什么,你可以这样做:

answer[Random.nextInt(answer.length)]


answer.length返回数组的大小,这将允许您在数组范围内弹出一个随机数。

您希望得到什么
int anInt=aString应该做什么?考虑使用<代码> EnUM<代码>而不是<代码>字符串< /代码> Enums,我不喜欢。他们真的很不灵活,而且有点巴洛克风格。我永远不会用枚举列表替换这样一个简单的字符串数组。真恶心,嗯。。。只是不同意。Enum是封装的单元,您甚至可以在其上创建一个方法randomAnswer(),该方法返回一个不同于数组的值。如果代码的结构更好的话,这可能是一个品味问题。。。但它不是:)你期望什么应该做什么?考虑使用<代码> EnUM<代码>而不是<代码>字符串< /代码> Enums,我不喜欢。他们真的很不灵活,而且有点巴洛克风格。我永远不会用枚举列表替换这样一个简单的字符串数组。真恶心,嗯。。。只是不同意。Enum是封装的单元,您甚至可以在其上创建一个方法randomAnswer(),该方法返回一个不同于数组的值。如果代码的结构更好的话,这可能是一个品味问题。。。但不是:)谢谢。我只是按照你告诉我的那样做了,但是当我试图编译它时,我得到了一个错误:“错误:非静态方法nextInt(int)不能从静态上下文系统.out.println(answer[Random.nextInt(answer.length)];“你知道我的代码有什么问题吗?哦,这就是我在不仔细检查的情况下复制你的代码所得到的。”
Random
是一个类,您的实例命名为
generator
。“我会修好的。”非常感谢你的帮助。现在可以编译了。不知何故,我的循环一直运行在“请输入1继续播放或2退出:”上。我将尝试修改我的循环体,看看它如何运行谢谢。我只是按照你告诉我的那样做了,但是当我试图编译它时,我得到了一个错误:“错误:非静态方法nextInt(int)不能从静态上下文系统.out.println(answer[Random.nextInt(answer.length)];“你知道我的代码有什么问题吗?哦,这就是我在不仔细检查的情况下复制你的代码所得到的。”
Random
是一个类,您的实例命名为
generator
。“我会修好的。”非常感谢你的帮助。现在可以编译了。不知何故,我的循环一直运行在“请输入1继续播放或2退出:”上。我将尝试修改我的循环体,看看它是如何实现的goes@thank非常感谢你的帮助。现在可以编译了。不知何故,我的循环一直运行在“请输入1继续播放或2退出:”上。我将尝试修改我的循环体,看看它是如何运行的。我认为您不需要whiledo循环。您可以使用一个循环,每次都获取输入,然后检查它-如果是1,则继续循环,如果是2,则中断。@非常感谢您的帮助。现在可以编译了。不知怎的,我的循环