2013-05-03 2 views
0

나는 은행을 만들려고 노력하고 있는데, 나는 스캐너 옵션을 만들고있다. 1을 누르면 돈을 인출 할 수있다. 2를 누르면 돈을 인출 할 수있다. 그러나 2 또는 3을 누르면 아무 것도하지 않지만 다른 경우 시도했지만 더 많은 오류가 발생하고 그 차이를 알 수 없습니다.내 변수가 작동하지 않는 이유는 무엇입니까?

------ 참고 ------ 중첩을 시도했지만 (문구가있는 경우) 버튼 근처에 구문 오류가 나타납니다 (어디에서 표시했는지) 그래서 확실하지 않습니다. 그 문제를 해결하는 방법

import java.util.Scanner; 




public class Bank { 




    public static void main (String [] args) { 
    System.out.println("Welcome To Harry's Bank"); 


    //Pin System 
     System.out.println("Please Enter Your Bank Pin."); 
    Scanner userInput = new Scanner (System.in); 
     int number; 
     int password = 7123; 
     int amount = 4000; 

     number = userInput.nextInt(); 

     if (number == password) { 
     System.out.println("Pin Accepted"); 
     System.out.println("You Have Now Entered Harry's Bank!"); 
     System.out.println("Press The Number Of The Option You Would Like."); 
     System.out.println("1.Withdraw Money."); 
     System.out.println("2.Put In Money"); 
     System.out.println("3.Exit Bank"); 
     Scanner Options = new Scanner (System.in); 
     int option; 
     option = userInput.nextInt(); 

     if (option == 1) { 
     //Withdraw Money System 
     System.out.println("You Have £4000"); 
     System.out.println("How Much Would You Like To Take Out?"); 
     Scanner Input = new Scanner (System.in); 
     int numbere; 


     numbere = userInput.nextInt(); 
     if (numbere < 4000) { 

     int money = amount - numbere; 
      System.out.println("You Have Now £" + money); 
      System.out.println("Thank You For Banking At Harry's Bank!"); 
      System.out.println("Please Come Again!"); 
      }else{ 
       System.out.println("You Do Not Have Enough Money!"); 
      } 
     } 


     else if (option == 2) { 
      //AddMoney System 
      Scanner AddMoney = new Scanner (System.in); 
      int AddMoney1; 
      AddMoney1 = userInput.nextInt(); 
      int NewMoney = (amount + AddMoney1); 
      System.out.println("How Much Would You Like To Enter?"); 
      System.out.println("You Now Have " + NewMoney + "!"); 
      System.out.println("Thank You For Using Harry's Bank!"); 




     } 
     else if (option == 3) { 
      System.out.println(""); 
      System.out.println(""); 
      System.out.println(""); 
      System.out.println(""); 
      System.out.println(""); 
      System.out.println(""); 
      System.out.println(""); 
      System.out.println("Goodbye!"); 
     } 
     }else{ 
      System.out.println("Next Time Only Press 1,2 or 3!"); 
     } 
     //error Here For Else "Syntax error on token "else", { expected" 
     }else{ 
       System.out.println("Pin Declined!"); 

      } 



      } 








     } 
+6

적절한 코드 들여 쓰기가 솔루션을 즉시 보여줬을 것입니다. –

답변

1

제 3 및 제 4 if 문이 도달 할 수 없기 때문에. 1을 누르지 않으면 절대로 도달하지 않습니다.

8

대괄호가 꺼져 있습니다. 당신은

if(option ==1) 
{ 
    if(option == 2) 
    { 
     //stuff 
    } 

    if(option == 3) 
    { 
     //stuff 
    } 
} 

이동하는 IFS 밖으로 문은 "옵션 == 1"당신이 도달 할 수 있도록 경우 옵션 2 또는 3 안에 중첩 된 경우가 한 첫

0

당신이이해야 측면이 else of chain of else

관련 문제