gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:06:55 26 4
gpt4 key购买 nike

如何使用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;
}

}

最佳答案

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

在你的主要方法中:

tax = computeTax(subtotal, taxRate);

您的方法 computeTax 将仅计算税费。

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

关于java - 如何使用computeTax方法计算main中定义的变量的税,然后在main中稍后调用税的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32855463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com