2 回答
TA贡献2012条经验 获得超12个赞
你必须阅读java中的if else。那就是条件语句。就像如果你想在某个条件为真时执行某件事,否则如果条件不满足,它将执行其他部分。
if(condition) {
//Some task
} else {
// some task
}
就像您可以在代码中添加 if else 一样。
import java.util.*;
public class RandomGuessMatch
{
public static void main(String[] args) {
boolean right = true;
int MAX = 5;
int MIN = 1;
int guess;
int random;
Scanner ask = new Scanner(System.in);
System.out.println("Input your guess between 1 through 5.");
guess = ask.nextInt();
random = MIN + (int)(Math.random() * MAX);
System.out.println("Your guess was " + guess);
System.out.println("The random number was " + random);
if (guess == random) {
right = true;
} else {
right = false;
}
System.out.println(right);
}
}
添加回答
举报