gpt4 book ai didi

java - 需要帮助格式化 switch 方法调用的输出

转载 作者:行者123 更新时间:2023-12-02 02:34:11 25 4
gpt4 key购买 nike

这是针对我正在学习的类(class),我们被指示创建一个具有两个参数(价格和状态)的方法,然后让用户输入任何价格,该方法将针对该价格运行并输出成本来自多个州。我为自己弄清楚如何让它大部分发挥作用而感到非常自豪。但是,有几个小问题需要帮助解决。

  • 仅当我输入整数时才有效
  • 超过两位小数位
  • 即使我没有要求它,它也会显示“case”值。

在展示我的代码之前,这里是一个示例输出。

输入 donut 的价格:
5
这是每个州的费用
肯塔基州-5.3

哦 - 5.2875

IN - 5.35

TN - 5.35
tn
佛罗里达州 - 5.3
FL
WF-5.3
wf

    import java.util.Scanner;
public class TaxCalculator {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter price of donut: ");
double price = input.nextInt();
String[] state=new String[6];
state[0]="ky";
state[1]="oh";
state[2]="in";
state[3]="tn";
state[4]="fl";
state[5]="wv";

System.out.println("Here's how much your donut would cost in each state");
System.out.println(addTax(price,state[0]));
System.out.println(addTax(price,state[1]));
System.out.println(addTax(price,state[2]));
System.out.println(addTax(price,state[3]));
System.out.println(addTax(price,state[4]));
System.out.println(addTax(price,state[5]));
}


public static String addTax( double price, String state ) {
switch (state) {
case "ky": System.out.println("KY - " + ((price*.06)+price));
break;
case "oh": System.out.println("OH - " + ((price*.0575)+price));
break;
case "in": System.out.println("IN - " + ((price*.07)+price));
break;
case "tn": System.out.println("TN - " + ((price*.07)+price));
break;
case "fl": System.out.println("FL - " + ((price*.06)+price));
break;
case "wv": System.out.println("WV - " + ((price*.06)+price));
break;
default: System.out.println("Invalid");
break;
}
return state;
}
}

谢谢大家!我结合了您的很多建议并得到了我所需要的。我喜欢这个地方。

import java.text.DecimalFormat;
import java.util.Scanner;
public class TaxCalculator {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter price of donut: ");
double price = input.nextDouble();
String[] state=new String[6];
state[0]="ky";
state[1]="oh";
state[2]="in";
state[3]="tn";
state[4]="fl";
state[5]="wv";

System.out.println("Here's how much your donut would cost in each state");
addTax(price,state[0]);
addTax(price,state[1]);
addTax(price,state[2]);
addTax(price,state[3]);
addTax(price,state[4]);
addTax(price,state[5]);
}


public static String addTax( double price, String state ) {
String result=("");
DecimalFormat money=new DecimalFormat("$#,###.00");
switch (state) {
case "ky": System.out.println("KY - " + money.format((price*.06)+price));
break;
case "oh": System.out.println("OH - " + money.format((price*.0575)+price));
break;
case "in": System.out.println("IN - " + money.format((price*.07)+price));
break;
case "tn": System.out.println("TN - " + money.format((price*.07)+price));
break;
case "fl": System.out.println("FL - " + money.format((price*.06)+price));
break;
case "wv": System.out.println("WV - " + money.format((price*.06)+price));
break;
default: System.out.println("Invalid");
break;
}

return state;

}
}

最佳答案

而不是:

System.out.println(addTax(price,state[0]));
case "ky": System.out.println("KY - " + ((price*.06)+price));
break;
return state;

这样做:

System.out.printf("%.1d", addTax(price,state[0]));
case "ky": String result = "KY - " + ((price*.06)+price);
break;
return result;

或者类似的东西。这也是您在返回状态时打印状态并每次调用两次 System.out.println 的原因。调用 Double.parseDouble 而不是 Integer.parseInt 即可输入 double 。

关于java - 需要帮助格式化 switch 方法调用的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46648908/

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