(JAVA)有没有更高效的方法来完成这个任务?

huangapple go评论79阅读模式
英文:

(JAVA) Is there a more efficient way to do this?

问题

这部分程序会要求用户输入所需饮品的数量,如果饮品数量为负数,它会返回“无效数量”。

System.out.print("请输入您想购买的每种饮品数量:");
System.out.println();

System.out.print("小杯咖啡:");
int smallCoffee = scnr.nextInt();
if (smallCoffee < 0) {
    System.out.println("无效数量");
    System.exit(1);
}
System.out.print("中杯咖啡:");
int mediumCoffee = scnr.nextInt();

System.out.print("大杯咖啡:");
int largeCoffee = scnr.nextInt();

System.out.print("香蕉莓果冰沙:");
int berrySmoothie = scnr.nextInt();

System.out.print("摩卡奶昔:");
int mochaShake = scnr.nextInt();

System.out.print("覆盆子绿茶:");
int greenTea = scnr.nextInt();

除了在每个选项下放置一个 if 语句,是否有其他方法可以在每个选项下输出“无效数量”?

英文:

This part of my program asks the user to enter the number of drinks they want and it must return "Invalid amount" if the number of drinks is negative.

System.out.print(&quot;Please enter the number of each drink that you like to purchase:&quot;);
System.out.println();
        
System.out.print(&quot;Small Coffee: &quot;);
int smallCoffee = scnr.nextInt();
if (smallCoffee &lt; 0) {
    System.out.println(&quot;Invalid amount&quot;)
    System.exit(1);
}
System.out.print(&quot;Medium Coffee: &quot;);
int mediumCoffee = scnr.nextInt();
        
System.out.print(&quot;Large Coffee: &quot;);
int largeCoffee = scnr.nextInt();
        
System.out.print(&quot;Banana Berry Smoothie: &quot;);
int berrySmoothie = scnr.nextInt();
        
System.out.print(&quot;Mocha Shake: &quot;);
int mochaShake = scnr.nextInt();
        
System.out.print(&quot;Raspberry Green Tea: &quot;);
int greenTea = scnr.nextInt();

Instead of putting an if-statement under each option, is there any other way I can output "invalid amount" under each option?

答案1

得分: 1

当然是方法。你有一个任务:

向用户询问一个非负数。

因此,编写一个封装了这个任务的方法:

public int getAmount(Scanner scanner, String prompt) {
    while (true) {
        System.out.println(prompt);
        int v = scanner.nextInt();
        if (v >= 0) return v;
        System.out.println("只能输入正数,请重新输入。");
    }
}

注意现在你可以写一次“如果他们搞错了,就再次询问,而不是直接退出”的代码,并且可以在需要时重复使用这段代码。

英文:

Methods, of course. You have a task:

Ask the user for a non-negative number.

So, write a method that encapsulates the job:

public int getAmount(Scanner scanner, String prompt) {
    while (true) {
        System.out.println(prompt);
        int v = scanner.nextInt();
        if (v &gt;= 0) return v;
        System.out.println(&quot;Please enter positive numbers only.&quot;);
    }
}

note how you can now write 'if they mess up, ask again instead of outright quitting' exactly once, and reuse this as many times as you like.

答案2

得分: 0

@rzwitserloot提出的建议是编写一个方法来封装输入任务这是最直接也可能是最正确的方法

然而如果出于任何原因您真的不想再编写另一个函数您可以使您的变量名变得动态将它们编写为一个字符串列表遍历这些字符串并将它们存储在数据结构中[`Map`可能是最直接的](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html))。

```java
Map<String, Integer> variables = new HashMap<>();
String[] variableNames = {
    "Small Coffee",
    "Medium Coffee",
    "Large Coffee",
    "Banana Berry Smoothie",
    "Mocha Shake", 
    "Raspberry Green Tea"
};
System.out.println("请输入您想购买每种饮品的数量:");
for (String drinkName : variableNames) {
    // 询问饮品数量
    System.out.print(drinkName + ": ");
    int amount = scnr.nextInt();
    // 错误检查
    if (amount < 0) {
        System.out.println("无效数量");
        System.exit(1);
    }
    // 添加至映射
    variables.put(drinkName, amount);
}
英文:

@rzwitserloot's suggestion of writing a method to encapsulate the task of input, is the most straightforward, and probably most 'correct' method of doing this.

However, if you really don't want to write another function for whatever reason, you can make your variable names dynamic! Write them as a list of strings, iterate through those strings, and store them in a data structure (Map is probably the most straightforward).

Map&lt;String, Integer&gt; variables = new HashMap&lt;&gt;();
String[] variableNames = {
    &quot;Small Coffee&quot;,
    &quot;Medium Coffee&quot;,
    &quot;Large Coffee&quot;,
    &quot;Banana Berry Smoothie&quot;,
    &quot;Mocha Shake&quot;, 
    &quot;Raspberry Green Tea&quot;
};
System.out.println(&quot;Please enter the number of each drink that you like to purchase:&quot;);
for (String drinkName : variableNames) {
    // ask for the amount of drink
    System.out.print(drinkName + &quot;: &quot;);
    int amount = scnr.nextInt();
    // error check
    if (amount &lt; 0) {
        System.out.println(&quot;Invalid amount&quot;);
        System.exit(1);
    }
    // add to map
    variables.put(drinkName, amount);
}
    


</details>



huangapple
  • 本文由 发表于 2020年9月29日 09:47:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64111755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定