2014-03-04 3 views
0

안녕 얘들 아 내가 아래에 게시 할 다음 코드가 있습니다.잠시 후에 루프를 얻을 수 없습니다.

나는 처음 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

당신이'동안에 대해 얘기하는 {'루프 동안? – AntonH

+0

Eclipse와 같은 IDE를 사용하고 있습니까? 디버거에서 코드를 단계별로 실행하고 무슨 일이 일어나고 있는지 확인하십시오. – Andrew

답변

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을 사용하기 전에 조건을 int로 지정하기 전에 값을 지정해야합니다.

+0

무슨 일이 일어나는지 더 잘 설명 할 수 있습니다. 영업 이익은 통제가 완전히 떨어지는 것을 이해하지 못하는 것처럼 보입니다. –

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; 
} 
관련 문제