0

如何使用 computeTax 方法计算 main 中定义的变量 tax,然后稍后在 main 中调用 tax 的值?对于您在下面看到的任何代码,我们将不胜感激。

import java.util.Scanner;

// Date: Sep 29, 2015


public class R8 {

    public static void main(String[] args) {
//      Read all the steps carefully before beginning. Understand the larger picture.
//      Create a new Java Project named R8, and make a class named R8.
//      Copy the following code and paste it inside the the curly braces of the main method:
//      // Declare variables
        String restaurantName;
        String serverName;
        double subtotal;
        double tax;
        double total;
        double taxRate = 0.05;
        double tipRate1 = 0.10;
        double tipRate2 = 0.15;
        double tipRate3 = 0.20;


//      // Ask and receive input from the user
//      Create a Scanner object, prompt the user for the name of the restaurant, and read in their input to the variable restaurantName. The restaurant can be be more than one word, like "Park Sushi."
        Scanner scanner = new Scanner(System.in);
        System.out.println("name of the restaurant: ");
        restaurantName = scanner.nextLine();

//      Prompt the user for the name of the server. Store the value in the variable serverName. Assume the server name will always be a first and last name, separated by one space character.
        System.out.println("server name: ");
        serverName = scanner.nextLine();

//      Prompt the user for the cost of the bill. Store the value in the variable subtotal. Assume the cost will be a double value representing dollars, for example 56.23.
        System.out.println("Total bill cost: ");
        subtotal = scanner.nextDouble();        

我还没有进行计算

    // Perform calculations

我的输出代码

    // Print receipt    
//              =====================================
//              Park Sushi
//              Your server was: JULIE
//              Subtotal: $56.23
//              Tax: $2.81
//              =====================================
//              Total: $59.04
//
//              Suggested tips:
//              10%: $5.90
//              15%: $8.86
//              20%: $11.81
//
//              Thank you!
//              =====================================
        System.out.println("=====================================");
        System.out.println(restaurantName);
        System.out.println("Your server was: " + serverName.toUpperCase());
        System.out.println("Subtotal: $" + subtotal);
        System.out.println(tax);
        System.out.println("=====================================");
        System.out.println("Total: $/n" + total);
        System.out.println("Suggested tips:");
        System.out.println("10%: " + tipRate1);
        System.out.println("15%: " + tipRate2);
        System.out.println("20%: /n" + tipRate3);
        System.out.println("Thank you!");

        System.out.println("=====================================");
    }

计算税法我必须用来计算。

//  Write a method to calculate the tax on the bill based on the subtotal and taxRate. Call the method and store the result in the variable tax. Here is the signature of the method:
//  Calculate the total bill by adding the subtotal and tax together. Store the total bill in the variable total.
//  Write a method to calculate the suggested tip, based on the total and the tip rate. Here is the signature of the method:
    public static double computeTax(double amount, double rate){
        tax = amount + rate
        return tax;
    }

}
4

2 回答 2

1

您只需要将方法的返回值computeTax与局部变量相关联tax

在您的主要方法中:

tax = computeTax(subtotal, taxRate);

而你的方法computeTax只会计算税收。

private static double computeTax(double amount, double rate) {
    return amount * rate;
}
于 2015-09-30T00:11:30.237 回答
0

要计算税收变量,您可以定义一个税收变量并在 main 方法中使用 tax= computeTax(subtotal, rate);

这是代码

导入 java.util.Scanner;

公共类 R8 {

public static double computeTax(double amount, double rate){
    double tax = amount + rate;
            return tax;
}

public static void main(String[] args) {
    String restaurantName;
    String serverName;
    double subtotal;
    double tax;
    double rate;
    double total;           

    Scanner scanner = new Scanner(System.in);
    System.out.println("name of the restaurant: ");
    restaurantName = scanner.nextLine();


    System.out.println("server name: ");
    serverName = scanner.nextLine();


    System.out.println("Total bill cost: ");
    subtotal = scanner.nextDouble(); 

    System.out.println("Suggested tips:");
    rate = scanner.nextDouble(); 

    tax = computeTax(subtotal, rate);
    System.out.println("Total bill cost with Tip: "+tax);

    System.out.println("Thank you!");
}

}

于 2015-09-30T00:53:33.027 回答