Sure, here’s the translation: 需要帮助创建一个循环流程。

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

Need help making a loop process

问题

package prelim.activities;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        System.out.println("Welcome to Aling Nena's Store. What do you want to buy?");
        System.out.println("\nEnter 1 for Milk");

        Scanner scanner = new Scanner(System.in);
        String product = scanner.nextLine();
        boolean milk = true;

        if (milk) {
            System.out.println("\nThis will cost P15 each, how much would you like to buy?");

            Scanner scanner_price = new Scanner(System.in);
            int amount = scanner_price.nextInt();
            int price = 15 * amount;
            System.out.println("\nYour product costs P" + price);

            System.out.println("\nPlease enter how much will you pay");
            Scanner money = new Scanner(System.in);
            int cash = money.nextInt();

            if (cash < price) {
                System.out.println("Sorry, your cash is not enough. Please enter the amount again");
                // This is the part where I want to go back to "amount of buyers cash"
            }

            if (cash >= price) {
                int change = cash - price;
                System.out.println("\nYour change is P" + change);
                System.out.println("\nThank you! Come again next time.");
                System.exit(0);
            }
        }
    }
}
英文:

so we have an assignment about a check-out counter. I want to loop back to a process if you typed an amount that is not enough for your price. I can't seem to do that because when I try to do the "do" loop, the scanned cash won't be read by "while ()". Thats all thank you.

this is the whole code, the process where my problem start is in "//amount of buyers cash"

package prelim.activities;
import java.lang.annotation.Repeatable;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
//choosing of product
System.out.println(&quot;Welcome to Aling Nena&#39;s Store. What do you want to buy?&quot;);
System.out.println(&quot;\nEnter 1 for Milk&quot;);
//scanners and booleans for product type
Scanner scanner = new Scanner(System.in);
String product = scanner.nextLine();
boolean milk = true;
//if statements for milk
if(milk){
System.out.println(&quot;\nThis will cost P15 each, how much would you like to buy?&quot;);
//scanners and booleans for product price
Scanner scanner_price = new Scanner(System.in);
int amount = scanner_price.nextInt();
int price = 15 * amount;
System.out.println(&quot;\nYour product costs P&quot; + price);
//amount of buyers cash
System.out.println(&quot;\nPlease enter how much will you pay&quot;);
Scanner money = new Scanner(System.in);
int cash = money.nextInt();
//if statement for not enough cash
if(cash &lt; price){
System.out.println(&quot;Sorry, your cash is not enough. Please enter amount again&quot;);
// this is the part where I want to go back to &quot;amount of buyers cash&quot;
}
if(cash &gt;= price){
int change = cash - price;
System.out.println(&quot;\nYour change is P&quot; + change);
System.out.println(&quot;\nThank you! Come again next time.&quot;);
System.exit(0);
}
}
}}
</details>
# 答案1
**得分**: 0
```java
如果我理解正确,您想要每次更新价格,为此您需要更改价格的范围
package prelim.activities;
import java.lang.annotation.Repeatable;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
//选择商品
System.out.println("欢迎来到 Aling Nena 的商店。您想购买什么?");
System.out.println("\n输入 1 选择牛奶");
//扫描仪和产品类型布尔值
Scanner scanner = new Scanner(System.in);
String product = scanner.nextLine();
boolean milk = true;
// 将价格移到此处,以便在整个方法中都可以访问。
int price = 0;
//对于牛奶的if语句
if(milk){
System.out.println("\n每个的价格为 P15,您想购买多少?");
//扫描仪和产品价格布尔值
Scanner scanner_price = new Scanner(System.in);
int amount = scanner_price.nextInt();
price = 15 * amount;
System.out.println("\n您的产品费用为 P" + price);
//购买者支付的金额
System.out.println("\n请输入您将支付多少钱");
Scanner money = new Scanner(System.in);
int cash = money.nextInt();
//不足金额的if语句
if(cash < price){
System.out.println("抱歉,您的金额不足。请重新输入金额");
// 这是我想要返回到 "购买者支付的金额" 部分的地方。
}
if(cash >= price){
int change = cash - price;
System.out.println("\n您的找零为 P" + change);
System.out.println("\n谢谢!下次再来。");
System.exit(0);
}
}
}}
英文:

If i'm following correctly you want to update the price each time for that you have to change the scope of the price

package prelim.activities;
import java.lang.annotation.Repeatable;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
//choosing of product
System.out.println(&quot;Welcome to Aling Nena&#39;s Store. What do you want to buy?&quot;);
System.out.println(&quot;\nEnter 1 for Milk&quot;);
//scanners and booleans for product type
Scanner scanner = new Scanner(System.in);
String product = scanner.nextLine();
boolean milk = true;
// price moved here so it will be accessible through out the method.
int price = 0;
//if statements for milk
if(milk){
System.out.println(&quot;\nThis will cost P15 each, how much would you like to buy?&quot;);
//scanners and booleans for product price
Scanner scanner_price = new Scanner(System.in);
int amount = scanner_price.nextInt();
price = 15 * amount;
System.out.println(&quot;\nYour product costs P&quot; + price);
//amount of buyers cash
System.out.println(&quot;\nPlease enter how much will you pay&quot;);
Scanner money = new Scanner(System.in);
int cash = money.nextInt();
//if statement for not enough cash
if(cash &lt; price){
System.out.println(&quot;Sorry, your cash is not enough. Please enter amount again&quot;);
// this is the part where I want to go back to &quot;amount of buyers cash&quot;
}
if(cash &gt;= price){
int change = cash - price;
System.out.println(&quot;\nYour change is P&quot; + change);
System.out.println(&quot;\nThank you! Come again next time.&quot;);
System.exit(0);
}
}
}}
</details>
# 答案2
**得分**: 0
```java
我认为你只需要将if条件更改为while条件,同时在检查价格是否大于现金。
package prelim.activities;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
//选择产品
System.out.println("欢迎光临 Aling Nena 的商店。你想买什么?");
System.out.println("\n输入 1 选择牛奶");
//扫描仪和产品类型的布尔值
Scanner scanner = new Scanner(System.in);
String product = scanner.nextLine();
boolean milk = true;
//牛奶的if语句
while(milk){
System.out.println("\n每个的价格是 15 元,请问你想买多少?");
//扫描仪和产品价格的布尔值
Scanner scanner_price = new Scanner(System.in);
int amount = scanner_price.nextInt();
int price = 15 * amount;
System.out.println("\n你的产品总价为 " + price + " 元");
//买家付的现金金额
System.out.println("\n请输入付款金额");
Scanner money = new Scanner(System.in);
int cash = money.nextInt();
//不足现金的情况
while(cash < price){
System.out.println("抱歉,你的现金不足。请重新输入金额");
// 这是我想要返回到 "买家付的现金金额" 的部分
Scanner money1 = new Scanner(System.in);
cash = money1.nextInt();
}
if(cash >= price){
int change = cash - price;
System.out.println("\n找零 " + change + " 元");
System.out.println("\n谢谢!下次再来。");
System.exit(0);
}
}
}
}
英文:

I think you just need to change the if condition to while condition while checking if the price greater than the cash.

package prelim.activities;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
//choosing of product
System.out.println(&quot;Welcome to Aling Nena&#39;s Store. What do you want to buy?&quot;);
System.out.println(&quot;\nEnter 1 for Milk&quot;);
//scanners and booleans for product type
Scanner scanner = new Scanner(System.in);
String product = scanner.nextLine();
boolean milk = true;
//if statements for milk
if(milk){
System.out.println(&quot;\nThis will cost P15 each, how much would you like to buy?&quot;);
//scanners and booleans for product price
Scanner scanner_price = new Scanner(System.in);
int amount = scanner_price.nextInt();
int price = 15 * amount;
System.out.println(&quot;\nYour product costs P&quot; + price);
//amount of buyers cash
System.out.println(&quot;\nPlease enter how much will you pay&quot;);
Scanner money = new Scanner(System.in);
int cash = money.nextInt();
//if statement for not enough cash
while(cash &lt; price){
System.out.println(&quot;Sorry, your cash is not enough. Please enter amount again&quot;);
// this is the part where I want to go back to &quot;amount of buyers cash&quot;
Scanner money1 = new Scanner(System.in);
cash = money1.nextInt();
}
if(cash &gt;= price){
int change = cash - price;
System.out.println(&quot;\nYour change is P&quot; + change);
System.out.println(&quot;\nThank you! Come again next time.&quot;);
System.exit(0);
}
}
}}

huangapple
  • 本文由 发表于 2020年8月28日 01:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63621691.html
匿名

发表评论

匿名网友

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

确定