我终于编写了我的程序(花了一段时间,因为我是初学者),程序的作用是:
使用2个选项打印菜单,平均成绩或退出。
如果用户选择平均成绩,则会询问他们的姓名。然后它会告诉他们成绩的平均顺序。
然后他们输入任意数量的成绩,然后按-1,它将为您提供这些成绩和平均成绩的总和。它做了3次,用于家庭作业,测验和测试。
我需要做的最后一件事是平均所有3个等级,然后输出:
System.out.println( name, "'s average is ", + averageFinal );
我从作业中获得了这一行:
Final Average = 0.25*HomeworkAvg + 0.25*QuizAvg + 0.50*TestAvg = 77.87
我尝试将该程序放入:
averageFinal = 0.25*averageOne + 0.25*averageTwo + 0.50*averageThree;
但由于某种原因,我得到了这个奇怪的编译错误,我似乎无法修复:
Assignment3.java:120: error: cannot find symbol
System.println( name, "'s final average is ", averageFinal );
^
symbol: method println(String,String,double)
location: class System
1 error
我的代码只是一个简单的问题,还是我必须在我的字符串方法中更改某些内容?
import java.util.Scanner;
public class Assignment3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
int total;
double averageOne;
double averageTwo;
double averageThree;
double averageFinal;
int grade;
int gradeCounterone;
int gradeCountertwo;
int gradeCounterthree;
gradeCounterone = 0;
gradeCountertwo = 0;
gradeCounterthree = 0;
total = 0;
String name;
System.out.println("Enter 1 or 2: \n 1 - Average grades \n 2 - Quit");
choice = input.nextInt();
if (choice == 1) {
System.out.println("Enter the students name:");
name = input.next();
System.out.println("Grades will be entered in this order: \n 1) Homework Grades \n 2) Quiz Grades \n 3) Test Grades ");
{
System.out.println("Enter homework grades, press -1 when finished:");
grade = input.nextInt();
while (grade != -1) {
total = total + grade;
gradeCounterone = gradeCounterone + 1;
System.out.println("Enter homework grades, press -1 when finished:");
grade = input.nextInt();
}
if (gradeCounterone != 0) {
averageOne = (double) total / gradeCounterone;
System.out.printf("\nThe total of the %d homework grades is %d\n", gradeCounterone, total);
System.out.printf("\nThe average of the homework grades is %.2f\n ", averageOne);
}
// second loop for quiz grades
System.out.println("\nEnter quiz grades, press -1 when finished:");
grade = input.nextInt();
while (grade != -1) {
total = total + grade;
gradeCountertwo = gradeCountertwo + 1;
System.out.println("Enter quiz grades, press -1 when finished:");
grade = input.nextInt();
}
if (gradeCountertwo != 0) {
averageTwo = (double) total / gradeCountertwo;
System.out.printf("\nThe total of the %d quiz grades is %d\n", gradeCountertwo, total);
System.out.printf("\nThe average of the quiz grades is %.2f\n ", averageTwo);
}
// third loop for test grades
System.out.println("\nEnter test grades, press -1 when finished:");
grade = input.nextInt();
while (grade != -1) {
total = total + grade;
gradeCounterthree = gradeCounterthree + 1;
System.out.println("Enter test grades, press -1 when finished:");
grade = input.nextInt();
}
if (gradeCounterthree != 0) {
averageThree = (double) total / gradeCounterthree;
System.out.printf("\nThe total of the %d test grades is %d\n", gradeCounterthree, total);
System.out.printf("\nThe average of the test grades is %.2f\n ", averageThree);
}
averageFinal = 0.25 * averageOne + 0.25 * averageTwo + 0.50 * averageThree;
System.println(name, "'s final average is ", averageFinal);
}
} else if (choice == 2) {
System.out.println("Exiting program.");
} else {
System.out.println("Invalid response, exiting program.");
}
}
}
答案 0 :(得分:1)
应该是:
System.out.println( name + " s final average is " + averageFinal );
而不是
System.println( name, "' s final average is ", averageFinal );
答案 1 :(得分:0)
而不是
System.println( name, "'s final average is ", averageFinal );
试
System.out.println( name + "'s final average is " + averageFinal );
您也可以从JDK文档中获取此信息。
答案 2 :(得分:0)
答案 3 :(得分:0)
这里有几个问题。
尽管显而易见:它是System.out.println
。并且,您将String
与+
:
System.out.println(name + "'s final average is " + averageFinal);
现在,在您修复该代码之后,您会得到这些变量未被初始化的问题:
averageFinal = 0.25 * averageOne + 0.25 * averageTwo + 0.50 * averageThree;
通过程序的自然流程,averageOne
,averageTwo
和averageThree
都无法保证初始化 - 似乎它们正在某些if
后面进行初始化语句。
将它们初始化为零是直截了当的,以防你没有点击任何if
语句:
double averageOne = 0;
double averageTwo = 0;
double averageThree = 0;