2017-10-23 1 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을 숫자로 유지하려면 'input! = 0 && input>가 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

관련 문제