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

查看:19
本文介绍了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 do while 循环回到开头申请

干杯!

推荐答案

您正在尝试访问声明范围之外的变量.您必须在 do 之外声明变量,所以您可以在 while 中访问它们:

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

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

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天全站免登陆