如何使用computeTax方法计算main中定义的变量的税,然后在main中调用税的值?

问题描述 投票:0回答:2

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

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;
    }

}
java methods
2个回答
1
投票

您只需要将方法

computeTax
的返回值与局部变量
tax
相关联即可。

在你的主要方法中:

tax = computeTax(subtotal, taxRate);

而你的方法

computeTax
只会计算税费。

private static double computeTax(double amount, double rate) {
    return amount * rate;
}

0
投票

要计算税收变量,您可以定义一个税收变量并在主方法中使用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!");
}

}

© www.soinside.com 2019 - 2024. All rights reserved.