(JAVA)用于我的自动售货机的do while循环

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

(JAVA) do while loop for my vending machine

问题

package vending.machine;
import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
import static vending.machine.adddrinks.drinksList;

public class VendingMachine {
    public static void main (String []args){
        Scanner sc= new Scanner(System.in);
        
        double money;
        double total;
        double balance;
        
        do{
            System.out.println("Please insert money:");
            money = sc.nextDouble();
            if(money < 1.2){
                System.out.println("Not enough money");
            }
        }while(money < 1.2);
        System.out.println("What drinks are you looking for");
        adddrinks.showDrinks();
        adddrinks.viewDrinks();
        
        System.out.print("Select: 1 or 2 or 3 or 4\n");
        int select=sc.nextInt();
        
        do{
            switch(select){
                case 1:{
                    total = adddrinks.drinksList.get(0).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }
                case 2:{
                    total = adddrinks.drinksList.get(1).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }
                case 3:{
                    total = adddrinks.drinksList.get(2).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }
                case 4:{
                    total = adddrinks.drinksList.get(3).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }
                
                default:{
                    System.out.println("Invalid");
                    break;
                }
            }
            
        }while(select<5);
    }
}
英文:

this project i use do while loop with switch case to check the input case is not match or not. i run the code but the result not what i wanted. what i expect is if the user type the wrong case, the do while loop will loop back to the input where user need to enter the case.

here is the code

package vending.machine;
import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
import static vending.machine.adddrinks.drinksList;
public class VendingMachine {
public static void main (String []args){
Scanner sc= new Scanner(System.in);
double money;
double total;
double balance;
do{
System.out.println(&quot;\nPlease insert money:&quot;);
money = sc.nextDouble();
if(money &lt; 1.2){
System.out.println(&quot;Not enough money&quot;);
}
}while(money &lt; 1.2);
System.out.println(&quot;What drinks are you looking for&quot;);
adddrinks.showDrinks();
adddrinks.viewDrinks();
System.out.print(&quot;Select: 1 or 2 or 3 or 4\n&quot;);
int select=sc.nextInt();
do{
switch(select){
case 1:{
total = adddrinks.drinksList.get(0).getdrinkPrice();
balance = money - total;
System.out.println(&quot;Here is your balance: &quot; + balance);
break;
}
case 2:{
total = adddrinks.drinksList.get(1).getdrinkPrice();
balance = money - total;
System.out.println(&quot;Here is your balance: &quot; + balance);
break;
}
case 3:{
total = adddrinks.drinksList.get(2).getdrinkPrice();
balance = money - total;
System.out.println(&quot;Here is your balance: &quot; + balance);
break;
}
case 4:{
total = adddrinks.drinksList.get(3).getdrinkPrice();
balance = money - total;
System.out.println(&quot;Here is your balance: &quot; + balance);
break;
}
default:{
System.out.println(&quot;Invalid&quot;);
break;
}
}
}while(select&lt;5);
}
}

here is the result
enter image description here

答案1

得分: 1

根据我从你的代码中所理解的,当你输入为5时,会显示无效。之后它会进入 while 语句并在那里检查条件。如果你在 switch 语句内部并选择了任意的 case,它会显示无效。之后会根据你输入的数字进行处理。

如果输入的数字小于5,它会再次进入 switch 语句。

但这似乎不太合理,如果你继续提供正确的输入,代码将会持续执行,导致余额变成负数。这个条件应该修改为:

while(balance>1.2)

假设这是购买饮料所需的最低金额。这将在每次购买饮料后检查条件,希望能够达到你的预期效果。
此外注意:将你的代码模块化。

英文:

From what I understood from your code. When you are giving the input as 5 it is giving invalid.
After that it will go to the while statement and check the condition there. If you are inside the switch case and select any random case It will show you invalid. After that depending upon the number that you have entered.

If the number is less than 5, It will again go to switch case.

As it doesn't make sense as If you continue to provide correct input to it. The code will continue to execute making the balance going in the negative. this condition should be changed to

while(balance&gt;1.2)

assuming that it is minimum amount that is necessary to buy a drink. This will check the condition after every drink and will hopefully do what you were hoping.
On side Note : Make your code modular.

答案2

得分: 0

你需要循环遍历你的输入,我稍微改进了你的代码(抱歉,我不喜欢重复):

private static void main10(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("你想要什么饮料");
    adddrinks.showDrinks();
    adddrinks.viewDrinks();
    int select = 0;
    double balance = 0;
    boolean running = true;
    while (running) {
        if (sc.hasNextInt()) {
            select = sc.nextInt();
            if (0 < select && select <= adddrinks.drinksList.size()) {
                double price = adddrinks.drinksList.get(select - 1).getdrinkPrice();
                if (balance < price) {
                    System.out.println("余额不足," + select + "需要" + price);
                } else {
                    balance -= price;
                    System.out.println("你选择了" + select + ",它将在售货机中找到");
                }
            } else {
                System.out.println("无效的输入,请重试");
            }
        } else if (sc.hasNextDouble()) {
            balance += sc.nextDouble();
        } else {
            String input = sc.next();
            if (input.equals("q")) {
                running = false;
                if (balance > 0)
                    System.out.println("请不要忘记带走你的零钱,金额为:" + balance);
                System.out.println("祝你有美好的一天,期待再次见到你");
                break;
            } else if (input.equals("h")) {
                System.out.println("你想要什么饮料");
                adddrinks.showDrinks();
                adddrinks.viewDrinks();
            } else {
                System.out.println("无效的输入,请重试");
            }
        }
        System.out.println("你的余额是:" + balance);
        System.out.println("请选择你的产品(例如 2),投入硬币(例如 2.0),点击'h'查看产品列表,或点击'q'找零");
    }
}
英文:

You need to loop over your input, i was so free to improve your code a bit (sorry I do not like repetations):

private static void main10(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(&quot;What drinks are you looking for&quot;);
adddrinks.showDrinks();
adddrinks.viewDrinks();
int select = 0;
double balance = 0;
boolean running = true;
while (running) {
if (sc.hasNextInt()) {
select = sc.nextInt();
if (0 &lt; select &amp;&amp; select &lt;= adddrinks.drinksList.size()) {
double price = adddrinks.drinksList.get(select - 1).getdrinkPrice();
if (balance &lt; price) {
System.out.println(&quot;Not enough money, &quot; + select + &quot; costs &quot; + price);
} else {
balance -= price;
System.out.println(&quot;You choosed &quot; + select + &quot; , you will find it in the dispenser&quot;);
}
} else {
System.out.println(&quot;Invalid input, please retry&quot;);
}
} else if (sc.hasNextDouble()) {
balance += sc.nextDouble();
} else {
String input = sc.next();
if (input == &quot;q&quot;) {
running = false;
if (0 &lt; balance)
System.out.println(&quot;please don&#39;t forget your change with amount of: &quot; + balance);
System.out.println(&quot;Have a nice day, happy to see you again&quot;);
break;
} else if (input == &quot;h&quot;) {
System.out.println(&quot;What drinks are you looking for&quot;);
adddrinks.showDrinks();
adddrinks.viewDrinks();
} else {
System.out.println(&quot;Invalid input, please retry&quot;);
}
}
System.out.println(&quot;Your balance is: &quot; + balance);
System.out.println(
&quot;please chouce your product (e.g 2), enter coins (e.g 2.0), click on &#39;h&#39; to show product list or click on &#39;q&#39; to get your change&quot;);
}
}

huangapple
  • 本文由 发表于 2020年3月16日 15:17:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/60701734.html
匿名

发表评论

匿名网友

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

确定