import java.io.*;
import java.lang.System;
import java.util.Scanner;
public class Writingclean{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
try{
System.out.println("File Name:");
String FileName = input.next();
FileWriter fw = new FileWriter( FileName + ".txt");
PrintWriter pw = new PrintWriter(fw);
System.out.println("How many questions do you want?");
String y = input.next();
int NumberofQuestions = Integer.parseInt(y);
int QuestionCounter = 1;
int x = 0;
while(x < NumberofQuestions){
System.out.println("Question " + QuestionCounter + ":"); String Question = input.nextLine();
System.out.println("Option 1:"); String Op1 = input.nextLine();
System.out.println("Option 2:"); String Op2 = input.nextLine();
System.out.println("Option 3:"); String Op3 = input.nextLine();
System.out.println("Correct Answer (Option Number):"); String An1 = input.nextLine();
pw.println(Question);
pw.println(Op1);
pw.println(Op2);
pw.println(Op3);
pw.println(An1);
x++;
QuestionCounter++;
}
pw.close();
}catch (IOException e){
System.out.println("Invalid File Name!");
}
}
}
我目前正在开发一个学习型应用程序,并遇到了这个问题:
我想要它,以便用户可以选择他们想要的问题数量,因此需要 while 语句(如果有更好的方法来做到这一点,请告诉我)。然而,在 while 语句之后,控制台似乎跳过了“input.nextLine();”。这意味着没有问题只有选择。
有办法解决这个问题吗?
输出如下:
文件名: 示例 你想问多少个问题? 示例 问题一: 选项 1:
最佳答案
您可以使用 BufferedReader 代替扫描仪类 语法如下
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
String x=input.readLine();
确保导入 java.io.BufferedReader 和 java.io.InputStreamReader 类。 还要处理 IOException。
关于java - 为什么这段代码的输出在 if 语句之后变得困惑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28793413/