简体   繁体   English

Java do while循环回到应用程序的开头

[英]Java do while loop back to the beginning of application

This question is a follow-up from this link Java SE do while loop issues 此问题是此链接的后续文章Java SE while循环问题

This is an application asking for a room's name then second prompt to ask for its dimensions to perform some calculations. 这是一个要求输入房间名称的应用程序,然后第二个提示要求输入房间尺寸以执行一些计算。 right after everything has been done, the application should be able to return back to the first prompt asking for the name of room etc. 一切完成后,应用程序应该能够返回到第一个提示,询问房间名称等。

class Room
{
private String name;
private double length;
private double width;
private double height;

public Room(String r, double l, double w, double h)
{
    name = r;
    length = l;
    width = w;
    height = h;
}


public String getName()
{
    return name;
}

public double getLength()
{
    return length;
}

public double getWidth()
{
    return width;
}

public double getHeight()
{
    return height;
}

public double getArea()
{
    double area = length*width;
    return area;
}

public double getVolume()
{
    double volume = length*width*height;
    return volume;  
}

public void setName(String s)
{
    name = s;
}
}

And my main method running the application. 和我运行应用程序的主要方法。

import java.util.Scanner;

class TestRoom
{
public static void main(String [] args)
{
    Scanner userInput = new Scanner(System.in);
    System.out.println("Please enter the name of room");

    String roomName = userInput.next();
    while(!(roomName.equals("quit")))
    {           
        Room r;
        do
        {
            Scanner dimensionsInput = new Scanner(System.in);
            System.out.print("Please enter the dimensions of the room, length, width and height accordingly");

            double roomLength = dimensionsInput.nextDouble();
            double roomWidth = dimensionsInput.nextDouble();
            double roomHeight = dimensionsInput.nextDouble();

            r = new Room(roomName, roomLength, roomWidth, roomHeight);
        }
        while (r.getLength() == 0 || r.getWidth() == 0 || r.getHeight() == 0);


        System.out.println("The name of room: " + r.getName() + " Dimensions: L= " + r.getLength() + 
                                " X " + "W= " + r.getWidth() + " X " + "H= " + r.getHeight());
        System.out.println("Area of room= " + r.getArea());
        System.out.println("Volume of room= " + r.getVolume());

    }
    userInput.close();
}
}

Right now the application only returns to the second prompt instead of the first one so could you guys help me on this? 现在,应用程序仅返回第二个提示,而不是第一个提示,因此你们可以帮我吗? Thanks in advance again guys! 再次感谢大家!

You don't need to declare two scanners. 您无需声明两个扫描器。 You obviously had to include the first prompt into the outer while loop as well. 显然,您也必须将第一个提示也包含在外部while循环中。

"Fixed" code: “固定”代码:

import java.util.Scanner;

class TestRoom
{
    public static void main(String[] args)
    {
        Scanner userInput = new Scanner(System.in);
        String roomName = "";

        while (!(roomName.equals("quit")))
        {
            System.out.println("Please enter the name of room");

            roomName = userInput.next();

            Room r;

            do
            {
                System.out
                        .print("Please enter the dimensions of the room, length, width and height accordingly");

                double roomLength = userInput.nextDouble();
                double roomWidth = userInput.nextDouble();
                double roomHeight = userInput.nextDouble();

                r = new Room(roomName, roomLength, roomWidth, roomHeight);
            } while (r.getLength() == 0 || r.getWidth() == 0
                    || r.getHeight() == 0);

            System.out.println("The name of room: " + r.getName()
                    + " Dimensions: L= " + r.getLength() + " X " + "W= "
                    + r.getWidth() + " X " + "H= " + r.getHeight());
            System.out.println("Area of room= " + r.getArea());
            System.out.println("Volume of room= " + r.getVolume());
        }

        userInput.close();
    }
}

Try this: 尝试这个:

 class TestRoom {

    static Scanner userInput = new Scanner(System.in);

    static Scanner dimensionsInput = new Scanner(System.in);

    public static void main(String[] args) {

        while(true) {
            prompt();
        }

    }

    private static void prompt() {
        System.out.println("Please enter the name of room");

        String roomName = userInput.next();

        if (roomName.equals("quit")){
            userInput.close();
            dimensionsInput.close();
            System.exit(0);
        }
        Room r;
        do
        {
            System.out
                    .print("Please enter the dimensions of the room, length, width and height accordingly");

            double roomLength = dimensionsInput.nextDouble();
            double roomWidth = dimensionsInput.nextDouble();
            double roomHeight = dimensionsInput.nextDouble();
            r = new Room(roomName, roomLength, roomWidth, roomHeight);
        } while (r.getLength() == 0 || r.getWidth() == 0
                || r.getHeight() == 0);


        System.out.println("The name of room: " + r.getName()
                + " Dimensions: L= " + r.getLength() + " X " + "W= "
                + r.getWidth() + " X " + "H= " + r.getHeight());
        System.out.println("Area of room= " + r.getArea());
        System.out.println("Volume of room= " + r.getVolume());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM