如何将 java 中的双重用户输入值四舍五入?
how do you round up a double user input value in java?
我是一名学生,正在制作一个简单的评分系统,我正在努力研究如何做到这一点
当我输入一个特定的数字时,它会绕过我的 else-if 语句
数字是 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, "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");
}
}
}
将else if (r>=80 && r<=84)
更改为else if (r>=80 && r<=84)
,它将起作用
你的变量 r 是双精度的。
例如:
You have these numbers : 98, 67, 98, 80 and 81
The average is : 424/5 = 84.8
这个值 84.8 不符合您写的两个条件:
if (r >=85 && r <= 89)
if (r>=80 && r<=84)
所以它要出去了。
您可以使用以下选项。
第一个选项:不要使用如下范围:
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 (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)
我是一名学生,正在制作一个简单的评分系统,我正在努力研究如何做到这一点
当我输入一个特定的数字时,它会绕过我的 else-if 语句 数字是 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, "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");
}
}
}
将else if (r>=80 && r<=84)
更改为else if (r>=80 && r<=84)
,它将起作用
你的变量 r 是双精度的。
例如:
You have these numbers : 98, 67, 98, 80 and 81
The average is : 424/5 = 84.8
这个值 84.8 不符合您写的两个条件:
if (r >=85 && r <= 89)
if (r>=80 && r<=84)
所以它要出去了。
您可以使用以下选项。
第一个选项:不要使用如下范围:
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 (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)