好吧我花了一段时间试图为此找到解决方案我甚至检查了帖子:
但我仍然无法弄清楚如何做我想要的问题我的问题如下:当我去检查该值是否在给定范围内时,它只检查示例32的第一个值并忽略其余的是代码。请帮忙不要带我去另一个帖子我是java新手我会非常感谢你的帮助
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.MessageBox;
public class Disneyland {
protected Shell shell;
private Text textHeight;
private String height;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Disneyland window = new Disneyland();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setImage(SWTResourceManager.getImage(Disneyland.class, "/Images/1000px-Disneyland_Park_Logo.svg.png"));
shell.setSize(600, 400);
shell.setText("Disneyland Park Height Requirements");
Label lblNewLabel = new Label(shell, SWT.NONE);
lblNewLabel.setImage(SWTResourceManager.getImage(Disneyland.class, "/Images/disney_magin_disneyland_6899.png"));
lblNewLabel.setBounds(233, 10, 128, 134);
Label lblNewLabel_1 = new Label(shell, SWT.NONE);
lblNewLabel_1.setForeground(SWTResourceManager.getColor(0, 51, 255));
lblNewLabel_1.setFont(SWTResourceManager.getFont("MathJax_Fraktur", 18, SWT.NORMAL));
lblNewLabel_1.setBounds(125, 166, 349, 27);
lblNewLabel_1.setText("Please input your height in inches\n");
textHeight = new Text(shell, SWT.BORDER);
textHeight.setBounds(233, 213, 128, 27);
Button btnSubmit = new Button(shell, SWT.NONE);
btnSubmit.setForeground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
btnSubmit.setImage(SWTResourceManager.getImage(Disneyland.class, "/Images/disney.gif"));
btnSubmit.setBounds(274, 261, 56, 45);
btnSubmit.addListener(SWT.Selection, new submitButtonListener());
}
public class submitButtonListener implements Listener {
public void handleEvent(Event event) {
height = textHeight.getText();
if(Integer.parseInt(height)<32 ){
MessageBox messagebox = new MessageBox(shell, SWT.OK );
messagebox.setText("Height limitations");
messagebox.setMessage("For your own safety you are only allowed to use the rides with Any Height signs under adult supervision. We hope you grow up very quick :)");
messagebox.open();
}else{
if(Integer.parseInt(height)==32 & Integer.parseInt(height)<35 ){
MessageBox messagebox = new MessageBox(shell, SWT.OK );
messagebox.setText("Height limitations");
messagebox.setMessage("You are allowed to go on the Autopia accompanied by another rider 54 inches or taller");
messagebox.open();
}
if(Integer.parseInt(height)== 35 & Integer.parseInt(height) < 40 ){
MessageBox messagebox = new MessageBox(shell, SWT.OK );
messagebox.setText("Height limitations");
messagebox.setMessage("You are allowed to go on the Autopia and the Gadget's Go Coaster accompanied by another rider 54 inches or taller");
messagebox.open();
}
if(Integer.parseInt(height)== 40 & Integer.parseInt(height)<42 ){
MessageBox messagebox = new MessageBox(shell, SWT.OK );
messagebox.setText("Height limitations");
messagebox.setMessage("You are allowed to go on the Autopia, Gadget's Go Coaster, the Big Thunder Mountain Railroad, Space Mountain, Splash Mountain and Star Tours accompanied by another rider 54 inches or taller");
messagebox.open();
}
if(Integer.parseInt(height)== 42 & Integer.parseInt(height)<46 ){
MessageBox messagebox = new MessageBox(shell, SWT.OK );
messagebox.setText("Height limitations");
messagebox.setMessage("You are allowed to go on the Autopia, Gadget's Go Coaster, the Big Thunder Mountain Railroad, Space Mountain, Splash Mountain, Star Tours and Matterhorn Bobsleds accompanied by another rider 54 inches or taller");
messagebox.open();
}
if(Integer.parseInt(height)== 46 & Integer.parseInt(height) < 54 ){
MessageBox messagebox = new MessageBox(shell, SWT.OK );
messagebox.setText("Height limitations");
messagebox.setMessage("You are allowed to go on the Autopia, Gadget's Go Coaster, the Big Thunder Mountain Railroad, Space Mountain, Splash Mountain, Star Tours, Matterhorn Bobsleds and Indiana Jones Adventure accompanied by another rider 54 inches or taller");
messagebox.open();
}
if(Integer.parseInt(height)>= 54 ){
MessageBox messagebox = new MessageBox(shell, SWT.OK );
messagebox.setText("Height limitations");
messagebox.setMessage("You are allowed to go on the Autopia without adult supervision" );
messagebox.open();
}
}
}
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
以下内容可能有助于澄清OP if
方法的问题。此外,它建议将一些常用代码移动到单个点。
public void handleEvent(Event event) {
// the title is always the same
final String title = "Height limitations";
// set the msg depending upon the discovered height
String msg;
// get value from box
final height = textHeight.getText();
// parse it once
final int parsedHeight = Integer.parseInt(height);
// set message depending upon height
if (parsedHeight < 32) {
msg = "For your own safety you are only allowed to use the rides with Any Height signs under adult supervision. We hope you grow up very quick :)";
} else if (parseHeight < 35) {
msg = "You are allowed to go on the Autopia accompanied by another rider 54 inches or taller";
}
...
//
// no need to repeat all of the code in each block
//
MessageBox messagebox = new MessageBox(shell, SWT.OK );
messagebox.setText(title);
messagebox.setMessage(msg);
messagebox.open();
答案 1 :(得分:0)
<强> 1。将==
更改为>=
- 这将检查小于或等于。
举个例子,
if(Integer.parseInt(height)>= 32 & Integer.parseInt(height) < 35 ){
此条件将检查32(包括)和35(不包括)之间。
<强> 2。也改变,
private String height;
至private int height;
为什么呢?您在很多地方将height
String
变量传递给int
。这是不必要的,如果你想要将它们传递给双倍,你必须改变所有。因此,为了避免冗余,请将height
变量数据类型更改为int
。
接下来,
将height = textHeight.getText();
更改为height = Integer.parseInt(textHeight.getText());
最后,
如果条件,删除里面的所有类型转换。像,
Integer.parseInt(height);
至height
。
最后一件事就是格式化代码,删除不必要的缩进和空行。然后很容易查看代码。
你的最终结果应该是:
class Disneyland {
protected Shell shell;
private Text textHeight;
private int height;
public static void main(String[] args) {
try {
Disneyland window = new Disneyland();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setImage(SWTResourceManager.getImage(Disneyland.class, "/Images/1000px-Disneyland_Park_Logo.svg.png"));
shell.setSize(600, 400);
shell.setText("Disneyland Park Height Requirements");
Label lblNewLabel = new Label(shell, SWT.NONE);
lblNewLabel.setImage(SWTResourceManager.getImage(Disneyland.class, "/Images/disney_magin_disneyland_6899.png"));
lblNewLabel.setBounds(233, 10, 128, 134);
Label lblNewLabel_1 = new Label(shell, SWT.NONE);
lblNewLabel_1.setForeground(SWTResourceManager.getColor(0, 51, 255));
lblNewLabel_1.setFont(SWTResourceManager.getFont("MathJax_Fraktur", 18, SWT.NORMAL));
lblNewLabel_1.setBounds(125, 166, 349, 27);
lblNewLabel_1.setText("Please input your height in inches\n");
textHeight = new Text(shell, SWT.BORDER);
textHeight.setBounds(233, 213, 128, 27);
Button btnSubmit = new Button(shell, SWT.NONE);
btnSubmit.setForeground(SWTResourceManager.getColor(SWT.TRANSPARENT));// COLOR_TRANSPARENT
btnSubmit.setImage(SWTResourceManager.getImage(Disneyland.class, "/Images/disney.gif"));
btnSubmit.setBounds(274, 261, 56, 45);
btnSubmit.addListener(SWT.Selection, new submitButtonListener());
}
class submitButtonListener implements Listener {
public void handleEvent(Event event) {
height = Integer.parseInt(textHeight.getText());
if (height < 32) {
MessageBox messagebox = new MessageBox(shell, SWT.OK);
messagebox.setText("Height limitations");
messagebox.setMessage(
"For your own safety you are only allowed to use the rides with Any Height signs under adult supervision. We hope you grow up very quick :)");
messagebox.open();
} else {
if (height >= 32 & height < 35) {
MessageBox messagebox = new MessageBox(shell, SWT.OK);
messagebox.setText("Height limitations");
messagebox.setMessage(
"You are allowed to go on the Autopia accompanied by another rider 54 inches or taller");
messagebox.open();
}
if (height >= 35 & height < 40) {
MessageBox messagebox = new MessageBox(shell, SWT.OK);
messagebox.setText("Height limitations");
messagebox.setMessage(
"You are allowed to go on the Autopia and the Gadget's Go Coaster accompanied by another rider 54 inches or taller");
messagebox.open();
}
if (height >= 40 & height < 42) {
MessageBox messagebox = new MessageBox(shell, SWT.OK);
messagebox.setText("Height limitations");
messagebox.setMessage(
"You are allowed to go on the Autopia, Gadget's Go Coaster, the Big Thunder Mountain Railroad, Space Mountain, Splash Mountain and Star Tours accompanied by another rider 54 inches or taller");
messagebox.open();
}
if (height >= 42 & height < 46) {
MessageBox messagebox = new MessageBox(shell, SWT.OK);
messagebox.setText("Height limitations");
messagebox.setMessage(
"You are allowed to go on the Autopia, Gadget's Go Coaster, the Big Thunder Mountain Railroad, Space Mountain, Splash Mountain, Star Tours and Matterhorn Bobsleds accompanied by another rider 54 inches or taller");
messagebox.open();
}
if (height >= 46 & height < 54) {
MessageBox messagebox = new MessageBox(shell, SWT.OK);
messagebox.setText("Height limitations");
messagebox.setMessage(
"You are allowed to go on the Autopia, Gadget's Go Coaster, the Big Thunder Mountain Railroad, Space Mountain, Splash Mountain, Star Tours, Matterhorn Bobsleds and Indiana Jones Adventure accompanied by another rider 54 inches or taller");
messagebox.open();
}
if (height >= 54) {
MessageBox messagebox = new MessageBox(shell, SWT.OK);
messagebox.setText("Height limitations");
messagebox.setMessage("You are allowed to go on the Autopia without adult supervision");
messagebox.open();
}
}
}
}
}