그래서 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>*/
Scanner.nextLine()은 문자열을 반환합니다. http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html을 참조하십시오. – beartech1