Java 不在if/else语句中打印我的代码
因此,我用java创建了jeapordy,我不在乎我是否拼写错了(如果我拼写错了),但到目前为止,我只有一个问题编码,只有一个答案,它会问这个问题,但只打印出你是错的,即使答案是对的 它问的是第一个历史问题,答案是乔治,但它打印出来的答案是错误的。第一个历史问题也值100英镑。我还没有开始编写数学部分的代码 谢谢你能帮我解决问题!这可能真的很简单,因为我是初学者Java 不在if/else语句中打印我的代码,java,if-statement,Java,If Statement,因此,我用java创建了jeapordy,我不在乎我是否拼写错了(如果我拼写错了),但到目前为止,我只有一个问题编码,只有一个答案,它会问这个问题,但只打印出你是错的,即使答案是对的 它问的是第一个历史问题,答案是乔治,但它打印出来的答案是错误的。第一个历史问题也值100英镑。我还没有开始编写数学部分的代码 谢谢你能帮我解决问题!这可能真的很简单,因为我是初学者 import java.util.Random; import java.util.Scanner; public class ga
import java.util.Random;
import java.util.Scanner;
public class game {
public static void main (String[] args){
//Utilites
Scanner s = new Scanner(System.in);
Random r = new Random();
//Variables
String[] mathQuestions;
mathQuestions = new String[3];
mathQuestions[0] = ("What is the sum of 2 + 2");
mathQuestions[1] = ("What is 100 * 0");
mathQuestions[2] = ("What is 5 + 5");
String[] historyQuestions;
historyQuestions = new String[3];
historyQuestions[0] = ("What is General Washingtons first name?");
historyQuestions[1] = ("Who won WWII, Japan, or USA?");
historyQuestions[2] = ("How many states are in the USA?");
//Intro
System.out.println("Welome to Jeapordy!");
System.out.println("There are two categories!\nMath and History");
System.out.println("Math History");
System.out.println("100 100");
System.out.println("200 200");
System.out.println("300 300");
System.out.println("Which category would you like?");
String categoryChoice = s.nextLine();
System.out.println("For how much money?");
int moneyChoice = s.nextInt();
if (categoryChoice.equalsIgnoreCase("history")){
if (moneyChoice == 100){
System.out.println(historyQuestions[0]);
String userAnswer = s.nextLine();
s.nextLine();
if (userAnswer.equalsIgnoreCase("george")){
System.out.println("Congratulations! You were right");
}
else{
System.out.println("Ah! Wrong answer!");
}
}
}
}
}
调用
nextLine()
时,一个换行符未读,因此后续调用nextLine()
将返回一个空字符串(因为它一直读到行尾)。在读取/放弃此尾随换行符之前调用一次newLine()
:
if (moneyChoice == 100) {
System.out.println(historyQuestions[0]);
s.nextLine(); // <--
String userAnswer = s.nextLine();
System.out.println(userAnswer);
...
if(moneyChoice==100){
System.out.println(历史问题[0]);
s、 nextLine();//当您调用nextInt()
时,换行符处于未读状态,因此后续调用nextLine()
将返回一个空字符串(因为它一直读到行尾)。在读取/放弃此尾随换行符之前调用一次newline()
:
if (moneyChoice == 100) {
System.out.println(historyQuestions[0]);
s.nextLine(); // <--
String userAnswer = s.nextLine();
System.out.println(userAnswer);
...
if(moneyChoice==100){
System.out.println(历史问题[0]);
s、 nextLine();//int-moneyChoice=s.nextLine();
只读取整数。它留下一个换行等待读取。然后String-userAnswer=s.nextLine();
读取一个明显不同于“george”的空行.解决方案:在int之后立即读取换行符,并在整个程序中执行此操作。您可以创建自己的方法nextIntAndLine()
int moneyChoice=s.nextLine();
只读取整数。它留下一个换行等待读取。然后String userAnswer=s.nextLine();
读取一个明显不同于“george”的空行.解决方案:在int之后立即读取换行符,并在整个程序中执行此操作。您可以创建自己的方法nextIntAndLine()
好的,谢谢!它起作用了。我把它放在后面,因为我的一位老师告诉我这是一个愚蠢的eclipse错误的修复程序,我从来不知道要放在前面。好的,谢谢!它起作用了。我放在后面,因为我的一位老师告诉我这是一个愚蠢的eclipse错误的修复程序,我从来不知道要放在前面。