- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我编译代码时,我的 java 应用程序出现问题,它说找不到符号 roomLength。应用程序应该做的是提示用户输入房间名称,然后如果它是“退出”,那么它必须关闭程序。否则会提示房间的长、宽、高。如果这些维度中的任何一个为零或小于零,它将再次重新提示输入维度。
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());
}
}
}
提前致谢! ;)
还有另一个问题几乎与这个问题类似,只不过它应该循环回到第一个提示,允许用户输入另一个房间的名称及其尺寸。
Java do while loop back to the beginning of application
干杯!
最佳答案
您正在尝试访问声明范围之外的变量。您必须在 do
之外声明变量,以便可以在 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
也声明了错误的范围。如果您想在循环之后访问它,则必须在循环之前声明它。因此,更简单的解决方案可能是:
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
。
关于Java SE do while 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24474905/
我无法从远程 Ubuntu 服务器打开在本地 macOS 机器上运行的 Stata 的 GUI。 我已经安装了stata-se在我的本地机器上,我可以打开 X11 应用程序,如 xclock和 xey
我正在尝试将 tobit 模型从 Stata 迁移到 R。 稳健的 Stata 命令只需将 ,vce(robust) 添加到模型中。对于集群,它将是 ,vce(cluster idvar)。 可重现的
以下代码在 JavaSE 6 中工作正常,但在 JavaSE 7 中执行时抛出 ConnectException(超时)。这是 JDK7 错误还是错误代码?实在是看不懂…… public sta
在 SE 6 或 SE 7 的任何类中是否有任何地方不是最终的,即可以直接设置的字段? 我正在研究从另一种语言 (Haskell) 到 Java 的绑定(bind),我正在寻找那个特定的案例来检查我的
我想学习 Java,特别是 Android 开发,因为我要买一台新的平板电脑,但也想学习 Java 可以做的许多其他事情(Struts、网络开发、JWebSockets 等)。我是一位在 PHP、Ja
我正在尝试使用 websockets 编写一些东西,但遇到了无法解析的导入问题。 经过几次尝试,我创建了两个相同的项目。一份使用 Java SE9,一份使用 JAVA SE-1.8。 导入适用于 1.
我使用 Weld SE 创建了一个简单的 JavaSE 应用程序。我正在尝试使用 gradle run 运行它会抛出异常: :compileJava UP-TO-DATE :processResour
我正在尝试将现有应用程序的 Java JDK 版本从 Java 5 更改为 Java 6(更新 38)。该应用程序使用一些 JAXB 生成的类来编码/解码我们从远程服务器发送/接收的 XML。 XML
我有一个命令行 Java 应用程序,它在 Windows 7 x64 平台上读写文件。当前应用程序使用随附的 IBM Java SE 6 运行。结构如下: APP_ROOT some_fold
在主题中,我有一个使用 Java 1.6 编译的 Java SE 应用程序部署在 Java 1.7 运行时环境中。由于 Java 是高度向后兼容的,我假设一切都会很好 - 但自从它部署在 1.7 环境
我正在开发一个 JavaFX 2 项目,并且该应用程序必须使用另一个 main 方法(根据生成的 JAR list 为 com.javafx.main.Main)启动。据官方焊缝reference引导
内部类 1. 内部类的概述 内部类指的是****把类定义在另一个类的内部,该类就被称为内部类。类名不需要和源文件名相同。 内部类的种类: 在Java中,内部类的使用共有两种情况: (1). 在类中定义
我正在开发一个 Java 应用程序,用于在 yfrog.com 上上传图像。 我可以在API页面成功发帖但没有二进制文件,只有一个字符串参数 . 另外我使用的方法只接受“字符串”。 URLConnec
我在我的应用程序中创建了一个与端口绑定(bind)的 ServerSocket。我将此应用程序分发到同一网络上连接的 PC。我可以使用此端口向安装了该应用程序的任何 PC 发送请求。现在接收者可以接收
我已经解决了Java SE Security特点。 据我了解,此标准功能的整体理念是保护用户免受恶意软件的侵害。 对于 Web 上的两个 Java 应用程序,默认情况下启用 SecurityManag
当我编译代码时,我的 java 应用程序出现问题,它说找不到符号 roomLength。应用程序应该做的是提示用户输入房间名称,然后如果它是“退出”,那么它必须关闭程序。否则会提示房间的长、宽、高。如
我不明白为什么下面的代码返回“false” String x="Hello World"; String z=" Hello World".trim(); System.out.println(x==
首先,我要说的是我在观看此视频时完成了所有这些操作:https://www.youtube.com/watch?v=Hl-zzrqQoSE 所以,我尝试下载适用于 Windows 64 位的 jdk-
我正在尝试创建一个具有用户友好界面的网络Java SE应用程序,我对GUI库(Swing、SWT、JavaFX)感到很困惑 以及关于应用程序 (MVC、PureMVC) 的架构 任何有经验的人都可以给
拜托,我有一个 java 桌面应用程序,我想要一种将我的桌面应用程序连接到在线数据库的方法。我想知道可用于执行此操作的技术。 最佳答案 您可以只使用 JDBC。 您必须创建与数据库属性的连接,例如:
我是一名优秀的程序员,十分优秀!