我正在用Java编写Hang Man游戏的代码。但是我无法编写正确的代码或找到计算用户的方法'在用户处打分并打印#39;结束命令0:停止。这是分数计算规则;
分数计算为单词的字母数减去当前显示的字母数。 这里“table”得分是5-4 = 1,因为当用户猜测时已经显示了4个字母。
如果用户猜测不正确,则得分为0 如果男子的数字在猜测之前完成,则得分为0
每个游戏玩法同一个用户是一个会话,同一个词不能由计算机持有。 如果“table”被视为“事物”,它可以在同一会话中由计算机再次保存。
当用户在主要按钮中按0时,会话结束。
此外,我有一个问题,以防止随机选择的单词数组
重复import java.util.Random;
import java.util.Scanner;
public class CEVIK_CAGATAY{
public static char[] star;
public static void main (String args[])
{
char game[];
int category;
int correct=0;
Scanner input = new Scanner(System.in);
Random r = new Random();
int totalplay=0;
int totalscore=0;
String man[] = new String[7];
man[2] = " --\n o |\n/ |\n |\n_____\n";
man[3] = " --\n o |\n/| |\n |\n_____\n";
man[4] = " --\n o |\n/|\\|\n |\n_____\n";
man[5] = " --\n o |\n/|\\|\n/ |\n_____\n";
man[6] = " --\n o |\n/|\\|\n/ \\|\n_____\n";
String arr[]={"serhat","cagatay","begum","emre","berk","ali","veli","istanbul",
"ankara","mersin","izmir","antalya","new york","samsun","kedi","kopek",
"kus","ayi","bocek","karinca","manda","masa","pencil","bag","clock","televison","book","glass"};
int arr1 []={6,7 };
System.out.println("0 To Stop,1 to Continue");
category=input.nextInt();
while(category!=0){
System.out.println("0 To STop,1 to Continue");
String word = arr[r.nextInt(arr.length)];
for(int i=0; i<arr.length;i++) {
if(arr[i]==word){
int letterlength=arr1[i];
}
//letterlength-existedlength=guessremain;
}
int count = word.length();
char[] CharArr=word.toCharArray();
char[] star = word.toCharArray();
for(int i=0;i<star.length;i++)
{
star[i] = '*';
System.out.print(star[i]);
}
for (int i=1; i<=5; i++)
{
System.out.printf ("\nGuess a Letter:");
char letter= input.next().charAt(0);
for (int j=0;j<CharArr.length; j++)
{
if(letter == star[j])
{
System.out.println("this word already exist");
}
else
{
if(letter==CharArr[j])
{
star[j]=letter;
i--;
System.out.printf("CORRECT GUESS!\n");
correct++;
}
}
}
System.out.print(star);
switch(i+0)
{
case 1: System.err.printf("Strike 1\n");
System.out.println(man[2]);
break;
case 2: System.err.printf("Strike 2\n");
System.out.println(man[3]);
break;
case 3: System.err.printf("Strike 3\n");
System.out.println(man[4]);
break;
case 4: System.err.printf("Strike 4\n");
System.out.println(man[5]);
break;
case 5: System.err.printf("Strike5\n");
System.err.printf("You're out!!! The word is Not_Matched\n");
System.out.println(man[6]);
break;
}
System.out.printf("\n");
if((new String(word)).equals(new String(star)))
{
System.err.printf("Winner Winner\n");
break;
}
}
totalplay++;
totalscore+=correct;
System.out.println("CONTINUE 1,STOP 0");
category=input.nextInt();
if(category==0) {
System.out.println(totalplay);
System.out.println(totalscore);
}
}
}
}
答案 0 :(得分:0)
摆脱以下for循环。除了抛出Array Index Out of Bounds错误之外没有任何意义。你的程序没有它运行。一定要练习语言的最佳实践(比如用于命名变量的camelCase)。它使您的代码更易于人类理解。
for(int i=0; i<arr.length;i++) {
if(arr[i]==word){
int letterlength=arr1[i];
}
//letterlength-existedlength=guessremain;
}