繁体   English   中英

如何只打印某些字母

[英]how to print only certain letters

所以我要执行此任务,在这里我必须输入两个字符串,然后我必须找到存在的常见字母,然后将它们写出来,但只能写一次。.例如,如果字符串1是“拟声词”而字符串2是“对话”我应该回去:o,n,a,t,e,i ...我唯一的问题是最后一部分(“我不知道怎么只写一次字母)

这是我的代码

import java.util.Scanner;
import java.util.Arrays;

public class Zadatak4 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        char niz[] = new char[100];
        char niz2[] = new char[100];

        System.out.print("Add the first string: ");
        niz = scan.nextLine().toCharArray();

        System.out.print("Add the second string: ");
        niz2 = scan.nextLine().toCharArray();

        for (int i = 0; i < niz.length; i++) {

            for (int j = 0; j < niz2.length; j++) {

                if (niz[i] == niz2[j]) {
                    System.out.println(niz[i] + " ");

                    // What now!?!?!?
                }

            }
        }

    }

}

使用一组:

LinkedHashSet<string> printNum = new LinkedHashSet<string>();
if(niz[i] == niz2[j])
{
      printNum.add( niz[i] );
}

// outside of loop
for( string s : printNum )
{
      System.out.println(s);
}

您可以通过利用两个HashSets来做到这HashSets

每个单词有一个哈希集。 当您在word1中遇到字母时,请输入set1。 在word2中遇到字母时,请输入set2。

最后,您只保留两组中的字母。

import java.util.HashSet;
public class Zadatak4 {

    /**
     * @param args
     */
    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);
        char niz[] = new char[100];
        char niz2[] = new char[100];

        System.out.print("Add the first string: ");
        niz = scan.nextLine().toCharArray();

        System.out.print("Add the second string: ");
        niz2 = scan.nextLine().toCharArray();

        HashSet<Integer> set1 = new <String>HashSet();
        HashSet<Integer> set2 = new <String>HashSet();


        for(int i = 0; i < niz.length; i++)
        {
            if(!set1.contains(niz[i]));
            set1.add((int) niz[i]);         
        }

        for(int i = 0; i < niz2.length; i++)
        {
            if(!set2.contains(niz2[i]));
            set2.add((int) niz2[i]);            
        }


        Iterator<Integer> it = set1.iterator();
        int currentChar;
        while(it.hasNext())
        {
            currentChar = it.next();
            if(set2.contains(currentChar))
            System.out.println((char)currentChar);
        }
    }

}

在for循环的最里面,您需要将它们添加到集合中

mutuals.add(niz[i])

然后在循环的开头添加此内容以对其进行声明

Set<char> mutuals = new HashSet<char>()

确保在循环外执行此操作

然后,将所有内容打印出来

几乎每个人都建议Set ,这是完成此任务的困难方法...

public static void main(String[] args) {

    String printed = "";

    Scanner scan = new Scanner(System.in);
    char niz[] = new char[100];
    char niz2[] = new char[100];


    System.out.print("Add the first string: ");
    niz = scan.nextLine().toCharArray();

    System.out.print("Add the second string: ");
    niz2 = scan.nextLine().toCharArray();


    for(int i = 0; i < niz.length; i++)
    {
        for(int j = 0; j < niz2.length; j++)
        {
                if(niz[i] == niz2[j])
                {                        
                    if(printed.indexOf(niz[i]) == -1) {
                           System.out.println(niz[i]+" ");
                    }

                    printed += niz[i];
                }
        }
    }

您需要的是两个集合的交集,因此可以使用Set.retainAll()

一班轮:

HashSet<Character> common =
    new HashSet<Character>(Arrays.asList(niz1)).retainAll(
        new HashSet<Character>(Arrays.asList(niz2)));

将字符存储在Set中

 Set<Character> cs=new HashSet<>();

 if(niz[i] == niz2[j])
 {
     cs.add(niz[i]); 
     //System.out.println(niz[i]+" ");

     //What now!?!?!?
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM