我必须在程序中添加一个计数器,以计算已处理的组,有效组和无效组的总数。同样,如果处理的数字之一无效> 300或<0,则程序主程序应跳过对其余分数的处理,然后转到嵌套组。我添加了一个带有-1标记的while循环来创建迭代。现在循环播放,但是计数器无法正常运行。而且我仍然不确定如何计算无效组和有效组
import java.util.Scanner;
import java.io.*;
public class BowlingProgram {
public static void main(String[] args) throws IOException {
//instantiate objects
//PrintWriter outputFile= new PrintWriter(System.out);
File myFile=new File ("bowlingScores.txt");
int score1, score2, score3, notValid=0, valid=0, count=0;
Scanner scanner = new Scanner(System.in);
while(count!=-1) {
System.out.print("Please enter score 1 (enter -1 to STOP): ");
score1 = scanner.nextInt();
System.out.print("Please enter score 2 (enter -1 to STOP): ");
score2 = scanner.nextInt();
System.out.print("Please enter score 3 (enter -1 to STOP): ");
score3 = scanner.nextInt();
count++;
validGroup(score1, score2, score3, myFile);
oneGameScore(score1,myFile);
oneGameScore(score2,myFile);
oneGameScore(score3,myFile);
/*while(validGroup(score1,score2,score3,myFile)!=true) {
notValid++;
}
System.out.println("we have processed"+notValid+"invalid scores");
while(validGroup(score1,score2,score3,myFile)==true) {
valid++;
}
System.out.println("we have processed"+valid+"invalid scores");
*/
System.out.println("The average score is " + avg3Scores(score1,score2, score3));
oneGameScore(avg3Scores(score1,score2,score3), myFile);
System.out.println();
System.out.println();
System.out.println();
}
System.out.println("You have processed "+count+" groups.");
public static boolean validGroup(int score1, int score2, int score3, File myFile)
{
if(score1 >300) {
System.out.println("The number you entered for score1 is greater than 300");
return false;
}
if(score1 <0) {
System.out.println("The number you entered for score1 is less than 0");
return false;
}
if(score2 >300) {
System.out.println("The number you entered for score2 is greater than 300");
return false;
}
if(score2 <0) {
System.out.println("The number you entered for score2 is less than 0");
return false;
}
if(score3 >300) {
System.out.println("The number you entered for score3 is greater than 300");
return false;
}
if(score3 <0) {
System.out.println("The number you entered for score3 is less than 0");
return false;
}
System.out.println("This group is valid");
return true;
}
public static void oneGameScore(int score, File myfile) {
if(score <=300 && score >=250)
System.out.println("You've played a proffesional game!");
if(score <=249 && score >=200)
System.out.println("You've played an excellent game!");
if(score <=199 && score >=150)
System.out.println("You've played a very good game!");
if(score <=149 && score >=100)
System.out.println("You've played a good game!");
if(score <=99 && score >=50)
System.out.println("You've played a poor game!");
if(score <=49)
System.out.println("You've played a horrible game");
}
public static int avg3Scores(int sc_1, int sc_2,int sc_3)
{
int avg= (sc_1+sc_2+sc_3)/3;
return avg;
}
}