Edt*_*Big 2 java eclipse if-statement boolean
我只是在玩java并想在用户做一个简单的程序; 我必须猜测/输入正确的数字,直到它正确为止.我能做什么,程序可以继续运行,打印出"再猜一猜",直到用户/我输入正确的数字.也许布尔值?我不确定.这就是我到目前为止所拥有的.
import java.util.Scanner;
public class iftothemax {
public static void main(String[] args) {
int myInt = 2;
// Create Scanner object
Scanner input = new Scanner(System.in);
//Output the prompt
System.out.println("Enter a number:");
//Wait for the user to enter a number
int value = input.nextInt();
if(value == myInt) {
System.out.println("You discover me!");
}
else {
//Tell them to keep guessing
System.out.println("Not yet! You entered:" + value + " Make another guess");
input.nextInt();
}
}
Run Code Online (Sandbox Code Playgroud)
您可能希望使用while循环来重复某些代码:
while (value != myInt) {
System.out.println("Not yet! You entered: " + value + ". Make another guess");
value = input.nextInt();
}
System.out.println("You discovered me!");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |