-3

我想比较两个用户定义的字符串并输出两个字符串之间共享的字符数,而不使用数组。然后我需要输出这些字符中的每一个。我理解使用 a 的用户输入部分Scanner,但之后我一无所知。如果您不想提供实际代码,伪代码就足够了。

例如,“hamper”作为 string1,“happened”作为 string2 将返回

共享字符数 = 4 (5)

共享字符>>“h”、“a”、“p”、“e”(“h”、“a”、“p”、“p”、“e”、“e”)

(任何一种方式都足够了)

4

1 回答 1

3

假设不是唯一匹配:

//get word1
//get word2
//set variable for character count:  count = 0
//for each character in word1
    //for each character in word2
        //if the characters are the same
            //print character
            //increment count
//print count

假设只有唯一的匹配:

//get word1
//get word2
//set variable for character count:  count = 0
//create empty list of already found characters:  found_list = {}
//for each character in word1
    //for each character in word2
        //if the characters are the same
            //if character is not in found_list
                //print character
                //add character to found_list
                //increment count
//print count

我建议查找for loops.charAt()方法:

http://www.tutorialspoint.com/java/java_loop_control.htm

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#charAt%28int%29

于 2013-10-22T19:01:58.757 回答