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

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

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

问题

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

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

以下是我的代码:

public static void main(String[] args) {

    double grade = 0, tgrade = 0, r;
    int gcount = 0;

    for (int i = 0; i < 5; i++) {
        grade = Integer.parseInt(JOptionPane.showInputDialog(null, "输入成绩:"));
        tgrade = tgrade + grade;
        gcount++;
        System.out.println(gcount + ". " + "成绩:" + grade);
    }

    DecimalFormat c = new DecimalFormat("##.##");

    tgrade = tgrade / 500 * 100;
    r = new Double(c.format(tgrade)).doubleValue();

    System.out.print("总成绩:" + (tgrade) + "\n");
    if (r >= 95 && r <= 100) {
        JOptionPane.showMessageDialog(null, "高荣誉");
    } else if (r >= 90 && r <= 94) {
        JOptionPane.showMessageDialog(null, "荣誉");
    } else if (r >= 85 && r <= 89) {
        JOptionPane.showMessageDialog(null, "良好");
    } else if (r >= 80 && r <= 84) {
        JOptionPane.showMessageDialog(null, "合格");
    } else if (r >= 75 && r <= 79) {
        JOptionPane.showMessageDialog(null, "及格,但合格");
    } else {
        JOptionPane.showMessageDialog(null, "低分");
    }
}
英文:

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:

public static void main(String[]args) {
double grade=0,tgrade=0,r;
int gcount=0;
for (int i = 0; i&lt;5;i++) {
grade	= Integer.parseInt(JOptionPane.showInputDialog(null, &quot;Enter the grades &quot;));
tgrade = tgrade+grade;
gcount++;
System.out.println(gcount+&quot;. &quot;+&quot;grade: &quot;+grade );
}
DecimalFormat c = new DecimalFormat(&quot;##.##&quot;);
tgrade = tgrade/500*100;
r = new Double(c.format(tgrade)).doubleValue();
System.out.print(&quot;Total Grade: &quot;+(tgrade)+&quot;\n&quot;);
if 		(r &gt;= 95 &amp;&amp; r &lt;=100) {
JOptionPane.showMessageDialog(null, &quot;High Honor&quot;);
}else if (r &gt;= 90 &amp;&amp; r &lt;= 94){
JOptionPane.showMessageDialog(null, &quot;Honor&quot;);
}else if (r &gt;=85 &amp;&amp; r &lt;= 89) {
JOptionPane.showMessageDialog(null, &quot;Good&quot;);
}else if (r&gt;=80 &amp;&amp; r&lt;=84)	{
JOptionPane.showMessageDialog(null, &quot;Satisfactory&quot;);
}else if (r&gt;=75 &amp;&amp; r&lt;= 79) {
JOptionPane.showMessageDialog(null, &quot;Low pass, but certifying&quot;);
}else {
JOptionPane.showMessageDialog(null, &quot;Low Failure&quot;);
}
}

}

答案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 :

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

This value 84.8 doesnot fit both the condition you wrote :

if (r &gt;=85 &amp;&amp; r &lt;= 89)
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 :

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

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

if (r &gt;= 95 &amp;&amp; r &lt;= 100) {
JOptionPane.showMessageDialog(null, &quot;High Honor&quot;);
}else if (r &gt;= 90 &amp;&amp; r &lt; 95){
JOptionPane.showMessageDialog(null, &quot;Honor&quot;);
}else if (r &gt;= 85 &amp;&amp; r &lt; 90) {
JOptionPane.showMessageDialog(null, &quot;Good&quot;);
}else if (r &gt;= 80 &amp;&amp; r &lt; 85) {
JOptionPane.showMessageDialog(null, &quot;Satisfactory&quot;);
}else if (r &gt;= 75 &amp;&amp; r &lt; 80) {
JOptionPane.showMessageDialog(null, &quot;Low pass, but certifying&quot;);
}else {
JOptionPane.showMessageDialog(null, &quot;Low Failure&quot;);
}

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

Example :

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:

确定