如何在条件(如果/则)语句中包含范围? [英] How can I include range in a conditional (if/then) statement?

查看:88
本文介绍了如何在条件(如果/则)语句中包含范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java编写一个程序,当所有季度的分数以及期中和期末的分数都在其中时返回字母分数。到目前为止,这是这样的:

I'm trying to write a program in Java that returns a letter grade when the grades for all quarters, as well as the grades for the midterms and finals are in. So far this is what it looks like:

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

}

我想在代码的最后一个if语句中显示90到94的范围。建议我使用Math.random作为命令,但是我不知道要写什么方程式,这样它才能在我提到的范围内工作。任何帮助将非常感激。

I want to show a range of 90 to 94 in the last else if statement in the code. I was recommended I use Math.random as a command, but I don't know what equation to write so that it works within the range I mentioned. Any help would be much appreciated. Thanks in advance.

推荐答案

因为您已经在测试 c> = 95 在第一条语句中,您只需要检查下限:

Since you are already testing c >= 95 in the first statement, you need only check the lower bound:

if(c >= 95) { /* A+ */ }
else if(c >= 90) { /* A */ }
else if(c >= 85) { /* A- */ }
...

这篇关于如何在条件(如果/则)语句中包含范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆