我需要帮助让这个程序在第一次运行后在自己内循环。它询问用户是否要编码,解码或退出。该程序运行,它编码/解码它应该。我现在需要弹出另一个窗格,询问用户是否想要编写另一个输入,然后循环遍历第一次运行的所有内容。我有窗格,它询问用户是否想要再次运行但不能让它循环编码器。
public void encoding()
{
int userChoice;
int i;
int p=1;
int counter=0;
counter++;
String fin = "";
String input = JOptionPane.showInputDialog("Want to ENCODE, DECODE or EXIT? Press
1, 2, or 3");
userChoice = Integer.parseInt(input);
if (userChoice == 1 )
{
String encode = JOptionPane.showInputDialog(null, "What Are We
Encoding? ");
char[] array = encode.toCharArray();
for(i=0; i <array.length; i++)
{
char Ecode = encode.charAt(i);
Ecode--;
Ecode--;
fin += Character.toString(Ecode);
}
JOptionPane.showMessageDialog(null, fin);
}
else if (userChoice == 2)
{
String decode =JOptionPane.showInputDialog(null, "What Are We
Decoding? ");
char[] array1 = decode.toCharArray();
for(i=0; i < array1.length; i++)
{
char Dcode = decode.charAt(i);
Dcode++;
Dcode++;
fin += Character.toString(Dcode);
}
JOptionPane.showMessageDialog(null,fin);
String again = JOptionPane.showInputDialog("Want to code another?
Press 1 or 2");
int aChoice = Integer.parseInt(again);
if (aChoice==1)
{
System.out.print("bob");
}
else
{
JOptionPane.showMessageDialog(null, "Good Bye");
System.exit(0);
}
}
答案 0 :(得分:2)
将您的代码包裹起来int i;
并将if-else block
包含在do-while
循环中:
do{
int i;
int p=1;
.....
.....
}while(userChoice != 3);
请注意:在您输入3
之前,这不会让您退出。
当用户输入1,2或3之外的任何其他内容时,您可能需要添加另一个块来处理这些条件。
或者,您可以这样做:
do{
String input = JOptionPane.showInputDialog...
.....
.....
}while(userChoice == 1 || userChoice == 2);
除了1或2之外,这将退出循环。
编辑:请在下面找到固定代码:
public void encoding(){
int userChoice, i;
do{
String fin = "";
String input = JOptionPane
.showInputDialog("Want to ENCODE, DECODE or EXIT? Press 1, 2, or 3");
userChoice = Integer.parseInt(input);
if (userChoice == 1 ){
String encode = JOptionPane.showInputDialog(null, "What Are We Encoding?");
char[] array = encode.toCharArray();
for(i=0; i <array.length; i++){
char Ecode = encode.charAt(i);
Ecode--;
Ecode--;
fin += Character.toString(Ecode);
}
JOptionPane.showMessageDialog(null, fin);
} else if (userChoice == 2) {
String decode =JOptionPane.showInputDialog(null, "What Are We Dencoding?");
char[] array1 = decode.toCharArray();
for(i=0; i < array1.length; i++){
char Dcode = decode.charAt(i);
Dcode++;
Dcode++;
fin += Character.toString(Dcode);
}
JOptionPane.showMessageDialog(null, fin);
}
}while(userChoice != 3);
JOptionPane.showMessageDialog(null, "Good Bye");
System.exit(0);
}
答案 1 :(得分:1)
您可以在JOptionPane
循环中附加if
输入+完整的相应while
块:
int userChoice = 0;
while (userChoice != 3) {
int i;
int p=1;
// the rest of the params here
String input = JOptionPane.showInputDialog(...)
...
}