2017-10-23 10 views
2
import java.util.Scanner; 

public class Main { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     System.out.print("Enter number (0 to quit): "); 
     //largest to-do 
     double largest = scan.nextDouble(); 
     double count = 0; 

     while (largest != 0) { 
      double input = scan.nextDouble(); 
      //max 
      if (input > largest) { 
       // not zero 
       // while (input > largest){ 
       // 
       // } 

       largest = input; 
       //counter 
       count = 0; 

      } 
      //counter start 
      if(input==largest){ 
       count++; 
      } 
      if (input == 0) { 
       System.out.println("Largest #: " + largest); 
       System.out.println("Occurance: " + count); 
      } 
     } 
    } 
} 

このプログラムは動作します!しかし、その私がその完全なとは思いません... のようなユーザは、それが出力入力を特定の番号を読み取らないようにする方法

-17 -5 -2 -1 -1 -1 0 

を入力しようとした場合:

Max: 0 
Occurrence: 1 

けれども:

:私の心に私はそれになりたいです
Max: -1 
Occurrence: 3 

配列を使用せずにどうすればいいですか?私はカウンターの上から始めたコードの部分を知っています。

ありがとうございます。

+0

0は停止点を意味しますか?文字やその他の文字を停止点にしてみませんか? – dimwittedanimal

+1

0を読んで処理しているので、ゼロを入力したままにしておきたい場合は、「入力!= 0 &&入力>最大」とコメントしたように、「入力>最大」でさらにチェックする必要があります。 – Al1

+0

@dimwittedanimal私はそれをやろうとする可能性がありますが、このプロセスで数字を省略する方法を見てみたかったのですが、 – hellothere

答えて

0

なぜそうでないのですか?

import java.util.Scanner; 

public class Main { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 

     System.out.print("Enter number (0 to quit): "); 

     double largest = 0, nextDouble; 
     int count = 0; 

     while ((nextDouble = scan.nextDouble()) != 0) { 
      if (nextDouble > largest || largest == 0) { 
       largest = nextDouble; 
       count = 1; 
      } 
      else if (nextDouble == largest) count++; 
     } 

     scan.close(); 

     if (count == 0) { 
      System.out.println("No number entered!"); 
      return; 
     } 

     System.out.println("Largest #: " + largest); 
     System.out.println("Occurance: " + count); 

    } 
} 
+0

大丈夫、私は(私のプログラムのために)あなたが何回も最高の負の数を入力すればなぜそれを計算するのか分かりませんでした。 – hellothere

関連する問題