gpt4 book ai didi

java - 将 System.out.print 转换为 JOptionPane?

转载 作者:行者123 更新时间:2023-12-02 07:14:44 26 4
gpt4 key购买 nike

想要输出这个程序来判断两个单词是否是字谜词。我想知道如何将所有 System.out.print 命令更改为 JOption Pane 命令!我希望得到任何帮助,因为我是一名一年级程序员,并且有这项作业到期。

import java.util.Arrays;

public class NewAnagram
{
public static void main(String[] args)
{
if (args.length != 2)
{
System.out.println("2 words have not been entered!");
}
else
{
printPhrases(args[0], args[1]);
}
}

public static boolean anagramSearch(String phrase1, String phrase2)
{

String ltrsOnlyOne = lettersOnly(phrase1);
String ltrsOnlyTwo = lettersOnly(phrase2);
char[] first = ltrsOnlyOne.toLowerCase().toCharArray();
char[] second = ltrsOnlyTwo.toLowerCase().toCharArray();

Arrays.sort(first);
Arrays.sort(second);

if (Arrays.equals(first, second))
{
return true;
}
else
{
return false;
}
}

public static String lettersOnly(String word)
{
int length = word.length();
StringBuilder end = new StringBuilder(length);
char j;

for (int i = (length - 1); i >= 0; i--)
{
j = word.charAt(i);
if (Character.isLetter(j))
{
end.append(j);
}
}
return end.toString();
}

public static void printPhrases(String phrase1, String phrase2)
{
boolean isFound = anagramSearch(phrase1, phrase2);
if (isFound == true)
{
System.out.println(phrase1 + " is an anagram of "+ phrase2);
}

if (isFound == false)
{
System.out.println(phrase1 + " is not an anagram of "+ phrase2);
}
}

}

最佳答案

像这样的一行

System.out.println(phrase1 + " is not an anagram of "+ phrase2);

在 JOptionPane 版本中将是这样的:

JOptionPane.showMessageDialog(null, phrase1 + " is not an anagram of " + phrase2);

其中第一个参数是对话框的。由于您的应用程序中没有任何其他窗口,因此只需传递 null

关于java - 将 System.out.print 转换为 JOptionPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15077209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com