错误的计数器

时间:2016-06-13 11:05:22

标签: java loops switch-statement

当用户注销时,用户可以选择重新登录。重新登录:如果用户错误地输入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++;
   }
 }

2 个答案:

答案 0 :(得分: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的值,而不是像之前那样。

答案 1 :(得分:0)

与评论中所说的一样,避免使用标签和/或转到代表。 使用类似的东西:

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;
    }
    }
    }
}