2017-01-17 3 views
1

나는 서점을위한 프로그램을 작성하여 비즈니스를 예측할 수있는 과제를 가지고 있습니다. 이를 위해 사용자는 도서 코드, 도서 단가, 현재 도서 수, 장래 학급 등록 등과 같은 모든 종류의 입력을 요구합니다. 그러나, 나는 사용자가 장래 등록을위한 부적당 한 입장이있을 때, 사용자가 무효 입장을 입력 할 때 문제점을 직면하고있다, 프로그램은 동일한 질문을 다시 요구하기 대신에 처음부터 질문을 묻는 시작한다.자바에서 스캐너 사용 관련 문제

누구든지 내가 뭘 잘못하고 있는지 알아낼 수 있습니까? 여기 내 코드가있다.

미리 감사드립니다. 당신은 루프에서 질문을하고

public static void main(String[] args) { 

    Scanner sc = new Scanner(System.in); 

    System.out.println("Welcome to College Store Sale Class."); 
    System.out.println("Please enter all the info below as asked."); 
    System.out.println("Write either 'Continue' or 'Exit' to continue or exit program respectively. "); 



    double bookCost; 
    int bookAvailable; 
    String ending = sc.next(); 
    int prosepectiveEnrollment; 
    String rOrO;  
    String nOrU; 

    while(!ending.equals("exit")){ 



     System.out.print("Please enter the cost of book ($>=0). "); 
     bookCost = sc.nextDouble(); 

     if(bookCost >= 0){ 

      System.out.print("Please enter the number of books available (n>=0). "); 
      bookAvailable = sc.nextInt(); 

      if(bookAvailable >= 0){ 

       System.out.print("Please enter prospective class enrollement > 0: ");     
       prosepectiveEnrollment = sc.nextInt(); 

       if(prosepectiveEnrollment > 0){ 

        System.out.print("Is the book Required or Optional (ignoreCase)? "); 
        rOrO = sc.next(); 

        if((rOrO.equalsIgnoreCase("Required") || rOrO.equalsIgnoreCase("Optional"))){ 

         System.out.print("Is the book New or Used? Enter in 'N' or 'U' format. "); 
         nOrU = sc.next(); 
         if((nOrU.equalsIgnoreCase("New") || nOrU.equalsIgnoreCase("Used"))){ 

          System.out.println("Thanks for correct info, below is your result"); 

         } 

         else{ 

          System.out.println("(E) Please enter either 'New' or 'Used'. "); 
         } 

        } 
        else{ 

         System.out.println("(D) Please enter either 'Required' or 'Optional'. "); 
        } 

       } 
       else{ 

        System.out.println("(C) Number of prospective class enrollement must be > 0."); 
       } 
      } 
      else{ 

       System.out.println("(B) Number of books should be >= 0."); 
      } 

     } 

     else{ 

      System.out.println("(A) Cost should be >= 0. "); 
     } 
    } 

    if(ending.equalsIgnoreCase("exit")) 
     System.out.println("See ya later..!! Bbyee.."); 
    sc.close(); 
} 
+0

'내가 작동 있으면 알려 주시기 수 있습니다 사용자가 잘못된 항목을 입력 할 때 직면하는 문제, 내가 뭘 잘못했는지 알 수있는 사람이 누구든지, 우리가 그 문제를 알지 못하면 무엇이 잘못되었는지 말할 수 없습니다. – SomeJavaGuy

+1

사용자가 'int'대신 "A"를 입력하면 main에서 벗어난 예외가 발생해야합니다. 그게 니가 본거야? – Scovetta

+0

@Scovetta 아니요, '도서 비용 ($> = 0)을 입력하십시오. ** 1 **' '사용 가능한 책 수를 입력하십시오 (n> = 0). ** 1 **' '예상되는 학급 등록을 입력하십시오.> 0 : ** - 1 **' '_ (C) 예정 학급 등록 수> 0_' '도서 비용을 입력하십시오 ($> = 0). ' 반복되는 패턴이 여기에 표시됩니까? 사용자가 예상 등록에 대해 잘못된 입력을 한 경우 동일한 질문을하기보다는 책의 비용을 다시 입력해야하는지 묻습니다. – Aayush

답변

0

행운을 빕니다, 당신은 이런 식으로 할 수있는 일이 당신이

public class Questionaire { 

    public static void main(String[] args) { 

     double bookCost = 0.0; 
     int bookAvailable = 0; 
     int prosepectiveEnrollment = 0; 
     String rOrO = "";  
     String nOrU = ""; 

     try (Scanner sc = new Scanner(System.in)){ 
      System.out.println("Welcome to College Store Sale Class."); 
      System.out.println("Please enter all the info below as asked."); 
      System.out.println("Write either 'Continue' or 'Exit' to continue or exit program respectively. "); 

      String ending = sc.next(); 
      System.out.println("Your response "+ending); 

      while(!ending.equalsIgnoreCase("exit")){ 

       bookCost = promptQuestion("Please enter the cost of book ($>=0).", param -> { 
        return param >= 0; 
       }, bookCost,sc); 
       System.out.println("Your response "+bookCost); 

       bookAvailable = promptQuestion("Please enter the number of books available (n>=0). ", param -> { 
        return param >= 0; 
       }, bookAvailable, sc); 
       System.out.println("Your response "+bookAvailable); 

       prosepectiveEnrollment = promptQuestion("Please enter prospective class enrollement > 0: ", param -> { 
        return param > 0; 
       }, prosepectiveEnrollment, sc); 
       System.out.println("Your response "+prosepectiveEnrollment); 

       rOrO = promptQuestion("Is the book Required or Optional (ignoreCase)? ", param -> { 
        return param.equalsIgnoreCase("Required") || param.equalsIgnoreCase("Optional"); 
       }, rOrO, sc); 
       System.out.println("Your response "+rOrO); 

       nOrU = promptQuestion("Is the book New or Used? Enter in 'N' or 'U' format. ", param -> { 
        return (param.equalsIgnoreCase("New") || param.equalsIgnoreCase("Used") 
          || param.equalsIgnoreCase("N") || param.equalsIgnoreCase("U")); 
       }, nOrU, sc); 
       System.out.println("Your response "+nOrU); 

       System.out.println("Write either 'Continue' or 'Exit' to continue or exit program respectively. "); 
       ending = sc.next(); 
       System.out.println("Your response "+ending); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    static Integer promptQuestion(String question, Validation<Integer> validation, Integer input, Scanner sc) { 
     do { 
      System.out.println(question); 
      input = sc.nextInt(); 
     } while(!validation.validate(input)); 
     return input; 
    } 

    static Double promptQuestion(String question, Validation<Double> validation, Double input, Scanner sc) { 
     do { 
      System.out.println(question); 
      input = sc.nextDouble(); 
     } while(!validation.validate(input)); 
     return input; 
    } 

    static String promptQuestion(String question, Validation<String> validation, String input, Scanner sc) { 
     do { 
      System.out.println(question); 
      input = sc.next(); 
     } while(!validation.validate(input)); 
     return input; 
    } 

    @FunctionalInterface 
    private static interface Validation<T> { 
     boolean validate(T t); 
    } 
} 
+0

답장을 보내 주셔서 감사합니다. 그러나 소개 수업 인만큼 자세한 내용을 아직 공부하지 않았으므로 아직 수행하지 않았습니다. 예를 들어 유효성 확인에서 정수 Integrity가 의미하는 바를 조사하지 않았습니다 (예 : ). – Aayush

0

-Aayush

. 조건 중 하나가 실패하면 프로그램은 모든 후속 조건을 지나치며 (잘못되었을 경우) LOOP을 다시 시작합니다. 이 문제를 해결하려면 루프 내에서 각 조건이 발생할 수 있습니다.

(거짓 조건이 답) 동안 같은 뭔가 : 자신을 도전하기 위해 다시

문의 (향상), 가능한 한 몇 줄에 코드를 유지! 코딩 여행 : 나는이 문제를 해결하기 위해 노력했다

+0

그게 내가 처음에 한 일입니다. 중첩 된 while 루프를 사용했지만 작동하지 않습니다. 중첩 된 while 루프의 스크린 샷을 보여줄 수는 있지만 첨부 파일 버튼을 찾을 수 없으므로 여기에 그림을 게시하는 방법을 모르겠습니다. – Aayush