循环扫描仪

时间:2015-01-21 17:57:22

标签: java while-loop java.util.scanner

所以我遇到了一个while循环的问题(一段时间)。我在尝试将扫描程序对象设置为执行程序时遇到编译错误,而用户输入是" y"。我如何让它工作?错误在于choice = Scanner.nextLine

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>*/

3 个答案:

答案 0 :(得分: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();

要更有效地在循环中使用它,您可以尝试类似:

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();
}

这将在每个循环运行后重新测试条件。如果用户选择“n”,循环将自然退出。

答案 1 :(得分:1)

您应该只声​​明一个Scanner来读取输入流

Scanner scanner = new Scanner(System.in);

然后,只要您想提示输入,就可以拨打scanner.next()并等待输入。

String input = scanner.next();

这样做的一个例子可能是:

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

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

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

}

答案 2 :(得分:0)

您的代码应为:

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。你也错了nextLine()