-1

我们已经设置了一项任务来计算用户输入。我的代码可以编译,但是当我测试它时,我的退款输出始终为 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();
     }
 }
4

3 回答 3

0

因为结果 of((100 - selfcon)/100)始终为零,并且您与您一起工作时integers应该使用floator double

于 2013-10-30T21:56:18.113 回答
0

您使用的是整数运算而不是浮点运算,因此像 2/10 这样的表达式的计算结果为 0。

于 2013-10-30T21:59:43.393 回答
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();
于 2013-10-30T23:25:37.157 回答