我试图制作这个简单的游戏。但我不知道这段代码有什么问题。到目前为止,这是我的代码:
import java.util.*;
import java.lang.*;
public class letterguess {
private static String[] animals = {"snake","dog","cat","bird","whale","ant","wolf","bear","mouse","rabbit","elephant","dragonfly","kangaroo","tiger","komodo","koala","chicken","lion","horse","goat","cow","gorilla","camel","chipmunk","octopus","lobster","phanter","frog","zebra","lizard","baboon","deer","bison","hamster","hyena","shark"};
public static void main(String[] args) {
String [] true_answer = {"Right", "Awesome", "Cool"};
String [] false_answer = {"Wrong answer!", "Oops!"};
String [] word;
String [] show_word;
Random string = new Random();
int i, n=0, start, random=0, z=0, word_size, size;
char tryagain=0;
boolean output=true, main=true, error=true;
System.out.print ("Press 1 to start, 2 to exit: ");
Scanner input = new Scanner(System.in);
start = input.nextInt();
if (start==1){
main=true;
}
else{
System.exit(0);
}
while(main){
random = string.nextInt(animals.length);
word = animals[random].split("");
show_word = animals[random].split("");
for(word_size=0;word_size<animals[random].length();word_size++){
n=word_size;
}
for(i=1;i<animals[random].length();i++){
show_word[i] = "_";
}
if(n==3){
for(i=0;i<1;i++){
size = (int) (Math.random()*n);
show_word[size]=word[size+1];
}
}
else if(n==4){
for(i=0;i<2;i++){
size = (int) (Math.random()*n);
show_word[size]=word[size+1];
}
}
else if(n==5){
for(i=0;i<2;i++){
size = (int) (Math.random()*n);
show_word[size]=word[size+1];
}
}
else if(n==6){
for(i=0;i<3;i++){
size = (int) (Math.random()*n);
show_word[size]=word[size+1];
}
}
else if(n>=7){
for(i=0;i<4;i++){
size = (int) (Math.random()*n);
show_word[size]=word[size+1];
}
}
do{
for(i=0;i<animals[random].length();i++){
System.out.print(show_word[i]);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
System.out.print("\t 1");
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
System.out.print(" 2");
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
System.out.print(" 3");
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
System.out.print(" 4");
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
System.out.print(" 5 ");
System.out.print("\n Answer: ");
Scanner input2 = new Scanner(System.in);
String word3 = input2.nextLine();
if(word3.equals(animals[random])){
int True = (int) (Math.random()*2+1);
System.out.println(" ==> " +true_answer[True]);
output=false;
}
else{
int False = (int) (Math.random()*2+1);
System.out.println(" ==> "+false_answer[False]+"\n");
output=false;
}
}while(output);
System.out.print("\n Try Again (Y/N) : ");
Scanner input3= new Scanner (System.in);
tryagain = input3.next().toLowerCase().charAt(0);
if(tryagain=='y')
main=true;
else if(tryagain=='n')
main = false;
else
while(error){
System.out.print ("\n Wrong Input. Please input Y or N: ");
Scanner input4= new Scanner (System.in);
String next=input4.nextLine();
if(next.equals("Y")||next.equals("y"))
break;
else if(next.equals("N")||next.equals("n")){
error=false;
main=false;
}
else
continue;
}
}
System.out.print ("\t\t\t(\tGAME OVER\t)");
}
}
问题是,输出不是我预期的。 我希望大马 ho_s _ 或大象 _l_ph ___ ,但它显示的内容类似于 or_e _ > 马和 ee ___ nt _ elephant 。
有人可以帮我解决这个问题吗?我已经尝试过任何我知道但仍无法解决的问题。我很抱歉,谢谢你。
答案 0 :(得分:3)
虽然很高兴能够运行所有东西,但是将来,你应该考虑专注于问题 - 代码的一部分,这是间距的显示,它只有3行,更容易调试。
for(i=1;i<animals[random].length();i++){ //<--
show_word[i] = "_";
}
这个for循环是个问题。 i
应该从0开始。
for(i=0;i<animals[random].length();i++){ //<--
show_word[i] = "_";
}
修改输出:
horse
___s_ 1 2 3 4 5
Answer: horse
修改的
以下是调整问题的错误:
for (word_size = 0; word_size < animals[random].length(); word_size++) {
n = word_size;
}
您尝试将n =设置为字大小,但您提供的逻辑不准确。你实际上得到了word_size - 1
因此,使用3个字母的单词,您将得到2,这意味着不会替换任何字母。
您可以将所有这些替换为:
n = animals[random].length(); //returns 3 for cat