Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何继续切换案例程序,同时询问用户是否要继续(是或否)?_Java_Swing_Loops_Joptionpane - Fatal编程技术网

Java 如何继续切换案例程序,同时询问用户是否要继续(是或否)?

Java 如何继续切换案例程序,同时询问用户是否要继续(是或否)?,java,swing,loops,joptionpane,Java,Swing,Loops,Joptionpane,代码如下: import javax.swing.*; public class SwitchCase { /** * @param args */ public static void main(String[] args) { int dec = 0, num, rem, org, pow, length = 0, i = 1, input; LOOPA: { input = Integer

代码如下:

    import javax.swing.*;

public class SwitchCase {

    /** * @param args */
    public static void main(String[] args) {

        int dec = 0, num, rem, org, pow, length = 0, i = 1, input;
        LOOPA: {
            input = Integer
                    .parseInt(JOptionPane
                            .showInputDialog("Press 1 for binary to decimal conversion \n "
                                    + "Press 2 for decimal to binary conversion\n"
                                    + "Press 3 for octal to decimal conversion\n"
                                    + "Press 4 for decimal to octal conversion\n"));

            switch (input) {

            case 1:
                num = Integer.parseInt(JOptionPane
                        .showInputDialog("Enter binary number"));
                org = num;
                while (num != 0) {
                    num = num / 10;
                    length++;
                }
                pow = length - 1;
                System.out.print("decimal Equivalent is: ");
                while (pow >= 0) {
                    rem = org % 10;
                    dec = dec + (rem * i);
                    i = i * 2;
                    pow--;
                    org = org / 10;
                }
                System.out.print(dec);
                break LOOPA;

            // decimal to binary conversion

            case 2:
                int addTwo = 2,
                bin;
                num = Integer.parseInt(JOptionPane
                        .showInputDialog("Enter decimal number"));

                while (num != 0) {
                    rem = num % 2;
                    num = num / 2;

                    addTwo = (addTwo * 10) + rem;
                }
                System.out.print("Binary Equivalent is: ");

                while (addTwo != 2) {
                    bin = addTwo;
                    bin = bin % 10;
                    System.out.print(bin);
                    addTwo = addTwo / 10;
                }
                break LOOPA;

            case 3: // octal to decimal conversion

                num = Integer.parseInt(JOptionPane
                        .showInputDialog("Enter octal number"));
                org = num;
                while (num != 0) {
                    num = num / 10;
                    length++;
                }
                pow = length - 1;
                System.out.print("decimal Equivalent is: ");
                while (pow >= 0) {
                    rem = org % 10;
                    dec = dec + (rem * i);
                    i = i * 8;
                    pow--;
                    org = org / 10;
                }
                System.out.print(dec);

                break LOOPA; // decimal to octal conversion

            case 4:
                int addEight = 8,
                octal;
                num = Integer.parseInt(JOptionPane
                        .showInputDialog("Enter decimal number"));

                while (num != 0) {
                    rem = num % 8;
                    num = num / 8;

                    addEight = (addEight * 10) + rem;
                }
                System.out.print("Octal Equivalent is: ");

                while (addEight != 8) {
                    octal = addEight;
                    octal = addEight % 10;
                    System.out.print(octal);

                    addEight = addEight / 10;
                }
                break LOOPA;
            default:
                System.out.print("do u want to continue");
            }
        }
    }
}
如何继续切换案例程序,同时询问用户是否要继续(是或否)


使用循环。

由于代码的未格式化程度有点太高,无法理解,下面是一个解决问题的简单方法。试着做以下几行:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
switch(yourVariable)
{

      case 1:
      //do something
      System.out.println("Do you wish to continue(y/n)?");
      s = br.readLine();
      if(s.equals("n"))
               break;


      //so on
}

但是我如何在switch语句之外使用它呢


Aehm-请格式化(显示一些努力)。编辑器中有一个代码图标。我当然不想看这个代码。格式不正确。如果您将代码缩小到一小段,以演示您的问题,将更容易获得帮助。请查看此内容,并向上投票正确或大致回答您问题的答案。您能将代码格式设置为不会让我生气吗?从第一次编辑开始,我的脑袋还在转。这只是一个开关盒程序,你只需要告诉我如何重复使用它,同时询问用户,他是否想继续。你只需要明白,那些免费帮助你的人希望你能付出一些努力。就从这个开始吧。但是我怎么能在switch语句之外使用它呢?很好的编辑。不确定OP是否注意到了,很可能他没有。
do
{
    switch(yourVariable)
          {

          }
    System.out.println("Do you wish to continue(y/n)?");
    s = br.readLine();
}while(s.equals("y");