Java 帮助测试两个单词,然后打印相同字符所在的十字
我正在尝试制作一个程序,从命令行读取两个英语单词,然后输出所有可能的单词交叉方式。如果它们没有交叉,则打印错误消息。我想使用charAt和length方法。。甚至不知道从哪里开始 以下是我目前得到的信息:Java 帮助测试两个单词,然后打印相同字符所在的十字,java,Java,我正在尝试制作一个程序,从命令行读取两个英语单词,然后输出所有可能的单词交叉方式。如果它们没有交叉,则打印错误消息。我想使用charAt和length方法。。甚至不知道从哪里开始 以下是我目前得到的信息: public class Cross2 { public static void main(String[] args) { // create two dimentional array that stores input private String[][
public class Cross2
{
public static void main(String[] args)
{
// create two dimentional array that stores input
private String[][] cross = new String[w1pos][w2pos];
// get input from user
Scanner input = new Scanner(System.in);
System.out.println("Enter two words: ");
String word1 = input.next();
String word2 = input.next();
// loop through length of words
for(int w1pos = 0; w1pos < word1.length(); w1pos++) {
for(int w2pos = 0; w2pos < word2.length(); w2pos++) {
// if characters are equal, print the array
if (word1.charAt(w1pos) == word2.charAt(w2pos))
System.out.println(cross[w1pos][w2pos]);
}
}
公共类Cross2
{
公共静态void main(字符串[]args)
{
//创建存储输入的二维数组
私有字符串[][]交叉=新字符串[w1pos][w2pos];
//从用户处获取输入
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入两个单词:”);
String word1=input.next();
String word2=input.next();
//循环单词的长度
对于(int w1pos=0;w1pos
我想是的。我需要遍历字符串的长度,然后使用charAt(i)打印每个字符 这是一个好的开始
是的,就像在字母匹配中一样。所以我需要用charAt来比较两个单词中每个位置的每个字符 很好。提示:那么有多少个循环 如果它们没有交叉,则打印错误消息 提示:……你将如何做到这一点 不要回答我的问题。它们是提示。根据它们编写一些代码。那么对于“abra”和“cadabra”这两个词,输出会是这样的吗
c
abra
d
a
b
r
a
c
a
d
abra
b
r
a
c
a
d
a
b
r
abra
c
a
d
a
abra
r
a
c
a
d
a
b
abra
a
c
abra
d
a
b
r
a
c
a
d
abra
b
r
a
c
a
d
a
b
r
abra
如果是这样的话,我建议您使用一个二维字符数组,其中填充空格,在显示之前将单词写入其中。一次打印一个字符可能要困难得多
[编辑:]
我不懂Java,所以我不能使用函数,也不能编写比您更多的东西,但我希望这能有所帮助
// This is a comment explaining what the code does.
公共类Cross2
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入两个单词:”);
String word1=input.next();
String word2=input.next();
字符串[][]交叉=新字符串[word1.length()][word2.length()];
/*用空格填充“十字架”*/
对于(int w1pos=0;w1pos
你能写一个循环,一次打印一个字母吗?写一些代码并发布。你是说字母匹配中的交叉吗?我想是的。我需要循环字符串的长度,然后使用字符(i)要打印每个字符..和字母匹配中的“是”交叉..所以我需要在两个单词中的每个位置使用charAt比较每个字符,然后比较它们,如果它们相等..那么我会感到困惑。不确定如何打印它们以便它们相应地相交。准确地说。因此,将用户输入存储为数组,然后循环一次f或者每个数组,将单词一中的每个位置与其他位置进行比较。然后如果它们相等..嗯,这就是我出错的地方。所以你已经检查过word1.charAt(w1pos)==word2.charAt(w2pos)
。在我上面的例子中,word1
是水平打印的,所以让我们继续。下一步是将word1
的每个字符存储到二维数组中。因为word1
需要在w2pos
处与word2
相交,所以您需要存储word1
的字符到二维数组的第w2pos
行。即使你现在不能得到完整的解决方案,也请发布一些代码,这样我们可以更好地帮助你。真的很感谢这里的帮助,伙计们。不知道没有你我会怎么做。我有时在这个世界上感到很孤独。以下是我得到的一些东西:Sting word1;String word2;//create存储输入私有字符串[][]的二维数组cross=new String[ROWS][COLS];//从用户扫描仪获取输入input=new Scanner(System.in);System.out.println(“输入两个词:”);String word1=input.next();String word2=intput.next();//使用长度()确定每个数组的长度(intw2pos=0;w2pos/* This is a comment explaining what you need to add to make the code work. */
public class Cross2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter two words: ");
String word1 = input.next();
String word2 = input.next();
String[][] cross = new String[word1.length()][word2.length()];
/* Fill 'cross' with spaces */
for(int w1pos = 0; w1pos < word1.length(); w1pos++) {
for(int w2pos = 0; w2pos < word2.length(); w2pos++) {
if (word1.charAt(w1pos) == word2.charAt(w2pos)) {
// Store 'word1' horizontally into 'cross'.
for(int w1posAgain = 0; w1posAgain < word1.length(); w1posAgain++) {
/* Store 'word1.charAt(w1posAgain)' horizontally into 'cross'
at row 'w2pos' and column 'w1posAgain'. */
}
// Store 'word2' vertically into 'cross'.
for(int w2posAgain = 0; w2posAgain < word2.length(); w2posAgain++) {
/* Store 'word2.charAt(w2posAgain)' vertically into 'cross'
at row 'w2posAgain' and column 'w1pos'. */
}
for(int w1posAgain = 0; w1posAgain < word1.length(); w1posAgain++) {
for(int w2posAgain = 0; w2posAgain < word2.length(); w2posAgain++) {
System.out.print(cross[w1posAgain][w2posAgain]);
}
System.out.println();
}
/* Fill 'cross' with spaces.
Yes, really.*/
}
}
}
}
}