2012-02-06 4 views
0

ループから最大数を検索しようとしていますが、発生回数です。これまでに書いたコードはここにあります。ループからの最大数の検索

public class MaxNumbers { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     int max = 0; 
     int number = 0; 
     int count_max = 0; 
     while (true) { 
      number = input.nextInt(); 
      if (number == 0) { 
       break; 
      } 
      number = max; 
      if (max > number) { 
       max=number; 
       count_max++; 
      } 

     } 
     System.out.println("Max Number: " + number); 
     System.out.println("Occurences: " + count_max); 
    } 

出力がゼロになっていますが、論理的なエラーは何ですか?

number = max 

if (max > number) { 

最大続いあなただけにそれらを設定するため、数よりも大きくなることはありません:

+0

数== 0は、多分あなたは反復がinput.nextInt() ''の値をチェックします決して場合は、ループを壊しているが、あなたの 'while'文は、私は、コンパイラはいくつかを作る感じている無限に見えますこれを奇妙な最適化、最大数(最大数?)を得るあなたの論理は疑問です –

+0

これは宿題の質問であれば、そのようにタグを付けてください。 :) – Erica

+1

以下のコード例がうまくいかない場合は、変数 'number'を表示するか、デバッガを使って何が起きているのかを確認してください。 – UmNyobe

答えて

2

これは私があなたが達成しようとしていると思っている私のバージョンです。主に働いています。数字で始める必要があることに注意してください。 long無視されたり、「トリミング」を取得

import java.util.Scanner; 

public class Test { 

    public static final void main(String[] args) { 
     long a = 0, b = 0, c = 0, d = 0; 

     System.out.println("Type some numbers!"); 

     Scanner sc = new Scanner(System.in); 
     String in = sc.nextLine(); 
     Scanner ln = new Scanner(in); 

     while (ln.hasNextLong()) { 
      a = ln.nextLong(); 
      if (a > b) { 
       b = a; 
       ++d; 
      } 
      ++c; 
     } 

     System.out.println("\n info:"); 
     System.out.println("  highest:" + b); 
     System.out.println("  iterations:" + c); 
     System.out.println("  overrides:" + d); 
    } 
} 
0

number = max

+0

まだ私はゼロになっています。 – Kaushank

+0

もしも(max> number)も間違っていれば、maxは0から始まり、numberより大きい場合にのみ再割り当てされます。numberが負でない限りは再割り当てされません。(number> max)try –

+0

しかし、最大数は0になり、発生数は1になります。 – Kaushank

1

がはいている行を削除し、次の行を持っています前の行と等しくなるようにする。

+0

まだゼロになっています。 – Kaushank

+1

あなたは何を変えましたか? –

0

問題のいくつかをwiコード。これはあなたのために動作するはずです:

public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     int max = Integer.MIN_VALUE; 
     int number = 0; 
     int count_max = 0; 
     while (true) { 
      number = input.nextInt(); 
      if (number == 0) { 
       break; 
      } 
      if(max == number){ 
       count_max++; 
      } 
      if (max < number) { 
       max=number; 
       count_max = 1; 
      } 
     } 
     System.out.println("Max Number: " + max); 
     System.out.println("Occurences: " + count_max); 
    } 
0

"number = max"の行は変更する必要があります。あなたが持っているコードは、ループのたびにnumberの値をmaxの値に設定しています。これは最初のループの値が0であるため、無期限にゼロを維持します。つまり、あなたの最大値は決して変化しません。

あなたはその行を望んでいませんが、そのようなものが欲しいです。 数字がの場合、同じ値の(==)、の場合、count_maxに1を加算する必要があります。

最後のIfブロックに少し微調整が必​​要です。 あなたはそれに1を追加し、1にリセットあなたCOUNT_MAXにする必要はありませんあなたは、新しい最高値(すなわち、数>最大、ない他の方法で回避)、を見つけるた場合。

0

ここでは、強調表示犯した過ちとコードの修正版です。

Scanner input = new Scanner(System.in); 
int max = 0; 
int number = 0; 
int count_max = 0; 
while (true) { 
    number = input.nextInt(); 
    if (number == 0) { 
     break; 
    } 
    // you are assigning number = max, and max is 0 initially, so number 
    // =0; 
    // number =max; 
    // if number =0 and max =0 then below condition will never   // satisfied. 
    if (max < number) { 
     max = number; 
     // it means every time when you have any number greater then the    // second one just add it. 
      // count_max++; 
      count_max = 0; 
     } 

    // This condition will make sure that only add count by 1 if max = 
    // currentNum 
    if (max == number) { 

     count_max++; 
    } 

} 
// You have written number instead of max down there. 
System.out.println("Max Number: " + max); 
System.out.println("Occurences: " + count_max); 
関連する問題