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 に答える