计算器提示为什么不编译

时间:2014-02-04 03:04:21

标签: java compiler-errors

import java.util.Scanner;

public class Hw4Part4 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Ask for the diners’ satisfaction level using these ratings: 1 = Totally
        // satisfied, 2 = Satisfied,
        // 3 = Dissatisfied.
        System.out.println("Satisfacion leve: ");
        int satisfactionNumber = sc.nextInt();

        // Ask for the bill subtotal (not including the tip)
        System.out.println("What is the bill subtotal: ");
        double subtotal = sc.nextInt();

        // Report the satisfaction level and bill total.
        System.out.println("The satisfaction level is: " +
                           satisfactionLevel(satisfactionNumber));

        System.out.println("The bill total is: " +
                           getBillTotal(tipPercentage, subtotal));
    }

    public static String satisfactionLevel(int satisfactionNumber) {

        String satisfactionL = "";

        if (satisfactionNumber == 1) {
          satisfactionL = "Totally-satisfied";
        }

        if (satisfactionNumber == 2) {
          satisfactionL = "Satisfied";
        }
        if (satisfactionNumber == 3) {
          satisfactionL = "Dissatisfied";
        }
        return satisfactionL;
     }

      // This method takes the satisfaction number and returns the percentage of tip
      // to be
      // calculated based on the number.
      // This method will return a value of 0.20, 0.15, or 0.10
    public static double getPercentage(int satisfactionNumber) {

        double getPercentage = 0;
        if (satisfactionNumber == 1) {
          getPercentage = 0.20;
        }
        if (satisfactionNumber == 2) {
          getPercentage = 0.15;
        }
        if (satisfactionNumber == 3) {
          getPercentage = 0.10;
        }
        return getPercentage;
    }

    public static double getBillTotal(double tipPercentage, double subtotal) {

        double totalWithTip =
            (subtotal + (getPercentage(satisfactionNumber) * subtotal));
        return totalWithTip;
    }
}

如果getPercentage(satisfactionNumber)*subtotal.....SatisfactionNumber cannot be resolved to a variable

,则会出错

在Main方法中出现错误 System.out.println("The bill total is: " + getBillTotal(tipPercentage, subtotal));我认为这与最后一次错误有关。

2 个答案:

答案 0 :(得分:2)

getBillTotal中,satisfactionNumber未定义,它在方法的上下文中具有意义。为了使用它,您需要在方法的上下文中将变量定义为参数或局部变量......

main方法中,tipPercentage遇到了同样的问题,它未定义...

答案 1 :(得分:1)

你的亲密关系。您需要通过添加另一个参数将satisfNumber传入getBillTotal。否则,当你说满意时,它不知道你正在采取什么。它不能直接看到其他函数中的变量。

public static double getBillTotal(double tipPercentage, double subtotal, int satisfactionNumber) {

    double totalWithTip = (subtotal + (getPercentage(satisfactionNumber) * subtotal));
    return totalWithTip;

}

然后在你的main方法调用中传入它。

public static void main(String[] args) {
    ....
    System.out.println("The bill total is: " + getBillTotal(tipPercentage, subtotal, satisfactionNumber));
}

实际上你不需要tipPercentage,事实上它甚至没有在main中定义。因为可以通过satisfNumber找到它,你可以这样做。

public static void main(String[] args) {
    ....
    System.out.println("The bill total is: " + getBillTotal(subtotal, satisfactionNumber));
}

...

public static double getBillTotal(double subtotal, int satisfactionNumber) {

    double totalWithTip = (subtotal + (getPercentage(satisfactionNumber) * subtotal));
    return totalWithTip;

}

或者你可以先通过计算来传递tipPercentage。

public static void main(String[] args) {
    ....
    double tipPercentage = getPercentage(satisfactionNumber);
    System.out.println("The bill total is: " + getBillTotal(tipPercentage, subtotal));
}

...

public static double getBillTotal(double tipPercentage, double subtotal) {

    double totalWithTip = (subtotal + (tipPercentage * subtotal));
    return totalWithTip;

}

最后两个中的任何一个都没问题。

相关问题