Closed. This question is not reproducible or was caused by typos。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

6年前关闭。



Improve this question



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));我相信这与上一个错误有关。

最佳答案

亲爱的您将需要通过添加另一个参数来将满意度编号传递到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中都没有定义。由于可以通过满意度编号找到它,因此您可以执行此操作。
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;

}

最后两个中的任何一个都可以。

10-08 13:09