代码帮助-使用扫描仪初始化字符串数组。显示时,数组值全部为空白

时间:2019-05-21 21:42:30

标签: java arrays

我目前正在上一门计算机科学入门课程,对于我们的一项任务,我们基本上必须使用一个数组创建一个神奇的8号球,用户可以在其中输入数组中的值的数量,还可以自己在数组中输入值。数组使用循环。编写代码之后,我对其进行了测试,发现由于某种原因,它没有像预期的那样将输入设置到数组中,经过数小时的调整并试图找出问题出在哪儿,我仍然一无所知。任何帮助将不胜感激。

Scanner input = new Scanner(System.in);

    String answer = "a";
    String question = " ";
    int currentResponse = 0;

    out.println("How many responses would you like there to be?");
    int numResponses = input.nextInt();

    String[] responses = new String[numResponses];

   //This is the loop that's the problem:

   for (int i = 0; i < numResponses; i++)
    {
      out.println("Enter an answer: "); 
      answer = input.nextLine(); 
      responses[i] = answer; //Should set the array value to the input 
      input.next();  //It kept skipping the input part the first time 
//so I added this
    } 

    //This is where I tried two different ways of printing out the array to 
//test it just in case that was the problem:

    out.println(Arrays.toString(responses));

    for (int i = 0; i < responses.length; i++) 
    {
      System.out.print(responses[i] + " ");
    }

    while (!question.equalsIgnoreCase("stop"))
    {
      out.println("What is your question?");
      question = input.nextLine();
      input.next();

      currentResponse = (int)(Math.random()*numResponses);
      out.println(currentResponse);
      out.println(responses[currentResponse]); //It also doesn't appear to 
//actually print this out but I think that that's because the values are //blank 
spaces
    }

    out.println("Thank you for using the Magic Eight Ball");

输出应该类似于

How many responses would you like there to be?
 4 //The input
Enter an answer: 
 s
Enter an answer: 
 d
Enter an answer: 
 f
Enter an answer: 
 g

[s,d,f,g]

    What is your question?
 s
0 //The randomized array index number
s //The value of that index

What is your question?
 d
2
f

What is your question?
 g
1
g

当前是

 4
Enter an answer: 
 s
Enter an answer: 
 d
Enter an answer: 
 f
Enter an answer: 
 g

[, , , ]

    What is your question?
 s
0 //the randomized array index number

What is your question?
 d
2

What is your question?
 g
1

1 个答案:

答案 0 :(得分:1)

input.nextInt();仅使用输入的整数,不使用在其后输入的换行符。为了解决这个问题,您可以使用input.nextLine();在其后使用换行符。这样,在接收响应时,您应该能够从for循环的末尾删除input.next();

您也可以稍后在问题循环中删除input.next();,因为也不需要这么做– input.nextLine();占用了整行。

以下是调整后的代码:

    Scanner input = new Scanner(System.in);

    String answer = "a";
    String question = " ";
    int currentResponse = 0;

    System.out.println("How many responses would you like there to be?");
    int numResponses = input.nextInt();
    input.nextLine();
    String[] responses = new String[numResponses];

    //This is the loop that's the problem:

    for (int i = 0; i < numResponses; i++)
    {
        System.out.println("Enter an answer: ");
        answer = input.nextLine();
        responses[i] = answer; //Should set the array value to the input
    }

    //This is where I tried two different ways of printing out the array to
    //test it just in case that was the problem:

    System.out.println(Arrays.toString(responses));

    for (int i = 0; i < responses.length; i++)
    {
        System.out.print(responses[i] + " ");
    }

    while (!question.equalsIgnoreCase("stop"))
    {
        System.out.println("What is your question?");
        question = input.nextLine();
        currentResponse = (int)(Math.random() * numResponses);
        System.out.println(currentResponse);
        System.out.println(responses[currentResponse]); //It also doesn't appear to
    //actually print this out but I think that that's because the values are //blank spaces
    }

    System.out.println("Thank you for using the Magic Eight Ball");
}

一些其他文档:https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html