私はプログラミングを始めたばかりで、各 do while ループの成績をインクリメントまたは保存するカウンターを取得できません。このプログラムは、宿題の成績を取り込んで平均し、次に小テストの成績を取り込んで平均し、テストも同じようにして、最後に 3 つの平均をまとめて平均すると想定されています。
import java.util.Scanner;
public class Homework3 {
public static void main ( String[] args ) {
Scanner input = new Scanner( System.in );
int number1;
int totalhomework = 0;
int counterhomework = 0;
double averagehomework = 0;
String name;
int homeworkgrade = 0;
int totalquiz = 0;
int counterquiz = 0;
double averagequiz = 0;
int quizgrade = 0;
int totaltest = 0;
int countertest = 0;
double averagetest = 0;
int testgrade = 0;
double finalaverage = 0;
boolean run = true;
do {
System.out.print("\nEnter 1 to average grades for new student");
System.out.print("\n Enter 2 to quit");
number1 = input.nextInt();
if (number1 == 1) {
System.out.println("\nWhat is the student's name?");
name = input.next();
}
if (number1 == 2) {
System.out.print("\nThank you");
System.exit(1);
}
counterhomework = 0;
totalhomework = 0;
do {
counterhomework++;
totalhomework = totalhomework + homeworkgrade;
System.out.println("\nFirst enter Homework grades then type " +
"-1 to move onto next category"
);
homeworkgrade = input.nextInt();
} while(homeworkgrade != -1);
if (homeworkgrade == -1) {
System.out.print("\nYour Homework average is:" +
averagehomework
);
averagehomework = totalhomework/counterhomework;
}
do {
counterquiz = counterquiz +1;
System.out.print("\nNext enter quiz grades then type -1 " +
"to move onto next category"
);
totalquiz = totalquiz + quizgrade;
quizgrade = input.nextInt();
} while (quizgrade != -1);
if (quizgrade ==-1) {
System.out.print("\nYour quiz average is:" + averagequiz);
averagequiz = totalquiz/counterquiz;
}
do {
countertest = countertest +1;
System.out.print("\nFinally enter test grades then type " +
"-1 when finished"
);
totaltest = totaltest + testgrade;
testgrade = input.nextInt();
} while (testgrade != -1);
if (testgrade == -1) {
System.out.print("\nYour test average is:" +averagetest);
averagetest = totaltest/countertest;
}
System.out.print(name);
System.out.print("final average is:" + finalaverage);
finalaverage = 0.25 * averagehomework + 0.25 * averagequiz +
0.50 * averagetest;
} while (number1 != 2);
} //main
} //class