[英]How can i compare a string in arraylist with char type of array
Recently i started learning java. 最近,我开始学习Java。 And after some knowledge i starts with some program.
在了解了一些知识之后,我便开始了一些程序。 so i create a Jumble Word game.
所以我创建了一个Jumble Word游戏。 it works but i have a problem.
它有效,但是我有问题。 Here is my code....
这是我的代码。
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();
}
}
}
now in case of "CHAIN" is suffle and user must input chain for winning but "CHINA" is also a word with same chars. 现在,如果“ CHAIN”是松弛的,用户必须输入中奖链,但“ CHINA”也是一个具有相同字符的单词。 how can i build a logic for that.
我该如何建立一个逻辑。
You can compare the whole word: 您可以比较整个单词:
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;
}
...
First, your code is really not straight readable . 首先, 您的代码确实不是直接可读的 。
Variable names are very very bad chosen while you implement a specific matter : a word game. 在实现一个特定的问题(文字游戏)时,变量名称的选择非常非常糟糕。
Besides you declare your variables too early and have also a too broad scope, which is error prone and don't easy the reading too . 此外, 您过早声明变量,并且范围也太广, 这容易出错,也不太容易阅读 。
How to understand these statements without reading the whole code ? 如何在不阅读全部代码的情况下理解这些语句?
string = alist.get(value);
Or : 要么 :
if(string.equalsIgnoreCase(temp)==true){
It should be something like: 应该是这样的:
String wordToGuess = wordsToGuess.get(value);
and : 和:
if(wordToGuess.equalsIgnoreCase(userInput)){
Instead of using a List of String. 而不是使用字符串列表。
ArrayList <String>alist = new ArrayList<String>();
use a List of WordToGuess
where each instance will store a List<String> anagrams
where WordToGuess
could be declared : 使用的列表
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());
}
}
In this way you have just to check if the input of the user is contained in the List. 这样,您只需要检查用户的输入是否包含在列表中即可。
So 所以
ArrayList <String>alist = new ArrayList<String>();
will become : 会变成 :
List<WordToGuess> wordsToGuess = new ArrayList<WordToGuess>();
Favor the interface in the variable type declaration over concrete class. 在具体类型上偏爱变量类型声明中的接口。
And you could populate your List in this way : 您可以通过以下方式填充列表:
wordsToGuess.add(new WordToGuess("done", "node")); // two possibilities
wordsToGuess.add(new WordToGuess("nest")); // one
...
wordsToGuess.add(new WordToGuess("chain", "china")); // two
...
And the comparing input String
with the expected String
: 然后将输入的
String
与预期的String
进行比较:
if(wordToGuess.equalsIgnoreCase(userInput)){
will become : 会变成 :
if(wordsToGuess.contains(userInput)){
It is better to keep the ignoring-case task in the provider class that in the client class that may forget it. 最好将忽略大小写的任务保留在提供程序类中,而不是保留在可能忘记它的客户端类中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.