2015-01-21 2 views
-1

그래서 while 루프에 문제가 있습니다 (잠시있었습니다). 사용자 입력이 "y"인 동안 스캐너 개체를 설정하려고 시도하는 동안 컴파일 오류가 발생합니다. 어떻게 작동 시키나요? 선택 = Scanner.nextLine스캐너 while 루프

import java.util.Scanner; 

/** 
* Computes the area and perimeter of selected figures. 
*/ 
public class ComputeAreaAndPerimeter { 

    /** 
    * The main program performs the following steps. 
    * 1. It asks the user for the type of figure. 
    * 2. It asks the user for the characteristics of that figure. 
    * 3. It computes the perimeter. 
    * 4. It computes the area. 
    * 5. It displays the result. 
    * @param args The command line arguments -- not used 
    */ 
    public static void main(String args[]) { 
     Shape myShape; 
     double perimeter; 
     double area; 
     myShape = getShape(); // Ask for figure type 
     myShape.readShapeData(); // Read the shape data 
     perimeter = myShape.computePerimeter(); // Compute perimeter 
     area = myShape.computeArea(); // Compute the area 
     displayResult(area, perimeter); // Display the result 
     System.exit(0); // Exit the program 
     String choice; 
    } 

    /** 
    * Ask the user for the type of figure. 
    * @return An instance of the selected shape 
    */ 

     public static Shape getShape() { 
      Scanner in = new Scanner(System.in); 
      Scanner choice = new Scanner(System.in); 
      System.out.println("Do you want to run the program? Y or N"); 
      choice = Scanner.nextLine(); //Error is here 
      while (choice.equalsIgnoreCase("y")) 
      { 
       System.out.println("Enter C for circle"); 
       System.out.println("Enter R for Rectangle"); 
       System.out.println("Enter T for Right Triangle"); 
       String figType = in.next(); 
       if (figType.equalsIgnoreCase("c")) { 
        return new Circle(); 
       } else if (figType.equalsIgnoreCase("r")) { 
        return new Rectangle(); 
       } else if (figType.equalsIgnoreCase("t")) { 
        return new RtTriangle(); 
       } else { 
        return null; 
       } 
       System.out.println("Run prgram again?"); 
      } 
      System.out.println("Goodbye"); 
    } 

    /** 
    * Display the result of the computation. 
    * @param area The area of the figure 
    * @param perim The perimeter of the figures 
    */ 
    private static void displayResult(double area, double perim) { 
     System.out.printf("The area is %.2f%nThe perimeter is %.2f%n", 
       area, perim); 
    } 
} 
/*</listing>*/ 
+0

Scanner.nextLine()은 문자열을 반환합니다. http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html을 참조하십시오. – beartech1

답변

3
Scanner in = new Scanner(System.in); 
Scanner choice = new Scanner(System.in); 
System.out.println("Do you want to run the program? Y or N"); 
choice = Scanner.nextLine(); //Error is here 
while (choice.equalsIgnoreCase("y")) 

문제는 어디 잘못 Scanner를 사용하는에 오류가 있습니다. 같은

그것은해야 뭔가 :

String userChoice = choice.nextLine(); 
while (userChoice.equalsIgnoreCase("y")) { 
    // do your loop logic 
    // then, to be structured, at the end of your loop, do... 
    System.out.print("Do you want to run the program again? Y or N: "); 
    userChoice = choice.nextLine(); 
} 

이됩니다 다시 테스트 각 루프는 후 조건 :

String userChoice = choice.nextLine(); 

보다 효과적으로 루프를 사용하려면, 당신은 뭔가를 시도 할 수 있습니다 운영. 사용자가 "n"을 선택하면 루프가 자연스럽게 종료됩니다.

1

하나만 Scanner 당신이 입력하라는 메시지를 표시 할 때마다 다음, 당신이 scanner.next()를 호출 할 수 있으며 입력에 대기 입력 스트림

Scanner scanner = new Scanner(System.in);

그리고에서 읽을 선언해야합니다.

String input = scanner.next();

이 수행되는 예는 수 :

Scanner in = new Scanner(System.in); 
System.out.println("Do you want to run the program? Y or N"); 
String choice = in.nextLine(); 
while (choice.equalsIgnoreCase("y")) 
{ 
    //rest of code 

당신은 여분의 Scanner 만들어 낸 것은 당신이 필요로하지 않는 :

public static Scanner scanner = new Scanner(System.in); 

public static void main(String[] args) throws JSONException { 

    String input = scanner.next(); 
    System.out.println(input); 

} 
0

귀하의 코드는이 있어야한다 . nextLine()도 잘못 표시되었습니다.