从hang []数组中删除所有非唯一字母

时间:2013-12-06 03:15:24

标签: java arrays

我无法理解如何从char数组中删除一个字母,这是我的代码

import java.util.Random;
import java.util.Scanner;


public class Main {

    public static void main(String [] args)
    {

        start();

    }
    public static void start()
    {
        Scanner scan = new Scanner(System.in);
        Random rand = new Random();
        String [] Words = {"Dog","cat","Food","Bacon","Turkey","hood","poo","Good","look"};
        String RandomWord = Words[rand.nextInt(Words.length)];
        char [] array = RandomWord.toCharArray();
        boolean [] parrallelArray = new boolean[array.length];
        int i = 0;

        int placeholder = 0;
        System.out.println(findUniqueLetters(array));
        char input = 0;
        while(i<findUniqueLetters(array)){
            i++;


            System.out.println("You have a "+RandomWord.length()+" Word "+RandomWord);
            System.out.println("Guess a letter : ");
            input = scan.next().charAt(0);


            for(int j = placeholder; j<array.length;j++){


                if(input == array[j]){
                    j++;
                    placeholder = j;
                    System.out.println("You got it right");

                    break;
                }

                else if(!(input ==array[j])){
                        j++;
                        placeholder = j;
                        System.out.println("You got it wrong");
                        break;

                }

            }
        }
        System.out.println("You have wasted all your tries!");

    }

    public static int findUniqueLetters(char [] a){
        int Unique = 1;

        for(int i = 1; i<a.length;i++){
            if(!(a[i] == a[i-1])){
                Unique++;

            }

        }   
        return Unique;
    }


}

是否有其他方法可以执行此操作或删除它是唯一的方法吗? 我试图用数组中的非重复字母切换数组中的重复字母,但这只适用于某些单词。

1 个答案:

答案 0 :(得分:1)

请使用套装。更多信息 - &gt; http://docs.oracle.com/javase/7/docs/api/java/util/Set.html