java - 如何在条件 (if/then) 语句中包含范围?

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

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

最佳答案

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

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

关于java - 如何在条件 (if/then) 语句中包含范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14615802/

相关文章:

c++ - 如果目标对象死亡,Qt::BlockingQueuedConnection emission 会发生什么?

javascript - 使用节点、startOffset 和 endOffset 重建范围

c - while循环输出多个条件

java - 使用 AsyncTask 和重启方法

java - WebLogic 集成的替代方案

java - 为什么 Range 会抛出 UnsupportedOperationException?

haskell - 为什么在 Haskell 中使用范围时 map 会返回一个附加元素?

amazon-web-services - Lambda API 结果作为在 Cloud Formation 中创建 AWS 资源的条件

java - 从 Eclipse 运行 Maven 应用程序的所有 Servlet 上的 ClassNotFoundException

java - 为什么我的返回声明没有被识别?