1

おそらく本当にばかげた何かが欠けていますが、while ループはエラー メッセージの出力を停止しません。

誰かが簡単に見て、私を正しい方向に向けることができますか?

package week5;

import java.util.*;
public class Week5 {       
    public static void main(String[] args) {    
        Scanner myKeyboard = new Scanner(System.in);        
        inputInt();        
    }

    public static int inputInt(){        
        Scanner myKeyboard = new Scanner(System.in);        
        System.out.println("Enter number:");        
        int num;                                
        boolean carryOn = true;                       
        while (carryOn = true) {                 
                {                                    
                  try {                       
                      num = myKeyboard.nextInt();                      
                      carryOn = false;                                                                  
                      }                  
                  catch (Exception e) {
                    System.out.println ("Integers only");                                         
                   }                       
                  }                                                    
          }
        return 0;
      }
4

3 に答える 3

5

この行が問題です

    while (carryOn = true) {

比較演算子を使用する代わりに==、代入演算子を使用しています=。したがって、本質的に、すべての反復に設定carryOntrue、条件が本質的に次のようになるため、ループの本体を自動的に実行します

    while (true) {

その問題の行を次のように変更するだけです

    while (carryOn == true) {
于 2013-11-04T14:03:55.400 に答える
0

あなたの場合:

     while(carryOn==true) 

また

     while(carryOn) 

あなたの問題を解決します

于 2013-11-04T14:05:33.700 に答える