2017-10-10 5 views
1

해당 입력에 팀 이름과 점수가 하나의 공백으로 구분 된 문자열 입력이 있습니다. 예를 들어 bb 3, teamd 5 우승자, 나는 다음과 같은 일을 해요 가장 높은 점수를 득점 승자 팀을 얻기 위하여 teamd특정 문자 인쇄

해야한다 :

Scanner scanner = new Scanner(System.in); 
    int cases = scanner.nextInt(); 

    printWinnerTeam(cases); 



} 

public static void printWinnerTeam(int cases) { 
    Scanner scanner = new Scanner(System.in); 
    String str = ""; 
    String winnerTeam = ""; 
    int winnerScore = 0, countedChar = 0; 

    for (int i = 0; i < cases; i++) { 
     str += scanner.nextLine(); 
    } 
    char[] arr = str.toCharArray(); 
    for (int i = 0; i < arr.length; i++) { 
     countedChar++; 
     if (arr[i] == ' ') { 
      if (str.charAt(i + 1) > winnerScore) { 
       winnerTeam = ""; 
       winnerScore = (int) str.charAt((int) i + 1); 
       for (int j = 0; j < countedChar; j++) { 
        winnerTeam += str.charAt(j); 
       } 
       countedChar = 0; 
      } else { 
       //winnerTeam = ""; 
       countedChar = 0; 
      } 
     } 
    } 
    System.out.println(winnerTeam); 
} 

을하지만 지사 작동하지 않을 것, 그것은 인쇄 배선 결과, 방법 그 일을 기대하는 것처럼 만드나요?

+0

어떻게 간단하지만 당신이 가서 바로 처리에 대해. 입력 된 점수가 가장 큰 경우 숫자와 팀 이름을 저장합니다. String.split (","); –

+1

나는 당신의 코드를 시도하지 않았다. 그러나 나는 큰 문제가'str + = scanner.nextLine(); '의 연결이라는 것을 상상할 것이다. 새로운 라인은'number' 이전 라인의 끝 부분에 곧바로 추가 될 것이다. –

+0

@ SkaryWombat 정말로 예, 저는'+ =', 감사합니다, 내 질문에 답을 받아들이겠습니다. – user1058652

답변

1

내가 코드를 시도하지 않은,하지만 큰 문제 상상은 str += scanner.nextLine();의 연결이 새로운 라인이에서보세요 이전 라인

번호를 말에 바로 추가 할 것입니다

Scanner sc = new Scanner(System.in); 
    int bestScore = Integer.MIN_VALUE; 
    String team = "Nothing entered"; 
    System.out.println("how many teams"); 
    int count = sc.nextInt(); 
    sc.nextLine(); 
    while (count-- > 0) { 
     System.out.println("Entered team,score"); 

     String line = sc.nextLine(); 
     String arr [] = line.split(" "); 
     // check size - TBD 
     if (Integer.parseInt(arr[1]) > bestScore) { 
      bestScore = Integer.parseInt(arr[1]); 
      team = arr[0]; 
     } 
    } 

    System.out.println("nest team is " + team + " with a score of " + bestScore); 
+1

@AndyTurner 감사합니다. 화면을 청소해야합니다. –

+0

실례합니다. 잘못된 코드 [여기] (https://ideone.com/tB1Akw), 내 코드로 runTimeError를 얻으시겠습니까? 이클립스에서 나를 위해 잘 작동 @android – user1058652

+0

'얼마나 많은 경우 2 얼마나 많은 팀 진출 팀, 나 23 개 나 얼마나 많은 팀 진출 팀 점수, TEST1 2 진출 팀 점수, 점수 TEST2 4 진출 팀, me_again 23 me_again 점수 나 me_again ' –

0

내가 이런 식으로 그것을 할 것 같은 무언가를해야하는 경우 :

import java.util.*; 
     public class winner { 
      public static void main (String args []) { 
      Scanner scanner = new Scanner(System.in); 
      int cases = scanner.nextInt(); 

      printWinnerTeam(cases); 
     } 

    public static void printWinnerTeam(int cases) { 
      Scanner scanner = new Scanner(System.in); 
      String winnerTeam = ""; 
      String Team=""; 
      int winnerScore = 0, score = 0; 

      for (int i = 0; i < cases; i++) { 
       Team = scanner.next(); 
       score = scanner.nextInt(); 
       if(score > winnerScore){ 
       winnerTeam = Team; 
       winnerScore = score; 
       } 

      } 
      System.out.println("Winner team" + winnerTeam + "Score:" + winnerScore); 
     } 
     }