对于学校,我们必须完成以下任务;
“编写一个程序,该程序从10个开放式问题的字符串数组中随机抽取,该问题的答案由用户输入到控制台,并存储在带有问题的“ output.txt”文件中。”
import java.util.Scanner;
public class Questions {
public static void main(String args[]) {
int random = (int)(Math.random()*10);
int length;
Scanner scanner = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
int answer;
String question[] = new String[11];
question[0] = "What is your name?";
question[1] = "When is your birthday?";
question[2] = "What color are your shoes?";
question[3] = "How are you feeling today? ";
question[4] = "How old are you?";
question[5] = "What is your favorite color?";
question[6] = "How many siblings do you have?";
question[7] = "How many pets do you have?";
question[8] = "What is the weather like today?";
question[9] = "How tall are you?";
question[10] = "What is today's date?";
System.out.println(question[random]);
answer = scanner.nextInt();
}
}
这是我到目前为止的内容,但是我有2个问题,
当我执行程序时,键入一个随机生成的问题的答案,然后按Enter,将显示错误(java.util.InputMismatchException)
我真的不知道如何使程序将用户输入和问题存储在一个单独的文件中(我才几周前才开始编码)
任何帮助将不胜感激。预先谢谢你!
答案 0 :(得分:0)
考虑:
String answers[] = new String[ question.length() ];
...
answers[random] = scanner.nextLine();