如何取消当前循环并继续执行循环。

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

How to cancel current loop, and continue a loop

问题

以下是您提供的代码的翻译部分:

class app {
    public static void main(String[] args) {
        long pin = 2927942074L;
        double balance = 10000.0;
        int attempts = 3;

        System.out.println("请输入您的PIN码。");

        while (attempts > 0) {
            Scanner keyboardpin = new Scanner(System.in);

            long input = keyboardpin.nextLong();

            if (input == pin) {
                System.out.println("正确");
                System.out.println("欢迎使用您的ATM");
                int a = 1;
                int b = 2;
                int c = 3;
                int d = 0;
                System.out.println(a + " - 查询余额");
                System.out.println(b + " - 取款");
                System.out.println(c + " - 存款");
                System.out.println(d + " - 退出");
                System.out.println("请选择您想要进行的操作。");
                Scanner menuselect = new Scanner(System.in);
                int menuinput = menuselect.nextInt();
                if (menuinput == a) {
                    System.out.println(balance);
                    continue;
                }
                if (menuinput == b) {
                    System.out.println("请输入取款金额。");
                    Scanner withdrawamount = new Scanner(System.in);
                    float withdrawbalance = withdrawamount.nextFloat();
                    double newBalance = (balance - withdrawbalance);
                    System.out.println("您的新余额为:" + newBalance);
                    if (newBalance < balance)
                        System.out.println("余额不足");
                }
            } else {
                System.out.println("错误");
                attempts--;
                System.out.println("您还有 " + attempts + " 次尝试机会。");
            }
            if (attempts == 0) {
                System.out.println("已达到最大尝试次数");
            }
        }
    }
}

如果您需要进一步解决代码中的问题或有其他疑问,请随时提问。

英文:

the code that i have written is for an app that is similar to a pseudo ATM, where if the user inputs the specified pin they get access to the account details. This is very early stages but the main issue i am running into is that when the correct pin is put in and the user is given prompt for what they want to do next through the Please select what you want to do line if the user is to select for example Inquire Balance they are presented with the current balance being 10000.0. The java then loops back to the original loop meaning the user has to put in the pin again. What i want to do is for the user to be presented with the menu again inquiring what they want to do after they are shown the information they need.

This is the code i have written.

class app {
public static void main(String[] args) {
long pin = 2927942074l;
double balance = 10000.0;
int attempts = 3;
System.out.println(&quot;Please enter your pin.&quot;);
while (attempts &gt; 0) {
Scanner keyboardpin = new Scanner(System.in);
long input = keyboardpin.nextLong();
if (input == pin) {
System.out.println(&quot;Correct&quot;);
System.out.println(&quot;Welcome to your ATM&quot;);
int a = 1;
int b = 2;
int c = 3;
int d = 0;
System.out.println(a + &quot; - Inquire Balance&quot;);
System.out.println(b + &quot; - Withdraw&quot;);
System.out.println(c + &quot; - Deposit&quot;);
System.out.println(d + &quot; - Quit&quot;);
System.out.println(&quot;Please select what you want to do.&quot;);
Scanner menuselect = new Scanner(System.in);
int menuinput = menuselect.nextInt();
if (menuinput == a) {
System.out.println(balance);
continue;
}
if (menuinput == b) {
System.out.println(&quot;Please enter a withdrawal amount.&quot;);
Scanner withdrawamount = new Scanner(System.in);
float withdrawbalace = withdrawamount.nextFloat();
double balancex = (balance - withdrawbalace);
System.out.println(&quot;Youre new balance is &quot; + balancex);
if (balancex &lt; balance)
System.out.println(&quot;Insufficient balance&quot;);
}
} else {
System.out.println(&quot;Wrong&quot;);
attempts--;
System.out.println(&quot;You have &quot; + attempts + &quot; attempts remaining.&quot;);
}
if (attempts == 0) {
System.out.println(&quot;Maximum number of attempts exceeded&quot;);
}
}
}
}

I've tried messing around with the continue function but am not sure if i am using it right. All feedback is appreciated.

答案1

得分: 1

> 我想要做的是,在向用户显示所需信息后,再次呈现菜单,询问他们想要做什么。

只需将您的 if (input == pin) 代码块修改如下:

           if (input == pin) {
                System.out.println("正确");
                System.out.println("欢迎使用 ATM");
                while(true){ // 除非选择“退出”,否则一直显示选项。
                    int a = 1;
                    int b = 2;
                    int c = 3;
                    int d = 0;
                    System.out.println(a + " - 查询余额");
                    System.out.println(b + " - 取款");
                    System.out.println(c + " - 存款");
                    System.out.println(d + " - 退出");
                    System.out.println("请选择您想要进行的操作。");
                    Scanner menuselect = new Scanner(System.in);
                    int menuinput = menuselect.nextInt();
                    if (menuinput == a) {
                        System.out.println(balance);
                        continue;
                    }
                    if (menuinput == b) {
                        System.out.println("请输入取款金额。");
                        Scanner withdrawamount = new Scanner(System.in);
                        float withdrawbalace = withdrawamount.nextFloat();
                        double balancex = (balance - withdrawbalace);
                        System.out.println("您的新余额为:" + balancex);
                        if (balancex < balance)
                            System.out.println("余额不足");
                    }

                    // 您遗漏了“c”、“d”和默认(无)实现。
                    // 添加“d”以实现退出功能。

                    if (menuinput == d) { 
                        break;
                    }
                }
            }
英文:

> What I want to do is for the user to be presented with the menu again inquiring what they want to do after they are shown the information they need.

Just change your if (input == pin) block as follows:

       if (input == pin) {
System.out.println(&quot;Correct&quot;);
System.out.println(&quot;Welcome to your ATM&quot;);
while(true){ //keep printing your options unless &quot;Quit&quot; is chosen.
int a = 1;
int b = 2;
int c = 3;
int d = 0;
System.out.println(a + &quot; - Inquire Balance&quot;);
System.out.println(b + &quot; - Withdraw&quot;);
System.out.println(c + &quot; - Deposit&quot;);
System.out.println(d + &quot; - Quit&quot;);
System.out.println(&quot;Please select what you want to do.&quot;);
Scanner menuselect = new Scanner(System.in);
int menuinput = menuselect.nextInt();
if (menuinput == a) {
System.out.println(balance);
continue;
}
if (menuinput == b) {
System.out.println(&quot;Please enter a withdrawal amount.&quot;);
Scanner withdrawamount = new Scanner(System.in);
float withdrawbalace = withdrawamount.nextFloat();
double balancex = (balance - withdrawbalace);
System.out.println(&quot;Youre new balance is &quot; + balancex);
if (balancex &lt; balance)
System.out.println(&quot;Insufficient balance&quot;);
}
//You were missing &quot;c&quot;, &quot;d&quot;, and default (neither) implementations.
//adding &quot;d&quot; for the exit functionality.
if (menuinput == d) { 
break;
}
}
} 

答案2

得分: 0

我认为这样更适合解决你的问题。我已经添加了额外的条件来检查输入是否不等于 pin。

 while (attempts > 0) {
long input = 0;
if (input != pin) {
Scanner keyboardpin = new Scanner(System.in);
input = keyboardpin.nextLong();
}
if (input == pin) {
System.out.println("正确");
System.out.println("欢迎来到您的ATM");
int a = 1;
int b = 2;
int c = 3;
int d = 0;
英文:

I think this would be better suited for solving your problem. I've added an extra if condition for checking when input is not equal to pin

 while (attempts &gt; 0) {
long input = 0;
if (input != pin) {
Scanner keyboardpin = new Scanner(System.in);
input = keyboardpin.nextLong();
}
if (input == pin) {
System.out.println(&quot;Correct&quot;);
System.out.println(&quot;Welcome to your ATM&quot;);
int a = 1;
int b = 2;
int c = 3;
int d = 0;

huangapple
  • 本文由 发表于 2020年8月29日 16:18:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63644886.html
匿名

发表评论

匿名网友

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

确定