没有小数点打印
not getting a decimal to print
所以我试图找到 class.. 中的出勤率。使用 attend=total present / tot students.. 由于某种原因,我一直得到零的答案。我将 attend 变量初始化为 double。在有人回答之前,我总是想明确这是作业。我有一段时间很忙,因为我似乎无法像通常对 C++ 那样使用编译器进行调试,所以如果有人对 IDE 有任何建议,我也将非常感谢您的帮助。我的代码如下
tot_stu+=students;
tot_pres+=present;
absent = tot_stu - tot_pres;
attend = tot_pres / tot_stu;
//test
System.out.println(tot_stu);
System.out.println(tot_pres);
System.out.println(attend);
//output
System.out.println("The school has " +tot_stu+ " students");
System.out.println("The school has " +tot_pres+ " students present");
System.out.println("The school has "+absent+" students absent");
System.out.println("The schools over all attendence rate is: " + attend);
System.out.println("");
我不断得到这个输出
Please enter the total number of classes: 2
Enter the total students in class 1:10
Enter how many students were present in class 1: 5
10
5
0.0
The school has 10 students
The school has 5 students present
The school has 5 students absent
The schools over all attendance rate is: 0.0
Enter the total students in class 2: 65
Enter how many students were present in class 2: 7
65
7
0.0
The school has 65 students
The school has 7 students present
The school has 58 students absent
The schools over all attendance rate is: 0.0
问题是您可能将 int
用于 tot_pres
和 tot_stu
。要修复,请将您的行更改为:
attend = tot_pres / (tot_stu+0.0);
或
attend = tot_pres / ((double)tot_stu);
或任何其他变体!唯一吐出结果 int
值的是当分母和分子都为 int
.
时
执行此操作时,编译器会将分母读取为 double
而不是 int
。
确保 tot_pres 或 tot_stu 是双倍的。
或者您可以简单地执行以下操作..
((双)tot_pres)/tot_stu;
确保您使用的是浮点类型而不是 int。
attend 必须是浮点类型
如果 tot_pres 和 tot_stu 是整数,则在除法时将它们转换为浮点数:
attend = (double)tot_pres /tot_stu;
如果你用这个替换部分代码会发生什么?
double attend;
int total_students = 2;
int total_present = 10;
attend = total_present/total_students;
所以我试图找到 class.. 中的出勤率。使用 attend=total present / tot students.. 由于某种原因,我一直得到零的答案。我将 attend 变量初始化为 double。在有人回答之前,我总是想明确这是作业。我有一段时间很忙,因为我似乎无法像通常对 C++ 那样使用编译器进行调试,所以如果有人对 IDE 有任何建议,我也将非常感谢您的帮助。我的代码如下
tot_stu+=students;
tot_pres+=present;
absent = tot_stu - tot_pres;
attend = tot_pres / tot_stu;
//test
System.out.println(tot_stu);
System.out.println(tot_pres);
System.out.println(attend);
//output
System.out.println("The school has " +tot_stu+ " students");
System.out.println("The school has " +tot_pres+ " students present");
System.out.println("The school has "+absent+" students absent");
System.out.println("The schools over all attendence rate is: " + attend);
System.out.println("");
我不断得到这个输出
Please enter the total number of classes: 2
Enter the total students in class 1:10
Enter how many students were present in class 1: 5
10
5
0.0
The school has 10 students
The school has 5 students present
The school has 5 students absent
The schools over all attendance rate is: 0.0
Enter the total students in class 2: 65
Enter how many students were present in class 2: 7
65
7
0.0
The school has 65 students
The school has 7 students present
The school has 58 students absent
The schools over all attendance rate is: 0.0
问题是您可能将 int
用于 tot_pres
和 tot_stu
。要修复,请将您的行更改为:
attend = tot_pres / (tot_stu+0.0);
或
attend = tot_pres / ((double)tot_stu);
或任何其他变体!唯一吐出结果 int
值的是当分母和分子都为 int
.
执行此操作时,编译器会将分母读取为 double
而不是 int
。
确保 tot_pres 或 tot_stu 是双倍的。 或者您可以简单地执行以下操作.. ((双)tot_pres)/tot_stu;
确保您使用的是浮点类型而不是 int。 attend 必须是浮点类型 如果 tot_pres 和 tot_stu 是整数,则在除法时将它们转换为浮点数:
attend = (double)tot_pres /tot_stu;
如果你用这个替换部分代码会发生什么?
double attend;
int total_students = 2;
int total_present = 10;
attend = total_present/total_students;