我是编程的新手,我无法让计数器增加或存储每个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
答案 0 :(得分:2)
我猜您的问题是averagehomework= 0.0
,这是因为您在计算之前打印averagehomework
,所以请点击它:
averagehomework = totalhomework / counterhomework;
System.out.print("\nYour Homework average is:" + averagehomework);
你需要初始化name变量,如下所示:
String name = "";
counterhomework
需要按-1