打印具体的字符

问题描述:

我有一个字符串输入,其中,输入有一个团队名称和分数由一个空格分隔。例如bb 3teamd 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

@ScaryWombat确实是的,我想去外面的箱子,我的问题与'+ =',谢谢,随时让它接受回答 – user1058652

我没试过你的代码,但我想一个很大的问题是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? – user1058652

+0

@android在Eclipse为我工作得很好'多少案件多少球队进入球队,比分 test1的2 进入球队,得分 我23支 我 多少球队进入球队,得分 test2 4 输入球队得分 me_again 23 me_again me me_again ' –

如果我不得不做这样的事情,我会做这样的:

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); 
     } 
     }