2014-03-04 87 views
0

嘿傢伙我有以下的代碼,我會在下面發佈。不能通過一個while循環

我不能通過第一個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 

} //類的結束

+0

你談論'而{'while循環? – AntonH

+0

您是否在使用Eclipse之類的IDE?在調試器中瀏覽代碼,看看發生了什麼。 – Andrew

回答

2

你錯過了這條線option=stroption;,之後添加它首先while循環是這樣的:

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在你開始使用它之前,你必須把它分配給某個值。

+0

你可以更好地解釋發生了什麼事。 OP似乎不理解控制正在一路走下。 –

0

while循環之後,您正在使用不正確的變量名稱來檢查用戶輸入了什麼選項。

用戶輸入存儲在stroption中,但您正在檢查option。在您的if-else條件中將名稱更改爲stroption,它應該可以工作!

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 
0

嘗試讀取輸入的一條線,然後解析它作爲一個整數(!stroption = 1個&& stroption = 2!)

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