用户输入输入错误后循环代码

时间:2014-01-03 15:14:48

标签: java variables loops

所以我现在运行了一个非常基本和简单的代码,提示用户输入用户名,密码和帐号,然后将其与硬编码的用户名,密码和帐号进行比较。我正在寻找一种方法将代码添加到循环中,以便在程序终止之前,用户有三次正确输入用户名,密码和帐号(以便与硬编码的匹配)。如果他们错误地输入三次,程序将终止。有人可以帮帮我,我有点卡住了。我的代码目前将用户输入与硬编码变量进行比较,但在输入错误后终止。

由于

import java.io.*;
import java.lang.*;
import java.util.*;

public class Program4 {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) {

    final int Username = 3387;
    final int Password = 5183;
    final int AccountNumber = 22334455;

    final int EnteredUsername;
    System.out.println("Enter Username");
    EnteredUsername = console.nextInt();
    System.out.println("Username Entered is " + EnteredUsername);

    System.out.printf("%10s\n", " "); //Ignore these lines, they are just to make the console easier for me to read.

    final int EnteredPassword;
    System.out.println("Enter Password");
    EnteredPassword = console.nextInt();
    System.out.println("Password Entered is " + EnteredPassword);

    System.out.printf("%10s\n", " ");

    final int EnteredAccountNumber;
    System.out.println("Enter Account Number");
    EnteredAccountNumber = console.nextInt();
    System.out.println("Account Number Entered is " + EnteredAccountNumber);

    System.out.printf("%10s\n", " ");

    if (Username == EnteredUsername && (Password == EnteredPassword)
            && (AccountNumber == EnteredAccountNumber)) {

        System.out.println("Authentication Complete!");

    } else {
        System.out.println("Wrong Username, Password or Account Number. Please try again");
        System.exit(0);
    }

}

}

3 个答案:

答案 0 :(得分:1)

使用带有计数器和标志的do while循环。计数器应该用于3次尝试,而标志应该用于在条目有效时终止循环。这是一个示例:

  int attempts = 0; // increment this when wrong crendentials
  boolean authenitcated = false; // set to true when credentials are correct
  do { 
     // your code
   }while(attempts < 3 && authenticated == false)

编辑:试试这个

public static void main(String[] args) {

    final int Username = 3387;
    final int Password = 5183;
    final int AccountNumber = 22334455;

 int attempts = 0;
  boolean authenitcated = false;
  do { 
  final int EnteredUsername;
    System.out.println("Enter Username");
    EnteredUsername = console.nextInt();
    System.out.println("Username Entered is " + EnteredUsername);

    System.out.printf("%10s\n", " "); //Ignore these lines, they are just to make the console easier for me to read.

    final int EnteredPassword;
    System.out.println("Enter Password");
    EnteredPassword = console.nextInt();
    System.out.println("Password Entered is " + EnteredPassword);

    System.out.printf("%10s\n", " ");

    final int EnteredAccountNumber;
    System.out.println("Enter Account Number");
    EnteredAccountNumber = console.nextInt();
    System.out.println("Account Number Entered is " + EnteredAccountNumber);

    System.out.printf("%10s\n", " ");

    if (Username == EnteredUsername && (Password == EnteredPassword)
            && (AccountNumber == EnteredAccountNumber)) {

        System.out.println("Authentication Complete!");
        authenitcated = true;

    } else {
        System.out.println("Wrong Username, Password or Account Number. Please try again");
        attempts ++;

    }
   }while(attempts < 3 && authenticated == false)

}

答案 1 :(得分:0)

定义一个计数器。增加你的else块中的计数器。检查计数器是否= 3,然后退出。

答案 2 :(得分:0)

import java.util.Scanner;

public class StackFlow {




    public static void main(String[] args) {
      Scanner console = new Scanner(System.in);

        final int Username = 3387;
        final int Password = 5183;
        final int AccountNumber = 22334455;

         int EnteredUsername;
         int EnteredPassword;
         int EnteredAccountNumber;
        for(int s=0;s<=3;s++)
        {if (s<3)
        {System.out.println("Enter Username");
        EnteredUsername = console.nextInt();
        System.out.println("Username Entered is " + EnteredUsername);
        System.out.println("Enter Password");
        EnteredPassword = console.nextInt();
        System.out.println("Password Entered is " + EnteredPassword);
        System.out.println("Enter Account Number");
        EnteredAccountNumber = console.nextInt();
        System.out.println("Account Number Entered is " + EnteredAccountNumber);
        if (Username == EnteredUsername && (Password == EnteredPassword)
                && (AccountNumber == EnteredAccountNumber)) {
            System.out.println("Welcome");
            break;
        }
        else {
            System.out.println("Wrong Username, Password or Account Number. Please try again");
        }
        }
            else
        {
            System.out.println("You waste your 3 chances Bye-Bye");
        }
    }

}}