我们已经设定了计算用户输入的任务。我的代码编译,但是当我测试它时我的退款输出总是0 :( 用户在提示时输入他们的距离和自我贡献,但我的退款公式究竟出了什么问题?任何人都可以为我阐明这一点吗?
import java.util.*;
public final class UserCalc {`
public static void main(String[] args) {
Scanner scanner = new Scanner(System. in );`
System.out.print("Please enter the distance ");
int distance = scanner.nextInt();
System.out.print("Please enter the percentage of self contribution ");
int selfcon = scanner.nextInt();
int trainprice = (75 + (2 / 10)) * distance;
int carprice = (26 + (7 / 10)) * distance;
int refund = (Math.min(trainprice, carprice)) * ((100 - selfcon) / 100);
System.out.print("You get a refund of " + refund + " pounds");
int age = scanner.nextInt();
}
}
答案 0 :(得分:0)
由于((100 - selfcon)/100)
的结果始终为零且您使用integers
,因此您应使用float
或double
。
答案 1 :(得分:0)
您使用的是整数运算而不是浮点运算,因此像2/10这样的表达式求值为0。
答案 2 :(得分:0)
double distance = scanner.nextDouble();
System.out.print("Please enter the percentage of self contribution ");
double selfcon = scanner.nextDouble();
double trainprice = (75 + (2 / 10)) * distance;
double carprice = (26 + (7 / 10)) * distance;
doulbe refund = (Math.min(trainprice, carprice)) * ((100 - selfcon) / 100);
System.out.print("You get a refund of " + refund + " pounds");
int age = scanner.nextInt();