Java Scanner类到JOptionPane类?

时间:2015-12-03 16:20:05

标签: java swing java.util.scanner joptionpane

这就是这段代码的作用:允许用户输入一个字符(L或S)和三个100到1000之间的整数。程序输出以下内容:

  1. 如果使用方法CheckInput在区间[100-1000]中未包含数字,则显示消息:“输入错误!”
  2. 如果用户使用方法Largest;
  3. 输入字母L,则为最大数字
  4. 如果用户使用Smallest方法输入字母S,则为最小数字;
  5. 使用方法Backwards;
  6. 反转所有数字及其数字
  7. 使用方法EvenOrOdd取一个整数,如果数字为偶数则输出'True',如果是奇数则输出'False',输出输入的数字后跟'True'或'False'
  8. 使用方法CheckDivisor,它接受​​所有三个数字,并检查第一个数字是否为第二个或第三个的除数,并返回“True”或“False”
  9. 使输出更容易理解,并为程序添加两个方法
  10. 对于数字7,我添加了,加倍和总和

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Words {
    
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System. in );
    
            while (scan.hasNext()) {
                String line = scan.nextLine();
    
                if (line.equals("exit")) {
                    break;
                }
                String[] words = line.split(" ");
    
                //alphabetical
                System.out.print("Alphabetical order: ");
                alphabetical(words);
                // concatenate and mix
                String[] sorted = words.clone();
                Arrays.sort(sorted);
                String all = "";
                for (String s: sorted) {
                    all += s;
                }
    
                System.out.println("All together: " + all);
                System.out.println("Concatenate and Mix: " + mix(all));
    
                // find first 2 and last 2 letters
                first2last2(all);
                //middlexx
                System.out.println("Replace middle with xx or yy: " + middlexx(all));
    
                System.out.println();
            }
        }
        private static void alphabetical(String[] words) {
            String[] ws = words.clone();
            Arrays.sort(ws);
    
            for (int i = 0; i < ws.length; i++) {
                if (i != 0) {
                    System.out.print(" ");
                }
                System.out.print(ws[i]);
            }
            System.out.println();
        }
        private static String mix(String s) {
            return s.replaceAll("a|e|i|o|u", "x");
        }
        private static void first2last2(String s) {
            if (s.length() < 5) {
                System.out.println("Invalid command!");
            } else {
                System.out.println("First two characters: " + s.substring(0, 2));
                System.out.println("Last two characters: " + s.substring(s.length() - 2, s.length()));
            }
        }
        private static String middlexx(String s) {
            if (s.length() % 2 == 0) {
                return s.substring(0, s.length() / 2) + "xx" + s.substring(s.length() / 2, s.length());
            } else {
                return s.substring(0, s.length() / 2) + "yy" + s.substring(s.length() / 2 + 1, s.length());
            }
        }
    
    }
    

    我如何将其转换为JOptionPane课程?我希望程序要求用户输入LS,并使用以下内容键入3个整数:

    String input1 = JOptionPane.showInputDialog("Please enter L or S");
    a = Double.valueOf(input1).doubleValue();
    

1 个答案:

答案 0 :(得分:0)

您可以像这样

从JOptionPane获取输入
JOptionPane.showInputDialog ( "put your message here" ); 

示例代码

  Object[] possibilities = {"ham", "spam", "yam"};
  String s = (String)JOptionPane.showInputDialog(
                      frame,
                      "Complete the sentence:\n"
                      + "\"Green eggs and...\"",
                      "Customized Dialog",
                      JOptionPane.PLAIN_MESSAGE,
                      icon,
                      possibilities,
                      "ham");

  //If a string was returned, say so.
  if ((s != null) && (s.length() > 0)) {
      setLabel("Green eggs and... " + s + "!");
      return;
  }

  //If you're here, the return value was null/empty.
  setLabel("Come on, finish the sentence!");

更多信息:https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html