-2
我想建立一个简单的程序,将继续这样做,直到满足某些条件。在这种情况下,无论是对还是错。我一直在玩这个游戏一段时间,但我仍然无法按照我的需要去实现它。我使用while循环不正确吗?
import java.util.Scanner;
public class oddeven {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = scan.nextInt();
boolean play = true;
String restart = " ";
do {
if((num % 2) == 0) {
System.out.println("That number is even!");
} else {
System.out.println("That number is odd!");
}
System.out.println(
"Would you like to pick another number? (Type 'yes' or 'no')");
restart = scan.nextLine();
if(restart == "yes") {
System.out.println("Enter a number: ");
play = true;
} else {
System.out.println("Thanks for playing!");
play = false;
}
}
while(play == true);
}
}
鸵鸟政策比较==改用string1.equals字符串(字符串2) –