Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java执行while循环回到应用程序的开始_Java_Loops_While Loop_Do While - Fatal编程技术网

Java执行while循环回到应用程序的开始

Java执行while循环回到应用程序的开始,java,loops,while-loop,do-while,Java,Loops,While Loop,Do While,此问题是此链接的后续问题 这是一个请求房间名称的应用程序,然后第二次提示请求房间尺寸以执行某些计算。完成所有操作后,应用程序应该能够返回到第一个提示,询问房间名称等 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 =

此问题是此链接的后续问题

这是一个请求房间名称的应用程序,然后第二次提示请求房间尺寸以执行某些计算。完成所有操作后,应用程序应该能够返回到第一个提示,询问房间名称等

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;
}
}
以及我运行应用程序的主要方法

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

现在应用程序只返回到第二个提示,而不是第一个,所以你们能帮我吗?再次提前感谢各位

您不需要申报两台扫描仪。显然,您还必须将第一个提示包含到外部while循环中

“固定”代码:

试试这个:

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

您是否需要将第一个提示包含在while循环中?提示文件室名称应该在循环内部,而不是外部,因为您希望重复该提示。@JBNizet我已剪切该提示并将其粘贴到代码室r的正上方;虽然编译还可以,但它不会运行。编辑你的问题并在底部发布你的新代码。定义“它不运行”。会发生什么?您意识到在要求用户输入内容后,您必须继续阅读用户输入的内容,对吗?编辑您的问题,在原始问题末尾显示您的最新尝试作为附加代码,显示您的错误。也可以使用调试器或println语句来帮助您调试。哇,谢谢,但这行是什么意思?字符串roomName=“”;这意味着该字符串被分配了一个空字符串
null
不好,因为对其调用方法会引发
NullPointerException
。在本例中,除了
quit
之外,任何字符串都可以被分配,因为它将跳过外部while循环。
 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());
    }
}