【问题标题】:How to continue switch case program, while asking user, do you want to continue (yes or no)? [closed]如何继续切换案例程序,同时询问用户是否要继续(是或否)? [关闭]
【发布时间】:2012-05-26 14:39:08
【问题描述】:

代码如下:

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

【问题讨论】:

  • Aehm - 请格式化(显示一些努力)。编辑器中有一个代码图标。
  • 我当然不想看这段代码。格式错误。
  • 如果您将代码缩小到一小部分来展示您的问题,那么获得帮助会更容易。
  • 看看这个example
  • 请接受并支持正确或大致回答您问题的答案。

标签: java swing loops joptionpane


【解决方案1】:

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

使用循环。

【讨论】:

  • 你能格式化你的代码,这样它就不会让我眼花缭乱吗?从我做的第一次编辑开始,我的头还在旋转..
  • 它只是一个switch case程序,你只需要告诉用户我如何重复使用它,他是否想继续。就这样
  • 您只需要了解免费帮助您的人期望您付出一些努力。就这样(开始)。
【解决方案2】:

由于您的代码未格式化太多难以理解,因此这里有一个简单的解决您的问题的方法。尝试并在这些方面工作:

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

【讨论】:

  • 但是如何在 switch 语句之外使用它?
  • 伟大的编辑。不确定 OP 是否注意到它。
  • 很可能他没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-18
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多