2014-02-21 1 views
-4

입력 한 가장 낮은 점수를 인쇄하려면이 코드가 필요하며 여기 몇 시간 동안 앉아 있고 그것을 수행하는 방법을 알 수 없습니다. 배열을 사용하고 있지만 루프를 사용해야합니다. 힌트가 있습니까?for 루프를 사용하여 입력 한 가장 낮은 점수를 찾으려면 어떻게합니까?

예 출력 :

How many scores? 3 
    Enter score 1: 90 
    Enter score 2: 80 
    Enter score 3: 100 
    Lowest score is 80 

내 코드 :

int score,numScore,count; 


    Scanner keyboard = new Scanner(System.in); 
    System.out.print("How many scores? "); 
    numScore = keyboard.nextInt(); 

    for(count = 1; count <= numScore; count++){ 
    System.out.print("Enter score " + count + ":"); 
    score = keyboard.nextInt(); 
    } 
+0

적어도, 사용하는 언어를 알려주는 것이 도움이 될지 모르겠습니다. –

+0

자바입니다. 죄송합니다. – user3003920

+0

이 질문에 대한 대답은 이미 있습니다 [답변] (http://stackoverflow.com/questions/19435242/getting-the-lowest-and-highest-value-from-integers-without-using-arrays) – eatSleepCode

답변

0

단순히 변수에 가장 낮은 점수를 저장하고 그것에게 낮은 점수가 발생 될 때마다 업데이트 :

int lowestScore = 100; 

for(count = 1; count <= numScore; count++){ 
    System.out.print("Enter score " + count + ":"); 
    score = keyboard.nextInt(); 
    if(score < lowestScore){ 
     lowestScore = score; 
    } 
} 
+0

정확히 동일한 코드 (대략)에서 하이 5 만 13 초 간격으로. – panoptical

+0

글쎄, 그 많은 옵션 :) 변수 명명은 꽤 표준 하 - 하입니다. – sashkello

0

을 각 키보드 입력과 비교할 변수를 유지 관리하십시오.

int lowestScore = 100000; 
for(count = 1; count <= numScore; count++){ 
    System.out.print("Enter score " + count + ":"); 
    score = keyboard.nextInt(); 

    if (score < lowestScore) { 
      lowestScore = score; 
    } 
} 
0

if(lower == -1) { 
     lower = score; 
    } else if(score < lower) { 
     lower = score; 
    } 

마지막으로 낮은 필요한 값을 가지고

0

추가를 루프 안쪽이

int lower = -1; 

같은 가장 낮은 점수를 찾기 위해 3 선

를 변수를 유지
int score,numScore,count; 
int minimum; 

Scanner keyboard = new Scanner(System.in); 
System.out.print("How many scores? "); 
numScore = keyboard.nextInt(); 

for(count = 1; count <= numScore; count++){ 
    System.out.print("Enter score " + count + ":"); 
    score = keyboard.nextInt(); 
    if (count == 1) minimum = score; 
    if (score < minimum) minimum = score; 

} 
관련 문제