这就是这段代码的作用:允许用户输入一个字符(L或S)和三个100到1000之间的整数。程序输出以下内容:
CheckInput
在区间[100-1000]中未包含数字,则显示消息:“输入错误!”Largest
; Backwards
; CheckDivisor
,它接受所有三个数字,并检查第一个数字是否为第二个或第三个的除数,并返回“True”或“False”对于数字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
课程?我希望程序要求用户输入L
或S
,并使用以下内容键入3个整数:
String input1 = JOptionPane.showInputDialog("Please enter L or S");
a = Double.valueOf(input1).doubleValue();
答案 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