System.out.println("Input your last grades:");
Double last_grades = sc.nextDouble();
System.out.println("Input your this grades:");
Double this_grades = sc.nextDouble();
double increase = (this_grades-last_grades)/last_grades;
System.out.printf("%.2f",increase*100);
System.out.println("%");
成绩是int型时,输出的百分比就是0.00%。什么原因呢?
整形运算只保留整数部分,小数无论多少都不会保留
比如:1 / 4=0,而不是0.25。
可以 float increase = (float)(this_grades- last_grades)/this_grades;
嗯多谢,已解决~
Sign in to make a reply
用户5527594890
System.out.println("Input your last grades:");
Double last_grades = sc.nextDouble();
System.out.println("Input your this grades:");
Double this_grades = sc.nextDouble();
double increase = (this_grades-last_grades)/last_grades;
System.out.printf("%.2f",increase*100);
System.out.println("%");