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

时间:2012-05-19 05:11:21

标签: 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
                    .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");
            }
        }
    }
}

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:2)

  

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

使用循环。