Java g、 在错误的位置?程序每次都关闭
你好,我想做一个测验应用程序,到目前为止我已经完成了Java g、 在错误的位置?程序每次都关闭,java,string,Java,String,你好,我想做一个测验应用程序,到目前为止我已经完成了 import java.util.Scanner; public class Brotcrunsher { public static void main(String args[]){ Scanner scan = new Scanner(System.in); System.out.println ("Hallo"); System.out.println ("Welcome to the test");
import java.util.Scanner;
public class Brotcrunsher {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println ("Hallo");
System.out.println ("Welcome to the test");
System.out.println ("If you are the admin of the test and would like to edit it please type the password now");
System.out.println ("Otherwhise please say 'begin' to start");
String c = scan.nextLine();
if (c.equals ("edit")) {
System.out.println ("Editor mode coming soon, please edit via code");
}
else {
if (c.equals ("begin")) {
} // end of if
System.out.println ("A flag has more then 1 color right?");
String a = scan.nextLine();
if (a.equals("ja")) {
System.out.println ("You arent dumb, nice.");
System.out.println ("What is 352 + 223");
int b = scan.nextInt();
if (b == 575) {
System.out.println ("That is correct nice");
System.out.println ("You passed the basic questions, going to be tricky now");
System.out.println ("What was the date when Donald Trump became president");
String g = scan.nextLine();
System.out.println ("A = 19.01 B = 20.01 C = 21.01 D= 22.01");
if (g.equals ("B")){
System.out.println ("You are right, lets move on.");
} // end of if
else {
System.out.println ("That is wrong. Better luck next time");
} // end of if-else
}
else {
System.out.println("That isn't right, you failed.");
System.out.println ("If you'd like to repeat the test, go ahead and do so");
}
}
else {
System.out.println ("You arentn a genie");
}
}
}
}
所以我的问题是,我需要把字符串g=scan.NextLine放在哪里才能让它工作?我没有得到任何编译错误,只是控制台出口的。
我试着把它放在IF语句中的beggining和其他一些位置,但它只是停了下来。请提供帮助。如果在运行应用程序时控制台关闭,则可能会出现运行时错误。尝试从已打开的命令外壳运行程序以查看错误
这应该是一个注释,但我没有足够的rep 0:)如果在运行应用程序时控制台关闭,则可能会出现运行时错误。尝试从已打开的命令外壳运行程序以查看错误
这应该是一个注释,但我没有足够的rep 0:)问题是当您到达代码的关键部分时,扫描仪不是空的。您需要清除缓冲区,例如:
public class Brotcrunsher {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println ("Hallo");
System.out.println ("Welcome to the test");
System.out.println ("If you are the admin of the test and would like to edit it please type the password now");
System.out.println ("Otherwhise please say 'begin' to start");
String c = scan.nextLine();
if (c.equals ("edit")) {
System.out.println ("Editor mode coming soon, please edit via code");
}
else {
if (c.equals ("begin")) {
} // end of if
System.out.println ("A flag has more then 1 color right?");
String a = scan.nextLine();
if (a.equals("ja")) {
System.out.println ("You arent dumb, nice.");
System.out.println ("What is 352 + 223");
int b = scan.nextInt();
if (b == 575) {
System.out.println ("That is correct nice");
System.out.println ("You passed the basic questions, going to be tricky now");
System.out.println ("What was the date when Donald Trump became president");
System.out.println ("A = 19.01 B = 20.01 C = 21.01 D= 22.01");
scan.nextLine(); // FEED the new-line character
String g = scan.nextLine();
if (g.equals ("B")){
System.out.println ("You are right, lets move on.");
} // end of if
else {
System.out.println ("That is wrong. Better luck next time");
} // end of if-else
}
else {
System.out.println("That isn't right, you failed.");
System.out.println ("If you'd like to repeat the test, go ahead and do so");
}
}
else {
System.out.println ("You arentn a genie");
}
}
}
}
更多详情:
问题在于,当您到达代码的关键部分时,扫描仪不是空的。您需要清除缓冲区,例如:
public class Brotcrunsher {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println ("Hallo");
System.out.println ("Welcome to the test");
System.out.println ("If you are the admin of the test and would like to edit it please type the password now");
System.out.println ("Otherwhise please say 'begin' to start");
String c = scan.nextLine();
if (c.equals ("edit")) {
System.out.println ("Editor mode coming soon, please edit via code");
}
else {
if (c.equals ("begin")) {
} // end of if
System.out.println ("A flag has more then 1 color right?");
String a = scan.nextLine();
if (a.equals("ja")) {
System.out.println ("You arent dumb, nice.");
System.out.println ("What is 352 + 223");
int b = scan.nextInt();
if (b == 575) {
System.out.println ("That is correct nice");
System.out.println ("You passed the basic questions, going to be tricky now");
System.out.println ("What was the date when Donald Trump became president");
System.out.println ("A = 19.01 B = 20.01 C = 21.01 D= 22.01");
scan.nextLine(); // FEED the new-line character
String g = scan.nextLine();
if (g.equals ("B")){
System.out.println ("You are right, lets move on.");
} // end of if
else {
System.out.println ("That is wrong. Better luck next time");
} // end of if-else
}
else {
System.out.println("That isn't right, you failed.");
System.out.println ("If you'd like to repeat the test, go ahead and do so");
}
}
else {
System.out.println ("You arentn a genie");
}
}
}
}
更多详情:
我尝试了您的代码,最后一次扫描。nextLine()似乎被跳过。看看这篇文章。
调用的位置不是问题。我尝试了您的代码,最后一次扫描。nextLine()似乎被跳过。看看这篇文章。
您呼叫的位置不是问题。我认为代码System.out.println(“A=19.01 B=20.01 C=21.01 D=22.01”);应该在字符串g=scan.nextLine()前面;因为您需要首先将选项提供给用户,然后用户将做出选择 我认为代码System.out.println(“A=19.01b=20.01c=21.01d=22.01”);应该在字符串g=scan.nextLine()前面;因为您需要首先将选项提供给用户,然后用户将做出选择 就像它不是关闭而是暂停。。。当我按下一个键时,它会关闭。然后它可能正在等待你的输入。当你的输入被给出(按下一个键)时,程序仍可能由于运行时错误而终止。试着从命令行运行应用程序:比如它没有关闭,但是它暂停了。。。当我按下一个键时,它会关闭。然后它可能正在等待你的输入。当你的输入被给出(按下一个键)时,程序仍可能由于运行时错误而终止。尝试从命令行运行应用程序:接受答案表示相同:)接受答案表示相同:)