簡體   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