这是针对我正在指示创建一个具有两个参数(价格和状态)然后让用户输入任何价格的方法的类,并且该方法将针对它运行并输出成本来自多个州。我非常自豪地想弄清楚如何让它主要工作。但是,我需要帮助解决一些小问题。
在我展示我的代码之前,这是一个示例输出。
输入甜甜圈的价格:
5
这是每个州的成本
KY - 5.3
KY
OH - 5.2875
哦
IN - 5.35
在
TN - 5.35
TN
FL - 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;
}
}
答案 0 :(得分:1)
对于第1点,您要拨打input.nextInt()
以获得双倍,您只需拨打
input.nextDouble()
如果你想要两个小数位,你可以像这样使用DecimalFormater
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(decimalNumber));
您可以通过价格* 1.06等简化价格计算
你的addTax方法返回一个字符串,然后你返回状态,这就是打印case值的原因
答案 1 :(得分:1)
而不是:
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;
或类似的东西。这也是您在返回时使用state
进行打印并使用两次System.out.println
每次调用的原因。拨打Double.parseDouble
而不是Integer.parseInt
,您可以输入双打。
答案 2 :(得分:0)
1)修改你的输入以取nextDouble()
2)在你的print语句中使用String.format
,如下所示:
System.out.println("KY - " + String.format("%.2f", ((price * 0.6) + price)));
3)在调用System.out.println
方法时删除addTax
- 因为您已经在此方法中进行了打印
答案 3 :(得分:0)
1)尝试double price = input.nextDouble();
nextInt()可能会抛出inputMissMatchException,因为您从输入中获取整数值并分配给double。
2)您可以使用DecimalFormat df = new DecimalFormat("#.##");
然后使用System.out.println(df.format(value))
将double值四舍五入到2个十进制空格。
3)从main方法中删除所有print语句,因为返回值为State,它将打印main中所有print语句的状态值。
答案 4 :(得分:0)
干得好,我想建议一个更清晰的方式来写它:
public static void main(String[] args){
System.out.println("Enter price of donut: ");
Scanner input = new Scanner(System.in);
double price = input.nextInt();
Map<String, Double> multipliers = new LinkedHashMap<>();
multipliers.put("KY", .06);
multipliers.put("OH", .0575);
multipliers.put("IN", .07);
multipliers.put("TN", .07);
multipliers.put("FL", .06);
multipliers.put("WV", .06);
System.out.println("Here's how much your donut would cost in each state");
multipliers.forEach((x, y) -> System.out.println(x + " - " + (price + (price * y))));
}