2013-08-20 114 views
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」,但它需要一個「布爾」。請記住,我是一個新手,所以我還沒有在我的Java類中學過布爾值。Java:不兼容類型(int /布爾值)

+0

如何讓while循環進入正確的循環?在應用Mik378的修復之後,環路不會激活。 –

+0

如果在'while'行放置斷點,'counter2'和'counter3'的當前值是多少? – Mik378

回答

3

您無法將值與=(作爲賦值運算符)進行比較。使用==來比較您的值。更改

while(counter2 = counter3){ 

while(counter2 == counter3){ 

這裏是一個introductory page for Java operators

1

您使用賦值運算符:

while(counter2 = counter3) 

,而不是平等的經營者:

while(counter2 == counter3) 
0

現在的問題是:

   while(counter2 = counter3) 

=用於分配和張貼此語句,counter2將被分配counter3的值。因此你的while循環不會按照你想要的方式行事。您需要使用==來將計數器2與計數器3進行比較

   while(counter2 == counter3)