所以我的代码正在努力告诉我平均成绩是多少,但是,我需要它实际做的是告诉我每个等级本身的成绩是什么,例如用户输入test1 80,I需要它说等级是B等等。这就是我到目前为止所拥有的。我不确定我是否可以将多个变量放入1 if else语句中,或者如果Ill必须有5个不同的if语句。
import java.util.Scanner; //user input
import java.text.DecimalFormat; //formats decimal places
/*This program is made to recieve 5 test grade inputs from the
user and then calculate their average and return their grade.
*/
public class Lab
{
public static void main(String[] args)
{
//place holders for test scores
double test1,test2,test3,test4,test5;
System.out.println("Enter 5 of your test grades: ");
//create scanner for user imput
Scanner keyboard = new Scanner(System.in);
test1 = keyboard.nextDouble();
test2 = keyboard.nextDouble();
test3 = keyboard.nextDouble();
test4 = keyboard.nextDouble();
test5 = keyboard.nextDouble();
keyboard.nextLine();
//calls method 1
double average = calcAverage(test1, test2, test3, test4, test5);
//calls method 2
getGrade(average);
//formats how the average will be displayed
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("The average is: " + df.format(average));
System.exit(0);
}
/*
The calcAverage method takes the test inputs from the
user and divides them by 5 getting the average of all 5.
*/
public static double calcAverage(double test1, double test2, double test3, double test4, double test5)
{
double average = (test1 + test2 + test3 + test4 + test5) / 5;
return average;
}
/*
The getGrade method will take what was produced in the calcAverage method
and find out where the average falls, thus giving the user his/her grade.
*/
public static void getGrade(double average)
{
if (average>90)
{
System.out.println("You have an A");
}
else if (average>=80)
{
System.out.println("You have a B");
}
else if (average>=70)
{
System.out.println("You have a C");
}
else if (average>=60)
{
System.out.println("You have a D");
}
else if (average<60)
{
System.out.println("You have a F");
}
}
}
答案 0 :(得分:1)
您的getGrade()
应该可以返回成绩。像,
public static String getGrade(double average)
{
if (average>90)
{
return "A";
}
else if (average>=80)
{
return "B";
}
else if (average>=70)
{
return "C";
}
else if (average>=60)
{
return "D";
}
else
{
return "F";
}
}
然后你将其称为单独测试(或平均值) -
System.out.printf("You got a %s on test1.", getGrade(test1));
答案 1 :(得分:0)
只需针对各个测试调用getGrade
,如下所示,并将getGrade
参数名称重命名为score
而不是average
(这只是为了提高可读性)
import java.util.Scanner; //user input
import java.text.DecimalFormat; //formats decimal places
/*This program is made to recieve 5 test grade inputs from the
user and then calculate their average and return their grade.
*/
public class Lab
{
public static void main(String[] args)
{
//place holders for test scores
double test1,test2,test3,test4,test5;
System.out.println("Enter 5 of your test grades: ");
//create scanner for user imput
Scanner keyboard = new Scanner(System.in);
test1 = keyboard.nextDouble();
test2 = keyboard.nextDouble();
test3 = keyboard.nextDouble();
test4 = keyboard.nextDouble();
test5 = keyboard.nextDouble();
keyboard.nextLine();
//calls method 1
double average = calcAverage(test1, test2, test3, test4, test5);
//calls method 2
getGrade(average);
// Gets grade for individual tests
getGrade(test1);
getGrade(test2);
getGrade(test3);
getGrade(test4);
getGrade(test5);
//formats how the average will be displayed
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("The average is: " + df.format(average));
System.exit(0);
}
/*
The calcAverage method takes the test inputs from the
user and divides them by 5 getting the average of all 5.
*/
public static double calcAverage(double test1, double test2, double test3, double test4, double test5)
{
double average = (test1 + test2 + test3 + test4 + test5) / 5;
return average;
}
/*
The getGrade method will take what was produced in the calcAverage method
and find out where the average falls, thus giving the user his/her grade.
*/
public static void getGrade(double score)
{
if (average>90)
{
System.out.println("You have an A");
}
else if (average>=80)
{
System.out.println("You have a B");
}
else if (average>=70)
{
System.out.println("You have a C");
}
else if (average>=60)
{
System.out.println("You have a D");
}
else if (average<60)
{
System.out.println("You have a F");
}
}
}
答案 2 :(得分:0)
我会将您的测试放入Array
:
double[] myTests = {test1, test2, test3, test4, test5};
然后迭代:
for(int i=0; i<myTest.length; i++){
getGrade(myTests[i]);
}
至于您的getGrade
方法,您可能只想为每个年级返回char
:A,B,C,等等,然后只有一个System.out.print
语句。在这种情况下,我个人的偏好是使用switch-case
代替if-else
。显然这是学校的工作,所以我不知道你希望用什么教育水平编码
答案 3 :(得分:0)
对于更多OO设计,您可以创建一个Test
类并将数据封装在其中:
public class Test
{
public double Score;
public String GetGrade()
{
if (this.Score > 90)
{
return "A";
}
else if (this.Score >= 80)
{
return "B";
}
else if (this.Score >=70)
{
return "C";
}
else if (this.Score >= 60)
{
return "D";
}
else
{
return "F";
}
}
}
我假设您的问题包含接受5个测试分数的说明,因此我们可以使用大小为5的数组(否则您可以创建ArrayList
并为每个输入添加它):
Test[] tests = new Test[5];
不是连续接受5个输入,更好的解决方案可能是在输入每个分数时打印出成绩:
System.out.println("Enter test scores:");
//create scanner for user imput
Scanner keyboard = new Scanner(System.in);
for (int i = 0; i < tests.length; i++)
{
System.out.printf("Test %d: ", i + 1);
tests[i].Score = keyboard.nextDouble();
System.out.printf("Grade is %s", tests[i].GetGrade());
}
您的calcAverage也可以更通用,而不是硬编码5:
public static double calcAverage(Test[] tests)
{
double scoreSum;
for (int i = 0; i < tests.length; i++)
{
scoreSum += tests[i].Score;
}
return scoreSum / test.length;
}