【发布时间】:2010-09-23 16:31:36
【问题描述】:
好的,非常菜鸟的问题。我正在制作一个让用户设计调查的 CLI 应用程序。他们首先输入问题,然后输入选项的数量和选项。我正在使用扫描仪来获取输入,出于某种原因,它允许用户输入大多数内容,但不能输入问题的文本。代码如下:sn-p。
String title = "";
Question[] questions;
int noOfQuestions = 0;
int[] noOfChoices;
Scanner entry = new Scanner(System.in);
System.out.println("Please enter the title of the survey: ");
title = entry.nextLine();
System.out.println("Please enter the number of questions: ");
noOfQuestions = entry.nextInt();
noOfChoices = new int[noOfQuestions];
questions = new Question[noOfQuestions];
for (int i = 0; i < noOfQuestions; i++) {
questions[i] = new Question();
}
for (int i = 0; i < noOfQuestions; i++) {
System.out.println("Please enter the text of question " + (i + 1) + ": ");
questions[i].questionContent = entry.nextLine();
System.out.println("Please enter the number of choices for question " + (i + 1) + ": ");
questions[i].choices = new String[entry.nextInt()];
for (int j = 0; j < questions[i].choices.length; j++) {
System.out.println("Please enter choice " + (j + 1) + " for question " + (i + 1) + ": ");
questions[i].choices[j] = entry.nextLine();
}
}
谢谢:)
【问题讨论】:
-
你确定这是所有相关代码吗?您是否也从扫描仪中读取“noOfQuestions”?
-
是的,在 Scanner 声明和 for 循环之间还有一些代码。我把它们排除在外是因为它会使引用的代码太长。立即将它们重新添加...
标签: java input java.util.scanner