Java 如何在条件(if/then)语句中包含范围?
我正试图用Java编写一个程序,当所有季度的成绩以及期中考试和期末考试的成绩都在时,它会返回一个字母级。到目前为止,它看起来是这样的:Java 如何在条件(if/then)语句中包含范围?,java,range,conditional-statements,Java,Range,Conditional Statements,我正试图用Java编写一个程序,当所有季度的成绩以及期中考试和期末考试的成绩都在时,它会返回一个字母级。到目前为止,它看起来是这样的: public static void main (String args[]) { System.out.println("To figure out final grade answer these questions. Use only numbers, and include decimal points where applicable");
public static void main (String args[])
{
System.out.println("To figure out final grade answer these questions. Use only numbers, and include decimal points where applicable");
Scanner g = new Scanner(System.in);
System.out.println("What was your quarter one grade?");
int o = g.nextInt();
System.out.println("What was your quarter two grade?");
int t = g.nextInt();
System.out.println("What was your quarter three grade?");
int h = g.nextInt();
System.out.println("What was your quarter four grade?");
int r = g.nextInt();
System.out.println("What was your grade on the midterm?");
int m = g.nextInt();
System.out.println("What was your grade on the final?");
int f = g.nextInt();
double c = 0.2 * o + 0.2 * t + 0.2 * h + 0.2 * r + 0.1 * m + 0.1 *f;
if(c >= 95)
{
System.out.println("A+");
}
else if(c = ?)
{
System.out.println("A");
}
}
}
我想在代码的最后一个elseif语句中显示90到94的范围。有人建议我使用Math.random作为命令,但我不知道要写什么等式,以便它在我提到的范围内工作。任何帮助都将不胜感激。提前感谢。因为您已经在第一条语句中测试了
c>=95
,所以只需检查下限:
if(c >= 95) { /* A+ */ }
else if(c >= 90) { /* A */ }
else if(c >= 85) { /* A- */ }
...
由于您已经在第一条语句中测试了
c>=95
,因此只需检查下限:
if(c >= 95) { /* A+ */ }
else if(c >= 90) { /* A */ }
else if(c >= 85) { /* A- */ }
...
如果(c>=95)
{
System.out.println(“A+”);
}
else if(c>=90&&cif(c>=95)
{
System.out.println(“A+”);
}
否则,如果(c>=90&&c这里有一个稍微不同的方法来动态生成等级
private static final String[] constants = {"F","D","C","B","A"};
public String getGrade(float score) {
if(score < 0)
throw new IllegalArgumentException(Float.toString(score));
if((int)score <= 59)
return constants[0];
if((int)score >= 100)
return constants[4];
int res = (int) (score/10.0);
return constants[res-5];
}
private static final String[]常量={“F”、“D”、“C”、“B”、“A”};
公共字符串getGrade(浮动分数){
如果(分数<0)
抛出新的IllegalArgumentException(Float.toString(score));
如果((整数)分数=100)
返回常数[4];
int res=(int)(分数/10.0);
返回常数[res-5];
}
这里有一种稍微不同的方法来动态生成等级
private static final String[] constants = {"F","D","C","B","A"};
public String getGrade(float score) {
if(score < 0)
throw new IllegalArgumentException(Float.toString(score));
if((int)score <= 59)
return constants[0];
if((int)score >= 100)
return constants[4];
int res = (int) (score/10.0);
return constants[res-5];
}
private static final String[]常量={“F”、“D”、“C”、“B”、“A”};
公共字符串getGrade(浮动分数){
如果(分数<0)
抛出新的IllegalArgumentException(Float.toString(score));
如果((整数)分数=100)
返回常数[4];
int res=(int)(分数/10.0);
返回常数[res-5];
}
如果(c>=90&&c如果(c>=90&&c)因为c是双精度的,你应该用c<95来避免某些东西漏掉(例如,把94.5变成F)。因为c是双精度的,你应该用c<95来避免某些东西漏掉(例如,把94.5变成F)。