我是初學者,目前正在學習 Java 課程。他們提供練習來制定程式,根據給出的內容計算總價、凈價和增值稅。有一個我不明白的練習。所以我必須創建一個功能,提示用戶輸入總價和增值稅。有了這個,我必須創建另一個函式來計算凈價。但如果我沒記錯的話,你不能回傳 2 個值。我究竟如何在一個函式中提示用戶兩次?對不起,如果這個問題很愚蠢。這是我到目前為止所擁有的:
import java.util.Scanner;
public class Ex1_7_c {
public static void main(String[] args) {
getUser_doubleInput("Enter the gross price", "Enter the VAT");
}
public static double calculNet(double grossPrice, double VAT) {
double result = grossPrice (grossPrice/100*VAT);
return result;
}
public static double getUser_doubleInput(String enterGross, String enterVAT) {
Scanner doubleInput = new Scanner(System.in);
System.out.println(enterGross);
double inputGross = doubleInput.nextDouble();
System.out.println(enterVAT);
double inputVAT = doubleInput.nextDouble();
return inputVAT;
}
}
uj5u.com熱心網友回復:
您可以回傳一個包含所需值的物件。您可以使用 Map 或 ArrayList 等:
public static Map<String,Double> getUserDoubleFromStdin(String enterGross, String enterVAT) {
Scanner doubleInput = new Scanner(System.in);
System.out.println(enterGross);
double inputGross = doubleInput.nextDouble();
System.out.println(enterVAT);
double inputVAT = doubleInput.nextDouble();
return Map.of("inputGross",inputGross,"inputVAT",inputVAT);
}
uj5u.com熱心網友回復:
聽起來您可能會使用包含總價格和增值稅的自定義物件。計算凈價的邏輯也可能存在于這個類中。
public class PriceAndVat {
private double grossPrice;
private double vat;
private double net = -1; // net price is calculated lazily, initial value of -1 should signify that it hasn't been yet evalueated
public PriceAndVat(double grossPrice, double vat) {
this.grossPrice = grossPrice;
this.vat = vat;
}
public double getNet() {
if (net == -1) {
net = grossPrice (grossPrice / 100 * vat);
}
return net;
}
// getters, etc.
}
public static PriceAndVat getPriceAndVatFromUser(String enterGross, String enterVAT) {
Scanner doubleInput = new Scanner(System.in);
System.out.println(enterGross);
double inputGross = doubleInput.nextDouble();
System.out.println(enterVAT);
double inputVAT = doubleInput.nextDouble();
return new PriceAndVat(inputGross, inputVAT);
}
旁注:名稱喜歡getUser_doubleInput并且Ex1_7_c不符合Java 命名約定
uj5u.com熱心網友回復:
這是另一個與您類似的問題-那里有很好的答案:)
如何從 Java 方法回傳 2 個值?
在我看來,您應該計算并回傳 1 個單個值,并且不需要回傳多個值。
import java.util.Scanner;
public class Ex1_7_c {
public static void main(String[] args) {
double result = getUser_doubleInput("Enter the gross price", "Enter the VAT");
System.out.println("result is: " result);
}
public static double getUser_doubleInput(String enterGross, String enterVAT) {
Scanner doubleInput = new Scanner(System.in);
System.out.println(enterGross);
double inputGross = doubleInput.nextDouble();
System.out.println(enterVAT);
double inputVAT = doubleInput.nextDouble();
return inputGross ( inputGross / 100 * inputVAT );
}
}
使用單獨的功能:
public static double calculNet(double grossPrice, double VAT) {
double result = grossPrice ( grossPrice / 100 * VAT );
System.out.println("The result from the gross price " grossPrice " and the VAT " VAT " is " result ".")
return result;
}
public static double getUser_doubleInput(String enterGross, String enterVAT) {
Scanner doubleInput = new Scanner(System.in);
System.out.println(enterGross);
double inputGross = doubleInput.nextDouble();
System.out.println(enterVAT);
double inputVAT = doubleInput.nextDouble();
return calculNet(inputGross, inputVAT);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/512647.html
標籤:爪哇功能方法
