否则,如果语句

问题描述:

所以我有我的代码工作,告诉我平均成绩是什么,但是,我需要它实际做的是告诉我什么是每个等级本身的等级,例如用户输入test1 80,我需要说的是等级是B等等。这是我迄今为止所拥有的。林不知道我是否甚至可以把多个变量放入1 if else语句中,或者如果我必须有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

你能包括确切的输出类型吗? EG“如果我用例82提供它,我希望它输出'例子的等级(82)是B'”。 – Pokechu22 2014-10-28 23:55:12

+0

而不是硬编码'test {1-5}',考虑使用循环来按顺序处理每个分数。 – genisage 2014-10-28 23:55:38

+0

你已经学过数组和循环了吗? – 2014-10-28 23:59:43

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)); 
+0

@ T.Woody来自OP的帖子。也许91-100是A. – 2014-10-29 00:00:31

+0

想我会问:)谢谢你的澄清! – 2014-10-29 00:01:27

+0

谢谢我玩过你在这里给我的东西,并能够得到它的工作!非常感谢:) – 2014-10-29 00:24:57

只需拨打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"); 
     } 
    } 
} 
+0

感谢您的帮助! – 2014-10-29 00:29:02

+0

不用客气! – Chiseled 2014-10-29 00:37:53

我会把你的测试到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。显然,这是学校的工作,虽然,所以我不知道你是有望与

+0

你有一个错字。使用'length' **属性**而不是'size()'**方法**更常见。 – 2014-10-29 00:01:49

+0

良好的通话,这是一段时间,因为我已经使用阵列;) – 2014-10-29 00:04:49

+0

我还没有学习数组,但谢谢你的回复! – 2014-10-29 00:27:52

对于一些更多的面向对象设计的代码,你可以创建一个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输入S IN一排,一个更好的解决方案可能是打印出来的品位为每输入一个分数:

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; 
}