Java 如何检查数组上的重复字符串
我对java有点陌生,我正在尝试制作一个猜测游戏,寻找User01的副本。我遇到了一个问题,我不知道如何解决这个问题。我的目标是检查User01是否已经输入了那个特定的单词。以下是我目前的代码:Java 如何检查数组上的重复字符串,java,arrays,Java,Arrays,我对java有点陌生,我正在尝试制作一个猜测游戏,寻找User01的副本。我遇到了一个问题,我不知道如何解决这个问题。我的目标是检查User01是否已经输入了那个特定的单词。以下是我目前的代码: public static void main(String[] args) throws InterruptedException{ int k = x; boolean Given = false; boolean Given2 = false; //Playerone an
public static void main(String[] args) throws InterruptedException{
int k = x;
boolean Given = false;
boolean Given2 = false;
//Playerone and x are in Global Declarations.
for(int j = 0; j < x; j++, k--){
if(j == 0){
System.out.print("Please enter " + k + " words that Player 2 will Guess:");
Playerone[j] = input.nextLine();
System.out.println(j);
}
else if(j == x-1){
System.out.print("Last one:");
Playerone[j] = input.nextLine();
System.out.println(j);
}
else {
System.out.print(k + " more words:");
Playerone[j] = input.nextLine();
System.out.println(j);
}
do {
int Duplicates = 0;
while(Duplicates > j && Playerone[Duplicates] == Playerone[j]){
Duplicates++;
}
Given2= Duplicates < j;
if(Given2 == false){
Given2 = true;
System.out.println("It's already given");
Playerone[j] = input.nextLine();
}
}while(Given2 = true);
}
publicstaticvoidmain(String[]args)抛出InterruptedException{
int k=x;
布尔给定=假;
布尔给定n2=假;
//Playerone和x在全球声明中。
对于(int j=0;jj&&playeron[Duplicates]==playeron[j]){
复制++;
}
Given2=重复项
我尝试将do放置在for循环的开始下方,但它没有修复我遇到的问题。该条件存在问题:
Duplicates>j
这总是错误的,不允许重复+++
另外,Duplicates=0;
每次User1给出一个单词时都会发生,因此无论如何,这永远不会用于计算重复项
所有移动中的第一个移动Duplicates=0;
在循环的第一个之前
所以我要做的不是最后一件事,而是:
Given=false;
for(int c=0;c<=j;c++){
while(Playerone[j]==Playerone[c])
//duplicate found
System.out.println(“Already exists”);
Playerone[j]=input.nextLine();
Given=true;
}
//these loop also prevent user to give again a word that already exists
}
if(Given) Duplicates++;
Given=false;
对于(int c=0;c