我在打印数字时遇到问题

时间:2016-03-22 19:34:47

标签: java if-statement

我正在尝试使用scanner用于salesTotal变量的用户输入,并且有2个if语句,但它没有正确显示我的委托率。对于我写的每个值,我都给出0。

import java.util.Scanner;

public class Q2 {
    public static void main(String[] args) {

        double commisionRate = 0;
        double salesTotal;

        Scanner sc = new Scanner(System.in);

        salesTotal = sc.nextDouble();

    if(salesTotal <=10.000){
        commisionRate = (commisionRate*100)/2;
        System.out.println("the commision rate is : " +commisionRate);

    }
    else if(salesTotal>10.000){
        commisionRate = (commisionRate*100)/5;
        System.out.println("the commision rate is : " +commisionRate);
    }


}
}

1 个答案:

答案 0 :(得分:1)

commisionRate 是一个依赖于用户输入salesTotal的变量, 您正在阅读salesTotal但从未使用它来计算commisionRate

你应该这样做:

 if(salesTotal <=10.000){
        commisionRate = (salesTotal *100)/2;
        System.out.println("the commision rate is : " +commisionRate);

    }
    else if(salesTotal>10.000){
        commisionRate = (salesTotal *100)/5;
        System.out.println("the commision rate is : " +commisionRate);
    }