如果用户在输入输入后单击否,我会尝试使代码循环。
import java.awt.event.WindowAdapter;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Sizer extends WindowAdapter
{
public static void main (String[]args){
JFrame m = new JFrame();
JOptionPane.showMessageDialog(m,"Ok To set the window size you are going to type in the number for each value REMEMBER THE SIZE IS IN PIXELS");
String input1= JOptionPane.showInputDialog("Height (suggested under 1080 and above 300)");
int Height= Integer.parseInt( input1);
在输入之后,我必须确认如果用户单击“是”,则该用户是否正确输入了该信息;如果单击“否”,它将继续进行该操作,我希望它再次让用户键入它吗?
int a1 = JOptionPane.showConfirmDialog(m,"Are you sure that this is the correct Height"+ Height);
if (a1==JOptionPane.YES_OPTION){
if (a1==JOptionPane.NO_OPTION){
}
String input2= JOptionPane.showInputDialog("Width (suggested under 1920 and above 300)");
int Width = Integer.parseInt( input2);
JFrame frame = new JFrame();
Slop comp = new Slop();
frame.add(comp);
frame.setSize(Height,Width);
frame.setTitle("Slop of a Line");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
}
`
答案 0 :(得分:2)
使用do ... while循环,如果用户单击“否”,则清除高度
public class Sizer extends WindowAdapter
{
public static void main (String[]args){
JFrame m = new JFrame();
int height = 0;
JOptionPane.showMessageDialog(m,"Ok To set the window size you are going to type in the number for each value REMEMBER THE SIZE IS IN PIXELS");
do {
String input1= JOptionPane.showInputDialog("Height (suggested under 1080 and above 300)");
height= Integer.parseInt(input1);
int a1 = JOptionPane.showConfirmDialog(m,"Are you sure that this is the correct Height "+ height);
if (a1==JOptionPane.NO_OPTION){
height = 0;
}
} while (height==0)
}
}
这假设高度必须大于0。如果height可以为0,则将-1用作初始值和重置值。
编辑:
@Nicholas K的答案表明您实际上并不需要if语句,而是只需像这样完成while循环:
a1 = JOptionPane.showConfirmDialog(m,"Are you sure that this is the correct Height "+ height);
} while (a1==JOptionPane.NO_OPTION)
但是,您需要在方法开始时初始化a1。
答案 1 :(得分:1)
您可以使用
int a1 = 0;
do {
// Read input till user says 'yes
a1 = JOptionPane.showConfirmDialog(m, "Are you sure that this is the correct Height "+ Height);
} while (a1 == JOptionPane.NO_OPTION);
因此循环继续运行,直到用户输入值JOptionPane.NO_OPTION