【问题标题】:counter for errors错误计数器
【发布时间】:2016-06-13 11:05:22
【问题描述】:

当用户注销时,用户可以选择重新登录。对于重新登录:如果用户输入错误的密码 3 次,则程序终止。

System.out.println("You have logged out");
System.out.print("Please Enter Pin: ");  
pin2 = sc.nextInt();
while (pin != pin2){
   while (ctr < 2){
   System.out.print("Please Enter Pin: ");
   pin2 = sc.nextInt();
   ctr++;
   }
 }

【问题讨论】:

  • 您能否删除所有不必要的内容并准确告诉我们您的问题是什么?也许你想要的是while (pin == pin2)
  • 这段代码有什么问题?? 5点后它不会停止??
  • 摆脱那个标签!现在就去做……颤抖。如果要结束循环,请将布尔值设置为 false,您可以在 while 条件中检查它。
  • 使用标签和/或转到代表。命名中断通常被认为是不好的做法,有时甚至是有害的(至少在 java + 许多其他高级语言中)。在极少数情况下他们别无选择,在我使用 Java 的 10 多年中,我从未觉得有必要使用它。
  • @jankigadhiya 它确实在 5 处停止,但我想做的是在用户选择“1”之后,程序将再次询问用户想要执行的事务。 “你想做什么事务......”“1”“(1中的代码)”“你想做什么事务......”“3”

标签: java loops switch-statement


【解决方案1】:

如果我正确理解您的问题,您会想要这样的:

while (pin == pin2) {
        System.out.println("What would you like to do?");
        System.out.println("1 - Check Balance");
        System.out.println("2 - Deposite");
        System.out.println("3 - Withdraw");
        System.out.println("4 - Change Pin");
        System.out.println("5 - End Transaction");
        sel = sc.nextInt();

        switch (sel) {
            case 1:
                System.out.println("Your current balance is " + bal);
                break;
            case 2:
                System.out.println("How much would you want to deposite? ");
                dep = sc.nextInt();
                bal = dep + bal;
                System.out.println("Your new current balance is " + bal);
                break;
            case 3:
                System.out.println("How much would you want to Withdraw? ");
                with = sc.nextInt();

                if (with > bal) {
                    System.out.println("You do not have that amount on your                           account! Please enter again.");
                } else {
                    System.out.println("You withdrew " + with);
                    bal = bal - with;
                    System.out.println("Your new current balance is " + (bal));
                }
                break;
            case 4:
                System.out.print("Please enter a new pin: ");
                pin = sc.nextInt();

                System.out.println("Please verify your new pin: ");
                pin2 = sc.nextInt();
                break;
            case 5:
                System.out.println("Please Enter Pin: ");
                pin = sc.nextInt();
                break;
            default:
                break;
        }
    }

基本上,我已经删除了loop 标签,没有必要,我认为这是一种不好的风格。我还更改了 while 条件,因此只要用户输入与他在开始时确认的完全相同的引脚,程序就会运行。此外,我认为最好在打印指令后读取sel 的值,而不是像之前那样。

【讨论】:

  • 请输入新密码:1234 请验证您的新密码:1234 您想做什么? 1 - 检查余额 2 - 存款 3 - 取款 4 - 更改密码 5 - 结束交易 1 您当前的余额是 10000 您要存入多少?
  • 这就是我运行它时发生的事情。它实际上和我的一样。我不明白为什么它会自动转到案例 2,而不是再次询问用户想要做什么
【解决方案2】:

就像 cmets 中所说的那样,避免使用标签和/或 goto rep。 使用类似的东西:

import java.util.Scanner;

public class AtmMachine {

public static void main(String[] args){

    int actualPin = -1;
    int sel = 0, pin, pin2, check, ctr = 0, dep, with, bal = 10000;
    Scanner sc = new Scanner(System.in);

    while(actualPin == -1)
    {
    System.out.print("Please enter a new pin: ");
    pin = sc.nextInt();

    System.out.print("Please verify your new pin: ");
    pin2 = sc.nextInt();
    if(pin == pin2) actualPin = pin;
    else System.out.println("Error");
    }

    boolean logged = false;

    while (true){

    if(logged){
    System.out.println("What would you like to do?");
    System.out.println("1 - Check Balance");
    System.out.println("2 - Deposite");
    System.out.println("3 - Withdraw");
    System.out.println("4 - Change Pin");
    System.out.println("5 - End Transaction");
    sel = sc.nextInt();
    switch (sel){
        case 1: 
            System.out.println("Your current balance is "+ bal);
            break;
        case 2:
            System.out.println("How much would you want to deposite? ");
            dep = sc.nextInt();
            bal= dep+bal;
            System.out.println("Your new current balance is "+ bal);
            break;
        case 3:
            System.out.println("How much would you want to Withdraw? ");
            with = sc.nextInt();

            if(with > bal){
            System.out.println("You do not have that amount on your account! Please enter again.");
            }
            else{
            System.out.println("You withdrew "+ with);
            bal = bal-with;
            System.out.println("Your new current balance is "+ (bal));
            }
            break;
        case 4:
            System.out.print("Please enter a new pin: ");
            pin = sc.nextInt();

            System.out.println("Please verify your new pin: ");
            pin2 = sc.nextInt();
            if(pin == pin2) actualPin = pin;
            else System.out.println("Error");
            break;
        case 5:
            logged = false;
            break;
        default:
            break;
    }
    else{
        System.out.println("Please Enter Pin: ");
        sel = sc.nextInt();
        logged = actualPin == sel;
    }
    }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    相关资源
    最近更新 更多