对于学校,我们必须完成以下作业;
“编写一个程序,它从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();
}
}这就是我到目前为止的情况,但我有两个问题,
当我执行该程序时,输入一个随机生成的问题的答案,然后按enter键,出现一个错误(java.util.InputMismatchException)
我真的不知道如何让程序将用户输入和问题存储在一个单独的文件中(我几周前才开始编码)。
任何帮助都将不胜感激。提前谢谢你!
发布于 2019-02-27 07:40:05
考虑一下:
String answers[] = new String[ question.length() ];
...
answers[random] = scanner.nextLine();https://stackoverflow.com/questions/54895816
复制相似问题