我写了一些代码,允许我为用户玩过的游戏获取用户输入。我已经完成了大部分工作,但是我已经到了一个我不知道在哪里添加此代码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);
}
}
答案 0 :(得分:0)
我不知道你为什么使用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);
}
}
答案 1 :(得分:0)
同意很棒,不确定为什么需要内部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);
}
}