帮助测试两个单词,然后打印相同字符所在的十字架

时间:2010-10-24 01:39:06

标签: java

我正在尝试制作一个程序,从命令行读取两个英文单词,然后输出单词可以互相交叉的所有可能方式。并且如果它们不交叉则打印错误消息。我想使用charAt和length方法..不知道哪里开始..

这是我到目前为止所得到的:

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]);
        }
    }

2 个答案:

答案 0 :(得分:3)

  我是这么认为的。我需要循环遍历字符串的长度,然后使用charAt(i)打印每个字符。

这是一个好的开始。

  

并且是在字母匹配中交叉..所以我需要使用charAt比较每个字符在每个单词中的每个位置

那很好。提示:那么循环多少?

  

如果不交叉则打印错误消息

提示:......你将如何做到这一点?

不要回答我的问题。他们是暗示。写一些基于它们的代码。

答案 1 :(得分:1)

因此对于“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.

/* 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.*/
                }
            }
        }
    }
}
相关问题