简体   繁体   English

需要帮助格式化开关方法调用的输出

[英]Need help formatting output from a switch method call

This is for a class I'm taking where we were instructed to create a method that has two parameters (price and state) and then have the user enter in any price, and the method would run against it and output the cost from multiple states. 这是针对我正在上的课程的,我们被指示创建一个具有两个参数(价格和状态)的方法,然后让用户输入任何价格,该方法将针对该价格运行并从多个状态输出成本。 I'm pretty proud of myself for figuring out how to make it mostly work. 我为自己弄清楚如何使其有效而感到自豪。 However, there's a couple of minor issues that I need help resolving. 但是,我需要解决一些小问题。

  • It only works if I type in an integer 仅当我输入整数时才有效
  • It goes more than two decimal spaces 它超过两个小数位
  • It shows the "case" value even though I don't ask it to. 即使我不要求,它也会显示“大小写”值。

Before I show my code, here's is an example output. 在显示代码之前,这是示例输出。

Enter price of donut: 输入甜甜圈的价格:
5 5
Here's how much it would cost in each state 这是每个州要花多少钱
KY - 5.3 肯塔基州-5.3
ky ky
OH - 5.2875 俄亥俄州-5.2875
oh
IN - 5.35 输入-5.35
in
TN - 5.35 TN-5.35
tn n
FL - 5.3 FL-5.3
fl FL
WF - 5.3 WF-5.3
wf 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;
    }
}

Thanks Guys! 多谢你们! I combined a lot of your suggestions and got exactly what I needed. 我结合了您的许多建议,并得到了我真正需要的。 I love this place. 我喜欢这个地方。

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;

    }
}

For point number 1, you are calling input.nextInt() for a double, you can simply call 对于第1点,您正在调用input.nextInt()以获取双input.nextInt() ,您可以简单地调用

input.nextDouble()

If you want two decimal places, you can use DecimalFormater like this 如果要保留两位小数,可以使用DecimalFormater这样

DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(decimalNumber));

You can simplify the price calculation by doing price*1.06 etc 您可以通过执行price * 1.06等来简化价格计算

Your addTax method is returning a string, and you return state, this is why the case value is printed 您的addTax方法返回一个字符串,并且您返回状态,这就是打印case值的原因

Instead of: 代替:

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

Do this: 做这个:

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

Or something similar. 或类似的东西。 That's also why you're printing state as you're returning it and using two System.out.println each call. 这就是为什么要在返回state打印state并每次调用使用两个System.out.println Call Double.parseDouble instead of Integer.parseInt and you can input doubles. 调用Double.parseDouble而不是Integer.parseInt ,您可以输入双精度数。

1) amend your input to take nextDouble() 1)修改您的输入以采用nextDouble()

2) use String.format in your print statement like so: 2)在您的打印语句中使用String.format ,如下所示:

System.out.println("KY - " + String.format("%.2f", ((price * 0.6) + price)));

3) remove System.out.println when calling the addTax method - as you're already doing the printing in this method 3)调用addTax方法时删除System.out.println因为您已经在使用此方法进行打印

1) Try double price = input.nextDouble(); 1)尝试double price = input.nextDouble(); nextInt() will probably throw inputMissMatchException as you are fetching integer value from the input and assigning to the double. 当您从输入中获取整数值并分配给double时,nextInt()可能会抛出inputMissMatchException。

2) You can use DecimalFormat df = new DecimalFormat("#.##"); 2)您可以使用DecimalFormat df = new DecimalFormat("#.##"); and then System.out.println(df.format(value)) to round the double value to the 2 decimal spaces. 然后使用System.out.println(df.format(value))将double值四舍五入为2个小数位。

3) Remove all the print statements from the main method as the return value is State, it will print the state value for all the print statements in the main. 3)从main方法中删除所有打印语句,因为返回值为State,它将为main中的所有打印语句打印状态值。

Good job, I would like to suggest a cleaner way to write it: 干得好,我想提出一种更简洁的编写方式:

  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))));
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM