- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我确信我的程序还有很多其他问题,但我目前陷入这个问题并专注于首先解决这个问题。我的目标是根据输入的单词数量循环游戏,同时改变游戏编号。我的问题是“显示横幅”部分一直循环,直到遇到“超出边界”错误。当我输入“java Hangman 123 test”时,它显示以下内容:
========
Game: 1
========
Game: 2
========
Game: 3
========
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Hangman.main(Hangman.java:121)
这是我的程序:
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
public class Hangman {
//validation of raw questions
public static boolean isValidQuestion(String rawQuestion){
rawQuestion = rawQuestion.toUpperCase();
boolean vQuestion = true;
for(int i=0; i<rawQuestion.length(); i++){
char c = rawQuestion.charAt(i);
if(!(c>='A' && c<='Z'))
vQuestion = false;
}
return vQuestion;
}
//show invalid tries
public static String formatCharArray(char[] ca){
String output = "";
for(int i=0; i<ca.length; i++){
output += ca[i] + " ";
}
return output;
}
public static void main(String args[]){
if(args.length==0){
System.out.println("Usage: java Hangman word_to_guess [more_word_to_quess]...");
return;
}
List<String> validQuestions = new ArrayList<>();
for (int i = 0; i < args.length; i++){
String rawQuestion = args[i];
boolean b = isValidQuestion(rawQuestion);
if (b){
validQuestions.add(rawQuestion);
}else{
System.out.println("Error: " + rawQuestion + ", input must be a word consist of a-z or A-Z");
}
}
// store valid questions into an array
String questions[] = validQuestions.toArray(new String[validQuestions.size()]);
// shuffle the questions
for(int i=0; i<7; i++){
int x = (int)(Math.random()*validQuestions.size());
int y = (int)(Math.random()*validQuestions.size());
String temp = questions[x];
questions[x] = questions[y];
questions[y] = temp;
}
// game
// initiallize
int noInvalidTry = 0;
int count = 0;
char[] invalidTries = new char[6];
int j = 0;
char[] userGuess = new char[questions[j].length() ];
boolean isCorrect = false;
for(int i=0; i<invalidTries.length; i++){
invalidTries[i] = '_';
}
for(int k=0; k<userGuess.length; k++){
userGuess[k] = '_';
}
这是我搞砸的部分。
// display banner
while( j<= args.length){
System.out.println( "========" );
System.out.println( "Game: " + ++j );
System.out.println("========");
}
以上
// game loop
while( noInvalidTry<6 && count<questions[j].length() ){
Scanner sc= new Scanner(System.in);
System.out.print("Please input your guess: ");
String s = sc.nextLine().trim();
// check input length
while(s.length()==1 && isValidQuestion(s)){
//start guess
char inputChar = s.charAt(0);
inputChar = Character.toUpperCase(inputChar);
boolean c = false;
for(int i=0; i<questions[j].length(); i++){
if((s.equals(questions[j].charAt(i))) && (userGuess[i] == '_')){
//update userGuess
userGuess[i] = questions[j].charAt(i);
c = true;
count++;
System.out.println( "Good try!" );
// show status
System.out.printf("No of Invalid try: %d/6\t\tInvalid tries: ",noInvalidTry);
System.out.println( formatCharArray(invalidTries) );
}
}
if(!c){
noInvalidTry++;
System.out.println( "Oops..." );
// show status
System.out.printf("No of Invalid try: %d/6\t\tInvalid tries: ",noInvalidTry);
System.out.println( formatCharArray(invalidTries) );
}
}
}
if( noInvalidTry==6 && count<questions[j].length()){
System.out.println("Game over, the answer is " + questions[j] );
j=j++;
}
if( noInvalidTry<6 && count==questions[j].length()){
System.out.println("Congratulation!");
j=j++;
}
//final status
if(j==args.length){
double temp = 0;
double score = 0;
double total = args.length;
System.out.println("-------------------------------------------------------------------------------------------------------------------");
System.out.println("Summary");
System.out.println("-------------------------------------------------------------------------------------------------------------------");
while(j<=args.length && isCorrect ){
System.out.println(++j + ". " + questions[j] + "\t" + "Correct" + "\t" + count + "/" + questions[j].length() + " Correct guesses" + "\t" + noInvalidTry + "/" + questions[j].length() + " Incorrect guesses" );
temp++;
}
while(j<=args.length && !isCorrect) {
System.out.println(++j + ". " + questions[j] + "\t" + "Incorrect" + "\t" + count + "/" + questions[j].length() + " Correct guesses" + "\t" + noInvalidTry + "/" + questions[j].length() + " Incorrect guesses" );
}
score = temp/total;
System.out.println("Final result: " + score + " / 100 marks");
System.out.println("-------------------------------------------------------------------------------------------------------------------");
}
}
}
最佳答案
if(j==args.length){
// ...
while(j<=args.length && isCorrect ){
System.out.println(++j + "" + questions[j] + "\t" + "Correct" + "\t" + count + "/" + questions[j].length() + " Correct guesses" + "\t" + noInvalidTry + "/" + questions[j].length() + " Incorrect guesses" );
temp++;
}
while(j<=args.length && !isCorrect) {
System.out.println(++j + ". " + questions[j] + "\t" + "Incorrect" + "\t" + count + "/" + questions[j].length() + " Correct guesses" + "\t" + noInvalidTry + "/" + questions[j].length() + " Incorrect guesses" );
}
如果j
是数组长度,那么questions[j]
将引发错误(最后一个元素是length- 1)
。
但是,更糟糕的是,您仍在进一步增加 j
(2 个 j++
语句)!
它们还有 while 循环中的许多其他 j++
。
作为帮助您处理数组的良好实践:当您对作为索引的值进行一些迭代时:
for
循环0
和 length-1
之间)j
) 只增加一次且每次只增加一次迭代。为了帮助您做到这一点,您的循环中应该只有一行声明 j++
或 ++j
,而不是在不同位置嵌套一些 j++
在某些 if
block 中。for
循环:有一些地方可以执行 j++
,并且如果没有 非常好的方法,您不应该在其他地方执行此操作原因。此外,正如评论中所述,您应该学会分析您的错误( What is a stack trace, and how can I use it to debug my application errors? ),并使用调试工具并逐步检查您的程序,看看您对所有变量做了什么。
关于java - (索引越界)我用java写了一个刽子手游戏程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47511868/
我正在制作刽子手游戏。一切正常,我已经准备好用于使游戏失败并为猜测提供 -1 的代码。虽然将它添加到 else 语句时它会重复等于单词的长度并且它也会给出一个猜测——即使它是正确的?我看不出代码有什么
我是一名 Javascript 学生,正在开发我的 Hangman 游戏。我用于显示 secret 单词的正确破折号数量的循环不起作用。每次只显示一个破折号。感谢帮助。谢谢。 $(document).
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关于您编写的代码问题的问题必须在问题本身中描述具体问题 — 并且包括有效代码 以重现它。参见 SSC
print("Welcome to hangman. Are you ready to have some fun?") def play(): import random List = ["ra
我刚刚开始学习 Java 作为我的第一门编程语言。 在类里面,我们被分配使用 while 和 for 循环制作一个基本的 Hangman 游戏。 到目前为止我有什么 当用户输入第一个猜测时,它确实识别
我是编程新手。我正在做一个经典的刽子手游戏,但我希望它是从节目中猜测一些角色的名字(名字和姓氏)。 我的问题:破折号打印 name 和 surname 之间的空格而不是忽略它,例如: 密语:鲁伯特·吉
我正在尝试创建一个刽子手游戏,其中每个字母都有一个按钮。我用绝对定位和多个 Action 监听器以困难的方式做到了这一点。有什么办法可以用 for 循环同时完成这两个任务吗? 另外,如何使用多态数组实
我是 Java 新手,所以这听起来可能很傻。 我正在尝试从 Java 创建一个基于 GUI 的刽子手游戏。我已经从数组创建了按钮列表,单击时它返回单个字母作为字符。我有要猜测的单词列表,每次按下按钮时
我已经研究这段代码有一段时间了。我尝试了许多不同的方法来查找玩家在随机生成的单词中正确猜测的输入的索引 - 我认为我当前编写的内容应该有效,但我担心我忽略了一个非常简单的错误。事实上,每当我运行代码时
在我的 Hangman 应用程序中,我为用户创建了一组按钮,然后从文件中随机选择 Hangman 应用程序中猜测的单词...该单词使用 JLabel 打印...但是一旦单击按钮时,应用程序会卡住。谁能
我正在为我的 C 课编程编写一个刽子手程序。我似乎不知道如何执行最后一步,即在玩家猜出所有正确的字母后停止游戏。我感觉问题出在播放功能上。 #include #include #inc
用 python 编写一个 Hangman 程序,在传入一个包含多单词字符串和单单词字符串的文件时遇到了一个问题。 文件: 你好棕色狐狸 狗 猫 水 跳跃 #initialize list wordL
所以我在玩这个游戏的特定部分时遇到了问题。这些是我的初始变量: var words = ["sugar","banana","hulk"]; var guessLeft = 12; var wrong
我是一名优秀的程序员,十分优秀!