(Java)刽子手游戏:当循环逻辑似乎关闭时 背景:我正在通过赫尔辛基大学的大规模开放在线课程(MOOC)学习java。目标之一是建立一个基于文本的刽子手游戏。这里有更多的参考资料:
我算出了大部分代码。然而,游戏执行起来很笨拙。为了扩展,游戏应该一直运行,直到猜数或计数达到7为止。因此,while循环一直工作,直到它达到7或正确猜出单词为止 问题:我想说的是丰田。printWord()方法通过以下操作隐藏单词:******。每次用户猜到字母时,它都会删除星号。所以,如果我猜对了字母,它应该这样做:*o**o。然而,当用户输入下一个字母时,它忘记了猜到了“o”。因此,它目前的工作原理如下:(Java)刽子手游戏:当循环逻辑似乎关闭时 背景:我正在通过赫尔辛基大学的大规模开放在线课程(MOOC)学习java。目标之一是建立一个基于文本的刽子手游戏。这里有更多的参考资料:,java,Java,我算出了大部分代码。然而,游戏执行起来很笨拙。为了扩展,游戏应该一直运行,直到猜数或计数达到7为止。因此,while循环一直工作,直到它达到7或正确猜出单词为止 问题:我想说的是丰田。printWord()方法通过以下操作隐藏单词:******。每次用户猜到字母时,它都会删除星号。所以,如果我猜对了字母,它应该这样做:*o**o。然而,当用户输入下一个字母时,它忘记了猜到了“o”。因此,它目前的工作原理如下: User: o printWord(): *o*o** User: T printWo
User: o
printWord(): *o*o**
User: T
printWord(): T*****
这是我的主要课程:
public class main {
public static void main(String[] args) {
String word = "Toyota";
int count = 0;
Hangman hangman = new Hangman();
System.out.println("************");
System.out.println("* Hangman *");
System.out.println("************");
System.out.println("");
hangman.printMenu();
System.out.println("");
Scanner reader = new Scanner (System.in);
String input = reader.nextLine();
if (input.equals("quits")){
System.out.println("Thank you for playing!");
}else{
while (count < 7){
char c = reader.next().charAt(0);
hangman.printWord(c);
hangman.printMan(count);
hangman.printStatus(c);
count++;
}
}
}
}
公共类主{
公共静态void main(字符串[]args){
字符串word=“Toyota”;
整数计数=0;
刽子手刽子手=新刽子手();
System.out.println(“*************”);
System.out.println(“*Hangman*”);
System.out.println(“*************”);
System.out.println(“”);
hangman.printMenu();
System.out.println(“”);
扫描仪阅读器=新扫描仪(System.in);
字符串输入=reader.nextLine();
if(input.equals(“quits”)){
System.out.println(“谢谢你玩!”);
}否则{
而(计数<7){
char c=reader.next().charAt(0);
刽子手印刷字(c);
刽子手、印刷工(计数);
刽子手。打印状态(c);
计数++;
}
}
}
}
这是刽子手课:
public class Hangman {
public static void printMenu(){
System.out.println(" * menu *");
System.out.println("game on - starts the game.");
System.out.println("quit - quits the game");
System.out.println("status - prints the game status");
System.out.println("a single letter uses the letter as a guess");
System.out.println("an empty line prints this menu");
}
public static void printMan(int count){
String word = "Toyota";
if (count == 1) {
System.out.println("Wrong guess, try again");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("___|___");
System.out.println();
}
if (count == 2) {
System.out.println("Wrong guess, try again");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 3) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" | ");
System.out.println("___|___");
}
if (count == 4) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 5) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 6) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
}
if (count == 7) {
System.out.println("GAME OVER!");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | _|_");
System.out.println(" | / | \\");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
System.out.println("GAME OVER! The word was " + word);
}
}
public static void printStatus(char c){
String mystring = "abcdefghijklmnopqrstuvwxyz";
int count = 0;
int sum = 0;
StringBuilder sb = new StringBuilder();
sb.append(mystring);
for (int i = 0; i < sb.length(); i++){
if (c ==sb.charAt(i)){
sb.deleteCharAt(i);
}
}
count ++;
sum = sum + count;
System.out.println(sb.toString());
System.out.println("The number of guesses are: " + sum);
}
public void printWord(char c){
String word = "Toyota";
int count = 0;
char [] blank = new char [word.length()];
for (int i =0; i < word.length(); i++){
blank[i] = '*';
}
if (word.contains(c + "")){
for (int j = 0; j <word.length(); j++ ){
if (word.charAt(j) ==c){
blank[j] = c;
}
count++;
}
}else {
count++;
printMan(count);
System.out.println("Wrong guess");
}
if (word.equals(String.valueOf(blank))){
System.out.println(blank);
System.out.println("You won");
}
System.out.println(blank);
}
public static void gameOn(){
int count = 0;
if (count < 7){
System.out.println("Keep Trying. You can get this");
System.out.println(count);
}else {
System.out.println("Game Over bud");
System.out.println(count);
}
}
}
公共级刽子手{
公共静态无效打印菜单(){
System.out.println(“*菜单*”);
System.out.println(“游戏开启-启动游戏”);
System.out.println(“退出游戏”);
System.out.println(“状态-打印游戏状态”);
System.out.println(“单个字母使用该字母作为猜测”);
System.out.println(“空行打印此菜单”);
}
公共静态无效打印人员(整数计数){
字符串word=“Toyota”;
如果(计数=1){
System.out.println(“猜错了,再试一次”);
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println(“_124; u________;”);
System.out.println();
}
如果(计数=2){
System.out.println(“猜错了,再试一次”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“_124; u________;”);
}
如果(计数=3){
System.out.println(“猜错了,再试一次”);
System.out.println(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“_124; u________;”);
}
如果(计数=4){
System.out.println(“猜错了,再试一次”);
System.out.println(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
System.out.println(“| | | |”);
System.out.println(“|/\”);
System.out.println(“| | |”);
System.out.println(“|\\\\\\\\\/”;
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“|”);
System.out.println(“_124; u________;”);
}
如果(计数=5){
System.out.println(“猜错了,再试一次”);
System.out.println(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
System.out.println(“| | | |”);
System.out.println(“|/\”);
System.out.println(“| | |”);
System.out.println(“|\\\\\\\\\/”;
System.out.println(“| |”);
System.out.println(“| |”);
System.out.println(“|”);
System.out.println(“_124; u________;”);
}
如果(计数=6){
System.out.println(“猜错了,再试一次”);
System.out.println(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
System.out.println(“| | | |”);
System.out.println(“|/\”);
System.out.println(“| | |”);
System.out.println(“|\\\\\\\\\/”;
System.out.println(“| |”);
System.out.println(“| |”);
System.out.println(“|/\”);
System.out.println(“\\\\\\\\\\”;
}
如果(计数=7){
System.out.println(“游戏结束!”);
System.out.println(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
System.out.println(“| | | |”);
System.out.println(“|/\”);
System.out.println(“| | |”);
System.out.println(“|\\\\\\\\\/”;
System.out.println(“| | | |”);
System.out.println(“|/| \\”);
System.out.println(“|/\”);
System.out.println(“\\\\\\\\\\”;
System.out.println(“游戏结束!单词是“+单词”);
}
}
公共静态无效打印状态(字符c){
字符串mystring=“abcdefghijklmnopqrstuvxyz”;
整数计数=0;
整数和=0;
StringBuilder sb=新StringB
public class Hangman {
private String word;
private char[] blank;
private int guesses;
public Hangman() {
word = "Toyota";
blank = new char [word.length()];
for (int i =0; i < word.length(); i++){
blank[i] = '*';
}
guesses = 0;
}
// Optional Constructor - Hangman hangman = new Hangman("Toyota");
public Hangman(String word) {
this.word = word;
blank = new char [word.length()];
for (int i =0; i < word.length(); i++){
blank[i] = '*';
}
guesses = 0;
}
/**
* Rest of your code
*/
}
//String word = "Toyota";
int count = 0;
//char [] blank = new char [word.length()];
//for (int i =0; i < word.length(); i++){
// blank[i] = '*';
//}
public static void printStatus(char c){
String mystring = "abcdefghijklmnopqrstuvwxyz";
int count = 0;
StringBuilder sb = new StringBuilder();
sb.append(mystring);
for (int i = 0; i < sb.length(); i++){
if (c ==sb.charAt(i)){
sb.deleteCharAt(i);
}
}
guesses++;
System.out.println(sb.toString());
System.out.println("The number of guesses are: " + guesses);
}