Java扫描程序类到JOptionPane类?
这就是代码的作用:允许用户输入一个字符(L或S)和三个介于100和1000之间的整数。程序输出以下内容:Java扫描程序类到JOptionPane类?,java,swing,java.util.scanner,joptionpane,Java,Swing,Java.util.scanner,Joptionpane,这就是代码的作用:允许用户输入一个字符(L或S)和三个介于100和1000之间的整数。程序输出以下内容: 如果间隔[100-1000]中未包含数字,则使用方法检查输入,显示信息:“输入错误!” 如果用户输入字母L,则使用方法最大值 如果用户使用最小方法输入字母S,则为最小数字 所有数字及其数字都使用向后的方法反转 使用EvenOrOdd方法,该方法接受一个整数,如果数字为偶数,则输出“True”;如果数字为奇数,则输出“False”,然后输出输入的数字,后跟“True”或“False” 使用方法
检查输入,显示信息:“输入错误!”李>
如果用户输入字母L,则使用方法最大值李>
如果用户使用最小方法输入字母S,则为最小数字李>
所有数字及其数字都使用向后的方法反转
李>
使用EvenOrOdd方法,该方法接受一个整数,如果数字为偶数,则输出“True”;如果数字为奇数,则输出“False”,然后输出输入的数字,后跟“True”或“False”
使用方法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获取输入
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!");
更多信息:所以。。。你就这么试过?最后那两行?请你自己再努力一点解决它,我们不会做你的家庭作业。我尝试了更多,但我删除了它们,因为它们不起作用@frakcool什么不起作用?输出不是预期的?你有例外吗?请张贴一个我们可以复制粘贴的帖子,看看你错在哪里。示例输入/输出和/或错误日志
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!");