簡體   English   中英

不確定在哪里添加某些代碼

[英]Unsure of where to add certain piece of code

我編寫了一些代碼,使我可以獲取用戶玩過的游戲的用戶輸入。 我已經完成了大部分工作,但是到了我不知道在何處添加此代碼的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);
}         

}

我不知道你為什么要使用for循環。 閱讀分數后,應刪除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 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);
    }
}

同意真棒不知道為什么需要內部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 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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM