【问题标题】:Java Scanner class to JOptionPane class?Java Scanner 类到 JOptionPane 类?
【发布时间】:2015-12-03 16:20:04
【问题描述】:

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

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

对于我添加的数字 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();

【问题讨论】:

  • 那么……这就是你所尝试的全部?最后两行?请多花点精力尝试自己解决,我们不会做你的功课。
  • 我尝试了更多,但我删除了这些,因为它们不起作用@Frakcool
  • 什么不起作用?输出不是预期的?你有例外吗?请张贴Runnable Example,我们可以复制粘贴,看看你错在哪里。示例输入/输出和/或错误日志

标签: java swing java.util.scanner joptionpane


【解决方案1】:

您可以像这样从 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 2017-07-17
    相关资源
    最近更新 更多