Java 我需要使用布尔值true或false来告诉用户他/她的猜测是否正确

Java 我需要使用布尔值true或false来告诉用户他/她的猜测是否正确,java,Java,我必须编写一个代码,让用户猜测计算机选择的数字。我还需要使用布尔值true或false来告诉用户用户是否正确。我尝试使用if-else语句,但在Java中还没有学会。这就是我到目前为止所做的: import java.util.*; public class RandomGuessMatch { public static void main(String[] args) { boolean right = true; int MAX = 5;

我必须编写一个代码,让用户猜测计算机选择的数字。我还需要使用布尔值true或false来告诉用户用户是否正确。我尝试使用if-else语句,但在Java中还没有学会。这就是我到目前为止所做的:

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 (right = 



    }
}

您必须阅读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);
    }
}

对于这样一个简单的问题,你应该搜索更多

无论如何:

如果变量
right
布尔值

  if (right) {
     // do something
  }

boolean correct=guess==random