2014-03-04 85 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; 
}