0
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 类中学习布尔值。

4

3 回答 3

3

您不能将值与=赋值运算符进行比较。用于==比较您的值。改变

while(counter2 = counter3){

while(counter2 == counter3){

这是Java 运算符的介绍页面

于 2013-08-20T00:53:37.147 回答
1

您使用赋值运算符:

while(counter2 = counter3)

而不是相等运算符:

while(counter2 == counter3)
于 2013-08-20T00:54:10.240 回答
0

这是问题所在:

               while(counter2 = counter3)

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

               while(counter2 == counter3)
于 2013-08-20T00:55:19.007 回答