0

怎么改密码。。。。

if (yourAnswer = numberOne + numberTwo)
{
   System.out.println("That is correct!");
   counter = counter + 1;
}
else
{
   System.out.println("That is incorrect!");
}

这似乎对我不起作用。谁能帮我?。调试器说:

RandomNumbersProgramThree.java:21:错误:不兼容的类型:int 无法转换为布尔值”。

4

2 回答 2

0

您不能将数字与布尔值相关联。布尔值是真或假,而不是数字,但是您可以创建一个变量来指示某个数字是代表真还是假。这是你的整个程序吗?我想看看“yourAnswer”、“numberOne”和“numberTwo”存储了什么。但现在我将编写一个伪程序来解释该理论。

   import java.util.*;
   public class ExampleProgram
    {
   public static void main(String[] args)
     {
     System.out.println("Please enter two numbers");
     Scanner answer = new Scanner (System.in);

     int yourAnswer = answer.nextInt();
     int numberOne = 1;
     int numberTwo = 2;


     if (yourAnswer == (numberOne + numberTwo))
     {
        System.out.println("That is correct!");
     }
     else
     {
        System.out.println("That is incorrect!");
     }
  }
}

该程序将要求用户输入一个数字,例如数字“3”。然后它会将该数字与您声明的两个数字进行比较,“numberOne”等于 1 的值,“numberTwo”等于 2 的值。因此,在“IF”语句中它说,“If (yourAnswer == (numberOne + numberTwo))”这意味着 1 + 2 = 3,如果用户的答案是 3,那么 3 = 3 并且结果会回来作为真实。

于 2014-12-18T15:06:00.300 回答
0

在 if 语句中,您使用了 '=' 符号,这种类型的等号不是比较 '=',而是用于为变量设置值。即,int x = 2;您需要的等号类型是比较中使用的类型,即 ==。在处理布尔比较时总是使用 ==

于 2015-07-22T00:43:26.080 回答