2017-11-17 8 views
0

왜 내 addRecord() 메서드가 반복되는지 알 수 없습니다. 구체적으로 말하자면, 메소드의 my if/else의 첫 번째 else에서 "Enter Sales ID >>"에서만 반복됩니다. 코드의 끝에서 programMenu() 메서드를 호출하려고 시도했지만 컴파일러는이 두 가지를 모두 실행하려고 계속 시도합니다. 이 방법은 switch case 문에서 "a"가 수신 될 때만 실행되어야하지만 "a"와 "c"가 모두 입력 될 때 실행되고 "Enter Sales ID >>"에서 동일한 else 문으로 반복됩니다 .내 메서드가 루프없이 반복되는 이유는 무엇입니까?

내 방법은 다음과 같습니다

public static int addRecord(Salesperson[]salesPeopleArray, int 
numOfSalesPpl) 
    { 
     String name=null; 
     String idNum; 
     double salesAmt = 0; 
     String response; 
     numOfSalesPpl = 0; 
     final int MAX_LIMIT=20; 
     final int ID_NUM_LIMIT = 8; 

     if(numOfSalesPpl == MAX_LIMIT) 
     { 
      System.out.print("Database has reached capacity."); 
      System.out.print(" Please delete a record before "); 
      System.out.println("adding to the database."); 
     } 
     else 
     System.out.print("Enter Sales ID:>> "); 
     idNum = userInput.nextLine(); 

     if(idNum.length() != ID_NUM_LIMIT) 
     { 
      System.out.println(">>>>>Sales ID must be 8 digits<<<<<<"); 
      programMenu(); 
     } 
     else 
     { 
      System.out.print("Please enter name: >> "); 
      name = userInput.nextLine(); 
      System.out.print("Sales amount : >> "); 
      salesAmt = userInput.nextDouble(); 
      salesPeopleArray[numOfSalesPpl] = new Salesperson(name, 
idNum,salesAmt);  
      ++numOfSalesPpl; 
      userInput.nextLine(); 
      System.out.print("Do you want to display database> Y/N >>"); 
      response = userInput.nextLine(); 
      if(response.equals("y"))  

      displayDatabase(salesPeopleArray, numOfSalesPpl); 
    } 

    return numOfSalesPpl; 
    } 

내 스위치 케이스는 다음과 같습니다

여기
public static String programMenu() 
    { 
     String selection; 
    do 

     { 
     System.out.println("===================================="); 
     System.out.println("(A)dd a Record"); 
     System.out.println("(C)hange a Record"); 
     System.out.println("(Q)uit Database"); 
     System.out.print("Enter selection: >> "); 
     selection = userInput.nextLine(); 
      if(!selection.equalsIgnoreCase("a") && 
!selection.equalsIgnoreCase("c") && !selection.equalsIgnoreCase("q")) 
      System.out.println(">>>>Invalid Selection<<<<");   
     System.out.println("===================================="); 

     } 
     while(!selection.equalsIgnoreCase("a") && 
!selection.equalsIgnoreCase("c") && !selection.equalsIgnoreCase("q")); 

     return selection; 
} 

    } 

답변

0

코드 :

String selection = programMenu(); 
    boolean loop = true; 


    do 
     { 
      switch(selection) 
      { 
       case "A": 
       case "a":      
        addRecord(salesPeopleArray, numOfSalesPpl); 
        break; 

       case "C": 
       case "c": 
        changeRecord(numOfSalesPpl, salesPeopleArray); 
        break; 

       case "Q": 
       case "q": 
       System.out.print("You Are Leaving Database"); 
       System.out.print(" "); 
       loop = false; 
       break; 
      } 
     } 
    while(loop); 

} 

내 programMenu() 메소드는 다음과 같습니다 "선택"은 이미 "A"값을 가지고 있기 때문에 루프 한 후 사용자 입력 ("A/a")을 한 번만 받는다. 당신이 이 줄을 넣으려면 "문자열 선택 = programMenu();"루프 안에 루프 시간이 사용자 입력을 요청하면 스위치 케이스 문을 변경할 수 있습니다.

boolean loop = true; 
    do 
     { 
      String selection = programMenu(); 
      switch(selection) 
      { 
       case "A": 
       case "a":      
        addRecord(salesPeopleArray, numOfSalesPpl); 
        break; 

       case "C": 
       case "c": 
        changeRecord(numOfSalesPpl, salesPeopleArray); 
        break; 

       case "Q": 
       case "q": 
       System.out.print("You Are Leaving Database"); 
       System.out.print(" "); 
       loop = false; 
       break; 
      } 
     } 
    while(loop);  
}