2016-01-08 24 views
1

我寫了一些代碼,它允許我獲取用戶玩過的遊戲的用戶輸入。我已經完成了大部分工作,但是我已經到了一個地步,我不知道該在哪裏添加此代碼totalScore = totalScore + score;。每次添加新遊戲時,此代碼都會讓我獲得玩家總分。除此之外,我還對如何獲得用戶試圖輸入的無效條目數量感到困惑,這意味着我需要對每個無效條目進行計數,以便稍後顯示無效條目的總數。不確定在哪裏添加某段代碼

import java.util.Scanner; 
public class REQ3 
{ 
    public static void main (String[] args) 
    { 

    String playername;  
    String line; 
    String[] list = new String[100]; 
    int count = 0; 
    int score; 
    int time; 
    int gamesplayed =0; 
    int totalScore =0; 


    Scanner sc = new Scanner(System.in); 


     System.out.println("Please enter your name"); 

     playername = sc.nextLine(); 

     if(playername.equals("")) 
     { 
      System.out.println("Player name was not entered please try again"); 
      System.exit(0); 
     } 

     System.out.println("Please enter your game achivements (Game name:score:time played) E.g. Minecraft:14:2332"); 

     while (count < 100){ 

      line = sc.nextLine(); 

      if(line.equals("quit")){ 
        break; 
        } 
      if(line.equals("")){ 
       System.out.println("Nothing was entered please try again"); 
        break; 
        } 

      if(!(line.contains(":"))){ 
       System.out.println("Please enter achivements with the proper \":\" sepration\n"); 
       break; 
      } 

      list[count]=line; 
      System.out.println("list[count]" + list[count]); 

      count++; 
      gamesplayed++; 

     for (int i=0; i<count; i++){ 
      line=list[i]; 
      String[] elements =line.split(":"); 

      if (elements.length !=3){ 
       System.out.println("Error please try again, Please enter in the following format:\nGame name:score:timeplayed"); 
        break; 
      } 

      try { 
       score = Integer.parseInt(elements[1].trim()); 
           totalScore = totalScore + score; // added here 
      } catch(NumberFormatException ex) { 
       System.out.println("Incorrect score data, Please enter a valid integer"); 
      } 
      try { 
       time=Integer.parseInt(elements[2].trim()); 
      } catch(NumberFormatException ex) { 
       System.out.println("Incorrect time data, Please enter a valid integer"); 
      } 




     }   
    } 
     System.out.println("Player : " + playername); 
     System.out.println("--------------------------------"); 
     System.out.println("Games Played: " +gamesplayed); 
}   

}

+2

在你的「請再試一次」型消息之後,你應該使用'continue'而不是'break'。 –

+1

「'totalScore = totalScore +得分;''必須在你爲'得分'賦值之後去某個地方 - 顯然它必須進入一個循環。這把它縮小到只有幾行;爲什麼不試圖找出最好的地方呢? –

+0

'try {} \t \t \t score = Integer.parseInt(elements [1] .trim()); \t \t \t \t totalScore = totalScore + score; \t \t \t \t}趕上(NumberFormatException的前){ \t \t \t \t的System.out.println( 「不正確的得分數據,請輸入有效的整數」); \t \t \t \t}' 我在這裏加入,但它似乎每次都乘以2的用戶輸入? @AndyTurner –

回答

0

我不知道爲什麼你正在使用一個for循環。您應該刪除的for循環和閱讀成績的

import java.util.Scanner; 

public class REQ3 { 
    public static void main(String[] args) { 

     String playername; 
     String line; 
     String[] list = new String[100]; 
     int count = 0; 

     int time; 
     int gamesplayed = 0; 
     int totalScore = 0; 

     Scanner sc = new Scanner(System.in); 

     System.out.println("Please enter your name"); 

     playername = sc.nextLine(); 

     if (playername.equals("")) { 
      System.out.println("Player name was not entered please try again"); 
      System.exit(0); 
     } 

     System.out.println("Please enter your game achivements (Game name:score:time played) E.g. Minecraft:14:2332"); 

     while (count < 100) { 

      line = sc.nextLine(); 

      if (line.equals("quit")) { 
       break; 
      } 
      if (line.equals("")) { 
       System.out.println("Nothing was entered please try again"); 
       break; 
      } 

      if (!(line.contains(":"))) { 
       System.out.println("Please enter achivements with the proper \":\" sepration\n"); 
       break; 
      } 

      list[count] = line; 
      System.out.println("list[count]" + list[count]); 

      count++; 
      gamesplayed++; 

      String[] elements = line.split(":"); 

      if (elements.length != 3) { 
       System.out.println(
         "Error please try again, Please enter in the following format:\nGame name:score:timeplayed"); 
       break; 
      } 

      try { 
       int score = Integer.parseInt(elements[1].trim()); 
       totalScore += score; 
      } catch (NumberFormatException ex) { 
       System.out.println("Incorrect score data, Please enter a valid integer"); 
      } 
      try { 
       time = Integer.parseInt(elements[2].trim()); 
      } catch (NumberFormatException ex) { 
       System.out.println("Incorrect time data, Please enter a valid integer"); 
      } 

     } 
     System.out.println("Player : " + playername); 
     System.out.println("--------------------------------"); 
     System.out.println("Games Played: " + gamesplayed); 
     System.out.println("total score: " + totalScore); 
    } 
} 
+0

我正在使用一個循環,因此用戶可以爲while循環添加多個遊戲@Awsome –

+0

。然後在for循環裏面。我沒有看到任何用於內循環的用法 – awsome

0

與真棒不知道爲什麼你需要內部for循環同意後做totalScore。下面是針對無效輸入項總得分都適用的解決方案。

import java.util.Scanner; 

public class REQ3 { 

    public static void main(String[] args) { 

     String playername; 
     String line; 
     String[] list = new String[100]; 
     int count = 0; 
     int score = 0; 
     int time; 
     int gamesplayed = 0; 
     int totalScore = 0; 
     int invalidEntries = 0; 

     Scanner sc = new Scanner(System.in); 

     System.out.println("Please enter your name"); 

     playername = sc.nextLine(); 

     if (playername.equals("")) { 
      System.out.println("Player name was not entered please try again"); 
      System.exit(0); 
     } 

     System.out 
       .println("Please enter your game achivements (Game name:score:time played) E.g. Minecraft:14:2332"); 

     while (count < 100) { 

      line = sc.nextLine(); 

      if (line.equals("quit")) { 
       break; 
      } 
      if (line.equals("")) { 
       System.out.println("Nothing was entered please try again"); 
       invalidEntries++; 
       continue; 
      } 

      if (!(line.contains(":"))) { 
       System.out 
         .println("Please enter achivements with the proper \":\" sepration\n"); 
       invalidEntries++; 
       continue; 
      } 

      list[count] = line; 
      System.out.println("list[count]" + list[count]); 
      String[] elements = line.split(":"); 

      if (elements.length != 3) { 
       System.out 
         .println("Error please try again, Please enter in the following format:\nGame name:score:timeplayed"); 
       invalidEntries++; 
       continue; 
      } 

      try { 
       score = Integer.parseInt(elements[1].trim()); 
      } catch (NumberFormatException ex) { 
       System.out 
         .println("Incorrect score data, Please enter a valid integer"); 
       invalidEntries++; 
       continue; 
      } 
      try { 
       time = Integer.parseInt(elements[2].trim()); 
      } catch (NumberFormatException ex) { 
       System.out 
         .println("Incorrect time data, Please enter a valid integer"); 
       invalidEntries++; 
       continue; 
      } 
      count++; 
      gamesplayed++; 
      totalScore = totalScore + score; 
     } 
     System.out.println("Player : " + playername); 
     System.out.println("--------------------------------"); 
     System.out.println("Games Played: " + gamesplayed); 
     System.out.println("Total Score: " + totalScore); 
     System.out.println("Invalid Entries: " + invalidEntries); 
    } 
}