-6

这是代码:

    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");
            }
        }
    }
}
4

2 回答 2

3

由于您的代码未格式化太多以至于无法理解,因此这是解决问题的简单方法。尝试在这些方面工作:

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 语句之外使用它?

do
{
    switch(yourVariable)
          {

          }
    System.out.println("Do you wish to continue(y/n)?");
    s = br.readLine();
}while(s.equals("y");
于 2012-05-19T05:20:47.900 回答
2

如何继续 switch case 程序,同时询问用户是否要继续(是或否)?

使用循环。

于 2012-05-19T05:17:49.533 回答