无法通过一段时间循环

时间:2014-03-04 22:13:30

标签: java while-loop

嘿伙计们,我有以下代码,我将在下面发布。

我不能越过第一个while循环它只是说不正确?

我输入1或2并且说不正确可以帮助我吗?

/**
* @(#)Assignment2.java
* Display a menu to the user. Ask the user if they want to
* 1 : Log In 2 : Exit
* Ask the user for their Log In PIN
* If correct display the customers name and current balance
* Display a menu to the user. Ask the user if they want to
* 1 : Lodge Money 2 : Withdraw money 3 : Change pin 4 : Exit
* This program should keep going until the user enters 4
* @author Eoin Gilmartin
* @version 1.00 2014/2/04
*/

import java.util.Scanner;
public class Assignment2 { // start of class

public static void main (String args[]) { // start of main

    Scanner input = new Scanner(System.in); // needed to take inputs from the user

    //declare variables
    String customers[] = {"Amy","Tim","Bill","Joanne","Josh"};
    int pincode[] = {123,234,345,456,567};
    int balance[] = {400,225,100,360,600};
    int overdraft[] = {1,1,0,0,1};
    int lodgement=0, withdrawl=0, choice=0, option = 0, pin=0,pin2=0;
    int strchoice=0, stroption=0, strpin=0;
    int found =0;
    int user=0;
    String test="";
    String length;
    String menu = "\nPlease choose an option \n\n1: Log in\n2: Exit";
    String menu2 = "\n\n\t\tl: Lodge Money\n\t\t2: Withdraw money\n\t\t3: Change pin\n\t\t4: Exit\n";

    //welcome the user
    System.out.println("\n*** Welcome to the Bank of Eoin *** " + menu); // take initial input
    stroption = input.nextInt();

    System.out.println(stroption);
    while(stroption!=1 && stroption !=2){
            System.out.println("Error, please enter numbers 1- 2");
            // re-prompt the user
            System.out.println(menu);
            stroption =input.nextInt();
        }//end while

    if (option == 2){
        System.out.println("\nThank you for using my bank, goodbye\n");
        System.exit(0);
    } // end option=2

    else  if (option == 1){
        System.out.println("Please enter your PIN: ");
        strpin = input.nextInt();

    //while pin is not 3 digits long
        test = String.valueOf(pin);
    while(test.length()<3){
            System.out.println("Error, Invalid PIN, please try again: ");//error message
            strpin = input.nextInt();
        }//end while


    //check if input is a valid pin
    for (int i= 0; i < customers.length; i++){
        if (pincode[i] == pin ){
            System.out.println("Correct");
            System.out.println(customers[i]);
            user=i;
            found=1;

            }//end inner if
        }// end for loop
    } // end option=1

    if (found==0){
    System.out.println("\nIncorrect\nGoodbye");
    System.exit(0);
     }// end found = 0

    System.out.println("\nCustomer: " +customers[user] + "\nBalance " + balance[user]+" " +menu2);
    strchoice = input.nextInt();

    // while loop to validate input
    int x=1;
    while(x!=2){
    if(strchoice!=1 || strchoice!=2 || strchoice!=3|| strchoice!=4){

            System.out.println("Error, please enter numbers 1- 4");
            // re-prompt the user
            System.out.println(menu2);
            strchoice =input.nextInt();
        }//end if
        else
    x=2;
}//end while


    //loop to keep the menu2 appearing
    while (choice!= 4){

        if (choice ==1){
            System.out.print("\t\tEnter Lodgement: ");
            lodgement= input.nextInt();
            System.out.println("\t\tYour new balance is: " + (balance[user]+=lodgement));   //(+= is the same thing)
        }
        else if (choice == 2){
        if(overdraft[user]==0){
            System.out.print("\t\tEnter withdrawl: ");
            withdrawl= input.nextInt();
            if((balance[user]-withdrawl)<0){
            System.out.println("\t\tERROR INSUFFICIENT FUNDS");
            }
            }else if(overdraft[user]==1){
            System.out.print("\t\tEnter withdrawl");
            withdrawl=input.nextInt();
            System.out.println("\t\tYour new balance is: " + (balance[user]-= withdrawl));   
            }
        }
        else if (choice==3){
         System.out.print("\t\tPlease enter your new pin : ");
         pin2=input.nextInt();
         pincode[user]=pin2;
         System.out.println("\t\tYour new pin is: " + pincode[user]);
        }
        //prompt user
        System.out.println("\n\tPlease enter a number 1-4" + menu2);
        choice = input.nextInt();
    } //end of while loop



    System.out.println("\nThank you for using my bank, goodbye\n");

} // end of main

} //班级的结尾

3 个答案:

答案 0 :(得分:2)

你错过了这一行option=stroption;,在第一次循环之后添加它,如下所示:

 while(stroption!=1 && stroption !=2){
            System.out.println("Error, please enter numbers 1- 2");
            // re-prompt the user
            System.out.println(menu);
            stroption =input.nextInt();
        }//end while

    option = stroption;  // <<<---added this line 

    if (option == 2){
        System.out.println("\nThank you for using my bank, goodbye\n");
        System.exit(0);
    } // end option=2 ........

这是一个常见的编程错误,正如您在初始阶段看到的option=0,在您使用它之前必须将其分配给某个值。

答案 1 :(得分:0)

while循环后使用不正确的变量名来检查用户输入的选项。

用户输入存储在stroption中,但您正在检查option。在stroption条件中将名称更改为if-else,它应该有效!

    if (stroption == 2){
        System.out.println("\nThank you for using my bank, goodbye\n");
        System.exit(0);
    } // end option=2
    else  if (stroption == 1){
        System.out.println("Please enter your PIN: ");
        strpin = input.nextInt();

        //while pin is not 3 digits long
        test = String.valueOf(pin);
        while(test.length()<3){
            System.out.println("Error, Invalid PIN, please try again: ");//error message
            strpin = input.nextInt();
        }//end while

        //check if input is a valid pin
        for (int i= 0; i < customers.length; i++){
           if (pincode[i] == pin ) {
              System.out.println("Correct");
              System.out.println(customers[i]);
              user=i;
              found=1;
           }//end inner if
        }// end for loop
    } // end option=1

答案 2 :(得分:0)

尝试将输入作为一行读取,然后将其解析为整数:

Scanner input = new Scanner(System.in);
System.out.println("Please enter numbers 1- 2");
String stroption = input.nextLine();
int intoption = -1;
while (true){
    try{
        intoption = Integer.parseInt(stroption);
    }
    catch(NumberFormatException ex){
        System.out.println("Error, not an int!");
        intoption = -1;
    }
    if (intoption != 1 && intoption != 2)
        System.out.println("Error, please enter numbers 1- 2");
        stroption = input.nextLine();
    else
        break;
}