如何编写 while 循环

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

how to write while loop

问题

我目前正在用Java编写一个简单的ATM程序。
我想编写一个循环,当用户输入错误的PIN时,它会提示用户重新输入,直到PIN匹配为止。当PIN匹配时,它将显示主菜单。

我尝试了一下,但我不知道该如何修复它。

while (userPIN != savedPIN) {
    System.out.print("请输入正确的PIN:");
    Scanner again = new Scanner(System.in);
    int pass = again.nextInt();
    break;
}
英文:

I am currently making a simple ATM program in java.
I want to write a while loop where when user enters wrong pin it will prompt the user to enter again until the pin is matched. When the pin is matched, it will display the main menu.

I tried by myself, but I don't know how to fix it.

while(userPIN != savedPIN)	
	{
     System.out.print("Please enter your correct PIN : ");
	 Scanner again = new Scanner(System.in);
	 int pass = again.nextInt();
     break;
    }	 

答案1

得分: 0

好的,以下是您要翻译的内容:

错误2个:

  1. 您测试 userPIN != savedPIN,但您将该值接受到名为 pass 的变量中,却没有进行任何操作。

  2. 在第一个循环中去掉 break,否则它将总是在循环之前退出。

应该像这样:

while(pass!= savedPIN)  
{
    System.out.print("请输入正确的PIN:");
    Scanner again = new Scanner(System.in);
    int pass = again.nextInt();
}
英文:

Ok 2 errors:

1)you test userPIN != savedPIN but you accept the value into variable pass with which you do nothing.

2)remove the break in the first loop it will always exit without looping.

it should look like :

while(pass!= savedPIN)  
    {
     System.out.print("Please enter your correct PIN : ");
     Scanner again = new Scanner(System.in);
     int pass = again.nextInt();
    }

答案2

得分: 0

移除break;语句并按以下方式更新userPIN的新PIN:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int savedPIN = 4444;
        Scanner input = new Scanner(System.in);
        System.out.println("输入密码");
        int userPIN = input.nextInt();
        double withdraw = 0.0, amount = 0.0, deposit = 0.0;

        while (userPIN != savedPIN) {
            System.out.print("请输入正确的PIN码:");
            Scanner again = new Scanner(System.in);
            userPIN = again.nextInt();
        }

        while (userPIN == savedPIN) {

            System.out.println(" 1 - 查询余额 \n 2 - 取款 \n 3 - 存款 \n 0 - 退出 ");

            Scanner inputNum = new Scanner(System.in);
            int number = inputNum.nextInt();
            switch (number) {
                case 1:
                    System.out.println("当前余额为 $" + amount);
                    break;

                case 2:
                    System.out.println("输入取款金额:");
                    withdraw = input.nextDouble();
                    if (amount >= withdraw) {
                        amount = amount - withdraw;
                        System.out.println("当前余额为 $" + amount);
                    } else {
                        System.out.println("余额不足");
                    }
                    break;

                case 3:
                    System.out.println("输入存款金额:");
                    deposit = input.nextDouble();
                    amount = amount + deposit;
                    System.out.println("当前余额为 $" + amount);
                    break;

                case 0:
                    System.exit(4);
            }

        }

    }
}
英文:

Remove the `break;' statement and update userPIN with the new pin as follows:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int savedPIN = 4444;
Scanner input = new Scanner(System.in);
System.out.println("Enter password");
int userPIN = input.nextInt();
double withdraw = 0.0, amount = 0.0, deposit = 0.0;
while (userPIN != savedPIN) {
System.out.print("Please enter your correct PIN : ");
Scanner again = new Scanner(System.in);
userPIN = again.nextInt();
}
while (userPIN == savedPIN) {
System.out.println(" 1 - Inquire Balance \n 2 - Withdraw \n 3 - Deposit \n 0 - Quit ");
Scanner inputNum = new Scanner(System.in);
int number = inputNum.nextInt();
switch (number) {
case 1:
System.out.println("The current balance is $" + amount);
break;
case 2:
System.out.println("Enter the amount to withdraw : ");
withdraw = input.nextDouble();
if (amount >= withdraw) {
amount = amount - withdraw;
System.out.println("The current balance is $" + amount);
} else {
System.out.println("Insufficient balance");
}
break;
case 3:
System.out.println("Enter the amount to deposit : ");
deposit = input.nextDouble();
amount = amount + deposit;
System.out.println("The current balance is $" + amount);
break;
case 0:
System.exit(4);
}
}
}
}

huangapple
  • 本文由 发表于 2020年9月3日 18:38:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63721904.html
匿名

发表评论

匿名网友

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

确定