英文:
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<5;i++) {
grade = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the grades "));
tgrade = tgrade+grade;
gcount++;
System.out.println(gcount+". "+"grade: "+grade );
}
DecimalFormat c = new DecimalFormat("##.##");
tgrade = tgrade/500*100;
r = new Double(c.format(tgrade)).doubleValue();
System.out.print("Total Grade: "+(tgrade)+"\n");
if (r >= 95 && r <=100) {
JOptionPane.showMessageDialog(null, "High Honor");
}else if (r >= 90 && r <= 94){
JOptionPane.showMessageDialog(null, "Honor");
}else if (r >=85 && r <= 89) {
JOptionPane.showMessageDialog(null, "Good");
}else if (r>=80 && r<=84) {
JOptionPane.showMessageDialog(null, "Satisfactory");
}else if (r>=75 && r<= 79) {
JOptionPane.showMessageDialog(null, "Low pass, but certifying");
}else {
JOptionPane.showMessageDialog(null, "Low Failure");
}
}
}
答案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 >=85 && r <= 89)
if (r>=80 && r<=84)
Hence it is going out.
You can use the below options.
FIRST Option : Don't use the range as below :
if (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");
}
SECOND Option : If you are using range then frame the conditions like below :
if (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");
}
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>=80 && r<=84)
to else if (r>=80 && r<=84)
and it will work
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论