Java:不兼容的类型(int / boolean)

时间:2013-08-20 00:52:33

标签: java boolean int incompatibletypeerror

import java.io.*;
public class AdamHmwk4 {
    public static void main(String [] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int counter1;
        int counter2;
        int counter3;
        String answer = "";

        System.out.println("Welcome to Adam's skip-counting program!");
        System.out.println("Please input the number you would like to skip count by.");
        counter1 = Integer.parseInt(br.readLine());

        System.out.println("Please input the number you would like to start at.");
        counter2 = Integer.parseInt(br.readLine());

        System.out.println("Please input the number you would like to stop at.");
        counter3 = Integer.parseInt(br.readLine());

        System.out.println("This is skip counting by" + counter1 + ", starting at" + counter2  + "and ending at" + counter3 +":");

        while (counter2 = counter3) {
            counter2 = counter2 + counter1;
            counter3 = counter2 + counter1;
        }
    }
}

我正在尝试制作跳过计数程序。编译此代码时,行while(counter2 = counter3){显示为不兼容类型错误。编译器说它找到了一个“int”但它需要一个“boolean”。请记住我是一个新手,所以我还没有在我的Java课程中学过布尔。

3 个答案:

答案 0 :(得分:3)

您无法将值与=进行比较,==是赋值运算符。使用while(counter2 = counter3){ 比较您的值。变化

while(counter2 == counter3){

{{1}}

这是一个introductory page for Java operators

答案 1 :(得分:1)

您使用赋值运算符:

while(counter2 = counter3)

而不是相等运算符:

while(counter2 == counter3)

答案 2 :(得分:0)

问题在于:

               while(counter2 = counter3)

=用于赋值并发布此语句,counter2将被赋予counter3的值。因此,你的while循环不会按照你想要的方式运行。您需要使用==将counter2与计数器3进行比较

               while(counter2 == counter3)