我怎样才能将arraylist中的字符串与char类型的数组进行比较

时间:2017-07-22 05:05:07

标签: java

最近我开始学习java。经过一些知识,我从一些程序开始。所以我创建了一个Jumble Word游戏。它有效,但我有一个问题。这是我的代码......

import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;
class JumbleWords
{
public static void main(String args[])
{
    Scanner scan = new Scanner(System.in);
    ArrayList <String>alist = new ArrayList<String>();
    ArrayList <Character>chars = new ArrayList<Character>();
    Random rd = new Random();
    int listLimit,charLimit,value,ulimit=0,counter=0;
    String string,temp;
    alist.add("done");
    alist.add("nest");
    alist.add("rat");
    alist.add("cat");
    alist.add("hello");
    alist.add("cycle");
    alist.add("chain");
    alist.add("paint");
    alist.add("collect");
    alist.add("your");
    alist.add("gift");
    alist.add("card");
    alist.add("today");
    alist.add("cheer");
    alist.add("what");
    alist.add("time");
    alist.add("share");
    alist.add("build");
    alist.add("help");
    alist.add("success");
    alist.add("career");
    alist.add("access");
    alist.add("learn");
    alist.add("course");
    alist.add("year");
    alist.add("expert");
    alist.add("school");
    alist.add("floor");
    alist.add("season");
    alist.add("education");
    alist.add("spread");
    listLimit = alist.size();
    int i=0;
    System.out.println();
    System.out.println("How many JumbleWords you want to play...");
    System.out.println("Max limit is "+listLimit);
    ulimit = scan.nextInt();
    scan.nextLine();
    if(ulimit < listLimit )
    {
    while(i<ulimit )
    {
        value = rd.nextInt(listLimit);
        string = alist.get(value);
        for ( char c : string.toCharArray() )
        {
        chars.add( c );
        }
        Collections.shuffle(chars);
        Collections.shuffle(chars);
        System.out.println(chars);
        System.out.println("\nEnter the correct order of the word.");
        temp = scan.nextLine();
        if(string.equalsIgnoreCase(temp)==true){
            System.out.println("You Win......");
            System.out.println("(*^*)");
            System.out.println();
            ++counter;
        }
        else{
            System.out.println("You Lose......");
            System.out.println("The correct word is :-");
            System.out.println(string);
            System.out.println("(*_*)");
            System.out.println();

        }
        chars.clear();
        alist.remove(value);
        i++;
    }

    System.out.println("Your Score is "+counter+" out of "+ulimit);
    System.out.println();
}
else
{
    System.out.println("Not enough words we have...");
    System.out.println();
}
}
}

现在在“CHAIN”的情况下,用户必须输入获胜链但“CHINA”也是具有相同字符的单词。我怎样才能为此建立逻辑。

2 个答案:

答案 0 :(得分:0)

您可以比较整个单词:

while(i<ulimit )
{
    value = rd.nextInt(listLimit);
    string = alist.get(value);
    if(string.equalsIgnoreCase(value)){
        System.out.println("You Win......");
        System.out.println("(*^*)");
        System.out.println();
        ++counter;
    }
...

答案 1 :(得分:0)

首先,您的代码真的不易阅读 在实现特定事项时,变量名称选择非常糟糕: 文字游戏。
除了你过早声明你的变量并且范围太宽,这容易出错并且阅读也不容易

如何在不阅读整个代码的情况下理解这些陈述?

if(string.equalsIgnoreCase(temp)==true){

或者:

String wordToGuess = wordsToGuess.get(value);

应该是这样的:

if(wordToGuess.equalsIgnoreCase(userInput)){

和:

 ArrayList <String>alist = new ArrayList<String>();

而不是使用字符串列表。

WordToGuess

使用List<String> anagrams列表,其中每个实例将存储WordToGuess,其中public class WordToGuess{ private List<String> anagrams; ... public WordToGuess(String... anagrams){ this.anagrams = Arrays.asList(anagrams); } public String getAnyWord(){ return anagrams.get(0); } public boolean contains(String word){ return anagrams.contains(word.toLowerCase()); } } 可以声明:

 ArrayList <String>alist = new ArrayList<String>();

通过这种方式,您只需检查用户的输入是否包含在列表中。

所以

 List<WordToGuess> wordsToGuess = new ArrayList<WordToGuess>();

将成为:

wordsToGuess.add(new WordToGuess("done", "node")); // two possibilities
wordsToGuess.add(new WordToGuess("nest")); // one 
...
wordsToGuess.add(new WordToGuess("chain", "china")); // two
...

支持变量类型声明中的接口超过具体类。

你可以用这种方式填充你的列表:

String

比较输入String与预期的if(wordToGuess.equalsIgnoreCase(userInput)){

if(wordsToGuess.contains(userInput)){

将成为:

private GameObject orange;
private GameObject avocado;

最好将ignoring-case任务保留在客户端类中可能会忘记它的提供程序类中。