如何在Java中将双精度用户输入的值四舍五入?

huangapple go评论105阅读模式
英文:

how do you round up a double user input value in java?

问题

我是一名正在制作简单评分系统的学生,我在如何做这个方面遇到了困难。

当我输入特定的数字时,它会跳过我的else-if语句,而进入else语句。
这些数字分别是98、67、98、80和81,我不知道为什么会这样。

以下是我的代码:

  1. public static void main(String[] args) {
  2. double grade = 0, tgrade = 0, r;
  3. int gcount = 0;
  4. for (int i = 0; i < 5; i++) {
  5. grade = Integer.parseInt(JOptionPane.showInputDialog(null, "输入成绩:"));
  6. tgrade = tgrade + grade;
  7. gcount++;
  8. System.out.println(gcount + ". " + "成绩:" + grade);
  9. }
  10. DecimalFormat c = new DecimalFormat("##.##");
  11. tgrade = tgrade / 500 * 100;
  12. r = new Double(c.format(tgrade)).doubleValue();
  13. System.out.print("总成绩:" + (tgrade) + "\n");
  14. if (r >= 95 && r <= 100) {
  15. JOptionPane.showMessageDialog(null, "高荣誉");
  16. } else if (r >= 90 && r <= 94) {
  17. JOptionPane.showMessageDialog(null, "荣誉");
  18. } else if (r >= 85 && r <= 89) {
  19. JOptionPane.showMessageDialog(null, "良好");
  20. } else if (r >= 80 && r <= 84) {
  21. JOptionPane.showMessageDialog(null, "合格");
  22. } else if (r >= 75 && r <= 79) {
  23. JOptionPane.showMessageDialog(null, "及格,但合格");
  24. } else {
  25. JOptionPane.showMessageDialog(null, "低分");
  26. }
  27. }
英文:

Im a student whos making a simple grading system and Im struggling on how to do this

when I type in a specific number it go down to else bypassing my else-if statements
the numbers are 98, 67, 98, 80 and 81 and i dont know why this happens

Here's my code:

  1. public static void main(String[]args) {
  2. double grade=0,tgrade=0,r;
  3. int gcount=0;
  4. for (int i = 0; i&lt;5;i++) {
  5. grade = Integer.parseInt(JOptionPane.showInputDialog(null, &quot;Enter the grades &quot;));
  6. tgrade = tgrade+grade;
  7. gcount++;
  8. System.out.println(gcount+&quot;. &quot;+&quot;grade: &quot;+grade );
  9. }
  10. DecimalFormat c = new DecimalFormat(&quot;##.##&quot;);
  11. tgrade = tgrade/500*100;
  12. r = new Double(c.format(tgrade)).doubleValue();
  13. System.out.print(&quot;Total Grade: &quot;+(tgrade)+&quot;\n&quot;);
  14. if (r &gt;= 95 &amp;&amp; r &lt;=100) {
  15. JOptionPane.showMessageDialog(null, &quot;High Honor&quot;);
  16. }else if (r &gt;= 90 &amp;&amp; r &lt;= 94){
  17. JOptionPane.showMessageDialog(null, &quot;Honor&quot;);
  18. }else if (r &gt;=85 &amp;&amp; r &lt;= 89) {
  19. JOptionPane.showMessageDialog(null, &quot;Good&quot;);
  20. }else if (r&gt;=80 &amp;&amp; r&lt;=84) {
  21. JOptionPane.showMessageDialog(null, &quot;Satisfactory&quot;);
  22. }else if (r&gt;=75 &amp;&amp; r&lt;= 79) {
  23. JOptionPane.showMessageDialog(null, &quot;Low pass, but certifying&quot;);
  24. }else {
  25. JOptionPane.showMessageDialog(null, &quot;Low Failure&quot;);
  26. }
  27. }

}

答案1

得分: 1

变量 r 是双精度浮点数。

例如:

你有这些数字:98、67、98、80 和 81
平均值是:424/5 = 84.8

这个值 84.8 不符合你写的条件:

如果 (r >= 85 && r <= 89)
如果 (r >= 80 && r <= 84)

因此它不满足条件。

你可以使用以下选项。

第一选项:不要使用以下范围:

如果 (r >= 95) {
JOptionPane.showMessageDialog(null, "High Honor");
}else if (r >= 90){
JOptionPane.showMessageDialog(null, "Honor");
}else if (r >= 85){
JOptionPane.showMessageDialog(null, "Good");
}else if (r >= 80){
JOptionPane.showMessageDialog(null, "Satisfactory");
}else if (r >= 75){
JOptionPane.showMessageDialog(null, "Low pass, but certifying");
}else {
JOptionPane.showMessageDialog(null, "Low Failure");
}

第二选项:如果你在使用范围,请像以下方式构建条件:

如果 (r >= 95 && r < 100) {
JOptionPane.showMessageDialog(null, "High Honor");
}else if (r >= 90 && r < 95){
JOptionPane.showMessageDialog(null, "Honor");
}else if (r >= 85 && r < 90) {
JOptionPane.showMessageDialog(null, "Good");
}else if (r >= 80 && r < 85) {
JOptionPane.showMessageDialog(null, "Satisfactory");
}else if (r >= 75 && r < 80) {
JOptionPane.showMessageDialog(null, "Low pass, but certifying");
}else {
JOptionPane.showMessageDialog(null, "Low Failure");
}

第三选项:如果你需要四舍五入,可以使用 Math.abs 来从 double r 中提取绝对值。

示例:

Math.abs(r)

英文:

You variable r is double.

For example :

  1. You have these numbers : 98, 67, 98, 80 and 81
  2. The average is : 424/5 = 84.8

This value 84.8 doesnot fit both the condition you wrote :

  1. if (r &gt;=85 &amp;&amp; r &lt;= 89)
  2. if (r&gt;=80 &amp;&amp; r&lt;=84)

Hence it is going out.

You can use the below options.

FIRST Option : Don't use the range as below :

  1. if (r &gt;= 95) {
  2. JOptionPane.showMessageDialog(null, &quot;High Honor&quot;);
  3. }else if (r &gt;= 90){
  4. JOptionPane.showMessageDialog(null, &quot;Honor&quot;);
  5. }else if (r &gt;= 85){
  6. JOptionPane.showMessageDialog(null, &quot;Good&quot;);
  7. }else if (r &gt;= 80){
  8. JOptionPane.showMessageDialog(null, &quot;Satisfactory&quot;);
  9. }else if (r &gt;= 75){
  10. JOptionPane.showMessageDialog(null, &quot;Low pass, but certifying&quot;);
  11. }else {
  12. JOptionPane.showMessageDialog(null, &quot;Low Failure&quot;);
  13. }

SECOND Option : If you are using range then frame the conditions like below :

  1. if (r &gt;= 95 &amp;&amp; r &lt;= 100) {
  2. JOptionPane.showMessageDialog(null, &quot;High Honor&quot;);
  3. }else if (r &gt;= 90 &amp;&amp; r &lt; 95){
  4. JOptionPane.showMessageDialog(null, &quot;Honor&quot;);
  5. }else if (r &gt;= 85 &amp;&amp; r &lt; 90) {
  6. JOptionPane.showMessageDialog(null, &quot;Good&quot;);
  7. }else if (r &gt;= 80 &amp;&amp; r &lt; 85) {
  8. JOptionPane.showMessageDialog(null, &quot;Satisfactory&quot;);
  9. }else if (r &gt;= 75 &amp;&amp; r &lt; 80) {
  10. JOptionPane.showMessageDialog(null, &quot;Low pass, but certifying&quot;);
  11. }else {
  12. JOptionPane.showMessageDialog(null, &quot;Low Failure&quot;);
  13. }

THIRD Option : If you need to round up then you can use Math.abs to extract the absolute value from the double r

Example :

  1. Math.abs(r)

答案2

得分: -1

else if (r>=80 && r<=84)更改为else if (r>=80 && r<=84),然后它就会起作用。

英文:

changeelse if (r&gt;=80 &amp;&amp; r&lt;=84) to else if (r&gt;=80 &amp;&amp; r&lt;=84) and it will work

huangapple
  • 本文由 发表于 2020年8月25日 20:39:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63579052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定