- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 java 类中使用我的 hangman 程序时遇到问题我似乎没有让失去的生命计数器正常工作并且显示已经猜到的正确的字母在你制作下一个时不按顺序显示正确的猜测。
import java.util.Scanner; //imports scanner so I could use user input
import java.util.Random; // imports random picker for words, to pick a random word
public class javaxx {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
StringBuffer buffer = new StringBuffer();
String wordToGuess;
int wordLength;
int wordToGuessLength;
int position;
int livesLost = 0;
int totalLives = 7;
int lettersRemaining;
boolean guessInWord;
char guess;
StringBuffer prevGuessedLetters;
//declare variable
//String wordToGuess[] = new String[29];
//insert array for words and their values
String[] myStringArray = new String[]{"programming","exhaustive","violin","selection","repetition","serendipity","watermelon","football","mobilephone","handbag","teddybear","cardigan","waterfall","cupcake","pineapple","strawberry","collection","chicken","tablecloth","candlestick","notebook","radiator","champagne","wineyard","parent","circus","snowbell","clocktower","mermaid","cardigan"};
//Display the rules of the game
System.out.println("You are playing the game Hang Man. You have to guess the letters in the word. You will see how many letters are in the word and you have 7 changes to be wrong. ");
//choose a random word from the array
wordToGuess = myStringArray[(int) (Math.random() * myStringArray.length)];
//determine the length of the word
wordLength = wordToGuess.length();
//show the player how many letters are in the word
System.out.println("The word you are quessing has " + wordLength + " letters in it");
lettersRemaining = wordLength;
for (position = 0; position < wordLength; position++) {
sb.append("_ ");
}
System.out.println(sb.toString());
//loop starts
while (lettersRemaining > 0 && livesLost < 7) {
//prompt user to guess a letter
System.out.println("Guess a letter:");
guess = myScanner.findWithinHorizon(".", 0).charAt(0);
//check if the letter guessed is in the secretWord
guessInWord = (wordToGuess.indexOf(guess)) != -1;
if (guessInWord == false) {
livesLost++;
System.out.print("Sorry, you have lost a life. You still have ");
System.out.print(totalLives -= livesLost);
System.out.println(" life/lives left. Keep trying.");
} else {
System.out.println("That was a good guess, well done!");
for (position = 0; position < wordLength; position++) {
if (wordToGuess.charAt(position) == guess) {
System.out.print(guess);
lettersRemaining--;
} else {
System.out.print("_ ");
}
}
}
System.out.println();
prevGuessedLetters = buffer.append(guess);
System.out.print("Previously guessed letters: ");
System.out.println(prevGuessedLetters);
System.out.print("Letters remaining: ");
System.out.println(lettersRemaining);
}
if (livesLost == totalLives) {
System.out.println("Sorry, you lose!");
} else {
System.out.print("Well done, you win! The word was ");
System.out.println(wordToGuess);
}
}
}
最佳答案
改变这一行
System.out.print(totalLives -= livesLost);
到
System.out.print(totalLives - livesLost);
这是 livelost 计数器的问题。
关于java源代码刽子手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29878135/
我正在制作刽子手游戏。一切正常,我已经准备好用于使游戏失败并为猜测提供 -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
我是一名优秀的程序员,十分优秀!