[英]rerun program with input
所以我正在为一个学校项目制作一个简单的程序。 我遇到了一个问题,当我运行它时,它会转到 if 语句,如果你弄错了,你会重新运行程序,并且它不会重新运行它。 无论我是什么 select(是或否),它都只是转到 else 语句。 我似乎看不出有什么问题。 谢谢你的帮助!
package com.nathandevelops;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int correctNumber = (int) (Math.random() * 100 + 1);
System.out.println(correctNumber);
System.out.print("Guess the right number: ");
int guess = scanner.nextInt();
if (guess == correctNumber) {
System.out.println("Correct!");
} else if(guess != correctNumber){
System.out.print("That was incorrect! The correct answer is " + correctNumber + ". Would you like to play again? (yes/no)");
String tryAgain = scanner.next();
if (tryAgain == "yes") {
// main(new String[]{});
System.out.println("ok");
} else if (tryAgain == "no"){
System.out.println("Ok! Goodbye!");
System.out.println(tryAgain);
} else {
System.out.println("OK!");
System.out.println(tryAgain);
}
}
}
}
尝试使用tryAgain.equals()
而不是==
。 在 Java 中,使用.equals()
方法比较字符串,而不是使用相等运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.