首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >问与答程序

问与答程序
EN

Stack Overflow用户
提问于 2019-02-27 07:30:27
回答 1查看 52关注 0票数 0

对于学校,我们必须完成以下作业;

“编写一个程序,它从10个开放式问题的字符串数组中随机抽取,用户将其答案键入控制台,并将其存储在一个"output.txt”文件中,前面有问题。“

代码语言:javascript
运行
复制
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)

我真的不知道如何让程序将用户输入和问题存储在一个单独的文件中(我几周前才开始编码)。

任何帮助都将不胜感激。提前谢谢你!

EN

回答 1

Stack Overflow用户

发布于 2019-02-27 07:40:05

考虑一下:

代码语言:javascript
运行
复制
   String answers[] = new String[ question.length() ];
   ...
   answers[random] = scanner.nextLine();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54895816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档