Java SE执行while循环问题 [英] Java SE do while loop issues

查看:137
本文介绍了Java SE执行while循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译代码时,我的java应用程序出现问题,它说找不到符号 roomLength
应用程序应该做的是提示用户输入房间名称然后如果它退出那么它必须关闭程序。否则会提示房间的长度,宽度和高度。如果这些尺寸中的任何一个为零或更小,它将再次重新调整尺寸。

I have a problem with my java application when i compile my codes it says that it cannot find symbol roomLength. what the application supposed to do is prompt the user to input a room name then if it is "quit" then it has to close the program. otherwise it will prompt for the length, width and height of the room. if any of these dimensions is zero or lesser, it will reprompt for the dimensions again.

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();
    if(roomName.equals("quit"))
    {
        userInput.close();
    } else
    {
        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();

        Room r = new Room(roomName, roomLength, roomWidth, roomHeight);
        }
        while (roomLength > 0 || roomWidth > 0 || roomHeight > 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());

    }
}
}

谢谢预先! ;)

除了它应该循环回到第一个提示,允许用户输入另一个房间的名称及其尺寸时,还有另一个与此问题几乎相似的问题。

There's another question almost similar to this question except it supposed to loop back to the first prompt allowing the user to input another room's name and its dimensions.

Java一边循环回到应用程序的开头

干杯!

推荐答案

您正在尝试访问它们声明范围之外的变量。您必须声明之外的变量,以便您可以访问它们,而

You're trying to access variables outside the scope they're declared in. You have to declare the variables outside the do so you can access them in the while:

    double roomLength;
    double roomWidth;
    double roomHeight;
    do
    {
    Scanner dimensionsInput = new Scanner(System.in);
    System.out.print("Please enter the dimensions of the room, length, width and height accordingly");

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

    Room r = new Room(roomName, roomLength, roomWidth, roomHeight);
    }
    while (roomLength > 0 || roomWidth > 0 || roomHeight > 0);

但我现在看到房间也是声明范围错误。如果你想在之后访问它,你必须在循环之前声明它。所以一个更简单的解决方案可能是:

But I see now that Room is also declared with the wrong scope. You have to declare it before the loop if you want to access it afterwards. So a simpler solution might be:

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

顺便说一句,在所有维度都为0之前你循环似乎是不对的。我怀疑你的意思是检查 == 0

Incidentally, it doesn't seem right that you're looping until all dimensions are 0. I suspect you mean to check == 0.

这篇关于Java SE执行while循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆