我目前正在用java编写一个hangman控制台游戏,到目前为止还没有遇到任何问题。
我现在遇到了一个似乎无法解决的问题。
我有不同单词的数组容易,中等和困难,并根据选择的难度随机从他们得到一个单词。然后我把这个单词转换成*'s,这样玩家就看不到角色了。
然后向用户索要信件,程序会通知用户信件是对还是错。但问题是,如果*在单词中,我似乎无法将*改为字母。例如,如果单词是“蜜蜂”,玩家选择了b,我想*改为b。
有人能帮我指出正确的方向吗?
刽子手级:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hangman;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Adam2_000
*/
public class Hangman {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String player = "";
String selection;
int turn = 12;
List<String> wordBox = new ArrayList<String>();
boolean hangman = false;
Scanner scan = new Scanner(System.in);
//New instances of class and arrays
words words = new words();
String easyWords1[] = words.easyWords;
String mediumWords1[] = words.mediumWords;
String hardWords1[] = words.hardWords;
Random random = new Random();
System.out.println("Welcome to Hangman version 1");
System.out.println("Please choose a difficulty");
System.out.println("A: Easy");
System.out.println("B: Medium");
System.out.println("C: Hard");
System.out.println(" _________ ");
System.out.println("| | ");
System.out.println("| 0 ");
System.out.println("| /|\\ ");
System.out.println("| / \\ ");
System.out.println("| ");
System.out.println("| ");
char iChoice;
do {
selection = scan.nextLine().toUpperCase();
} while (selection.isEmpty());
iChoice = selection.charAt(0);
if (iChoice != 'X') {
switch (iChoice) {
case 'A':
System.out.println("You have choosen easy:");
//Get and print random string from easyWords
int selectA = random.nextInt(easyWords1.length);
String replaceAllEasy = easyWords1[selectA].replaceAll("\\S", "*");
// Print the randomly selected word and its length
System.out.println("Random String selected: " + replaceAllEasy);
System.out.println("This word contains " + easyWords1[selectA].length() + " letters");
//while hangman == false continue to ask player for letters
while (hangman == false) {
System.out.println("Turns remaining: " + turn);
System.out.println("Please choose a letter A-Z :");
String easyChosenLetter = scan.next();
if (wordBox.contains(easyChosenLetter)) {
System.out.println("Letter alreay choosen please choose another letter");
} else {
wordBox.add(easyChosenLetter);
}
if (easyWords1[selectA].contains(easyChosenLetter)) {
System.out.println("Wordbox letters are: " + wordBox);
System.out.println("Yes!");
turn--;
} else {
System.out.println("Wordbox letters are: " + wordBox);
System.out.println("No!");
turn--;
}
if (turn == 0) {
hangman = true;
}
while (hangman == true) {
System.out.println("You lose!");
System.exit(0);
}
}
break;
case 'B':
System.out.println("You have choosen Medium");
//Get and print random string from mediumWords
int selectB = random.nextInt(mediumWords1.length);
String replaceAllMedium = mediumWords1[selectB].replaceAll("\\S", "*");
// Print the randomly selected word and its length
System.out.println("Random String selected: " + replaceAllMedium);
System.out.println("This word contains " + mediumWords1[selectB].length() + " letters");
//while hangman == false continue to ask player for letters
while (hangman == false) {
System.out.println("Turns remaining: " + turn);
System.out.println("Please choose a letter A-Z :");
String mediumChosenLetter = scan.next();
if (wordBox.contains(mediumChosenLetter)) {
System.out.println("Letter alreay choosen please choose another letter");
} else {
wordBox.add(mediumChosenLetter);
}
wordBox.add(mediumChosenLetter);
if (easyWords1[selectB].contains(mediumChosenLetter)) {
System.out.println("Wordbox letters are: " + wordBox);
System.out.print("Yes!");
turn--;
} else {
System.out.println("Wordbox letters are: " + wordBox);
System.out.println("No!");
turn--;
}
if (turn == 0) {
hangman = true;
}
while (hangman == true) {
System.out.println("You lose!");
System.exit(0);
}
}
break;
case 'C':
System.out.println("You have choosen Hard");
//Get and print random string from hardWords
int selectC = random.nextInt(hardWords1.length);
String replaceAllHard = hardWords1[selectC].replaceAll("\\S", "*");
// Print the randomly selected word and its length
System.out.println("Random String selected: " + replaceAllHard);
System.out.println("This word contains " + hardWords1[selectC].length() + " letters");
//while hangman == false continue to ask player for letters
while (hangman == false) {
System.out.println("Turns remaining: " + turn);
System.out.println("Please choose a letter A-Z :");
String hardChosenLetter = scan.next();
if (wordBox.contains(hardChosenLetter)) {
System.out.println("Letter alreay choosen please choose another letter");
} else {
wordBox.add(hardChosenLetter);
}
wordBox.add(hardChosenLetter);
if (easyWords1[selectC].contains(hardChosenLetter)) {
System.out.println("Wordbox letters are: " + wordBox);
System.out.print("Yes!");
turn--;
} else {
System.out.println("Wordbox letters are: " + wordBox);
System.out.println("No!");
turn--;
}
if (turn == 0) {
hangman = true;
}
while (hangman == true) {
System.out.println("You lose!");
System.exit(0);
}
}
break;
}
}
}
}
单词类别:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hangman;
import java.lang.reflect.Array;
import java.util.Random;
/**
*
* @author Adam2_000
*/
public class words extends Hangman {
String[] easyWords = {"Bee", "Car", "Fish", "Shed"};
String[] mediumWords = {"House", "Sheep", "Castle", "Phone"};
String[] hardWords = {"Octagon", "Crocodile", "Chocolate", "Motorbike"};
public String[] getEasyWords() {
return easyWords;
}
public void setEasyWords(String[] easyWords) {
this.easyWords = easyWords;
}
public String[] getMediumWords() {
return mediumWords;
}
public void setMediumWords(String[] mediumWords) {
this.mediumWords = mediumWords;
}
public String[] getHardWords() {
return hardWords;
}
public void setHardWords(String[] hardWords) {
this.hardWords = hardWords;
}
@Override
public String toString() {
return "words{" + "easyWords=" + easyWords + ", mediumWords=" + mediumWords + ", hardWords=" + hardWords + '}';
}
}
您必须将字符串 "***" 替换为“B**”,而不是更改字符串的第一个字符。字符串在Java中是不可变的。
String oldstr = "***";
String newstr = 'B'+oldstr.substring(1,2);
可能字符串
不是此任务的正确数据结构。也许您想使用字符数组之类的东西,编写自己的print
函数。
即使是包含这些字符数组的Word
类也是一个好主意,因为每个对象都可以有自己的索引,告诉您还有多少*'s
可以猜测,等等。
半伪为Word
类。这实际上使用了String
s。未经测试。
public class Word{
private String name;
private String guessedSoFar;
public String getGuessedSoFar(){
return guessedSoFar;
}
public void changeIndexWith(String achar, int index){
String temp = name.substring(0,index) + achar + name.substring(index+1, name.length();
guessedSoFar = temp;
}
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
}
我已经被这段代码困扰了一段时间,非常感谢您的帮助。因此,基本上我的刽子手代码运行良好,但控制台中的显示,破折号应该反映哪些字母猜对了,哪些字母仍然需要猜对,这是完全错误的。我将在这里发布我的代码。由于时间很长,我将只发布相关的方法。我觉得问题似乎出在显示数组列表上,我无法持续更新它。
嗨,我是编程新手,我们的任务是制作一个刽子手游戏。现在我面临的问题是猜测。程序猜对了一切,并且工作正常。虽然它不会打印出中的字母,或者它会打印出1个字母,但当我猜正确时,上一个字母会被覆盖。 那么,有谁能伸出援助之手呢? 这是我的检查代码: }
我是java新手,我正在尝试创建一个基本的刽子手游戏,我已经为这一轮创建了方法,但我一直在创建一个布尔值,该值将使游戏进入下一轮(如果玩家输入了错误的字母,他将失去一次猜测字母的尝试;他可能只会出错10次;如果猜测,他将继续游戏)。我的代码到目前为止,我甚至尝试通过if/else语句排除布尔值,但对我来说它似乎非常愚蠢。任何帮助都是有用的。非常感谢。
在我的游戏中出现的问题是,当我试图在游戏中插入单词时,它不显示字母,它应该在正确的时候显示字母,如果它是错误的,它应该通过增加刽子手的一部分来惩罚我。 我创建了惩罚函数和函数,当它正确时显示用户输入的字母,请帮助!! 这是我的代码:http://pastebin.com/du98i88G 我认为在pygame中的函数` 中可能存在问题,而不是gameQuit:for event。事件get():i
我想做一个刽子手游戏。每转一圈,用户猜一个字母。如果他们猜对了,他们可以再猜一次。否则,它们剩余的可用猜测将减少1,猜测的字母将放入一个数组中。 当用户点击零剩余猜测时,程序应该询问他们是否想再次玩。如果他们回答“是”,则应重新启动游戏;如果他们回答“否”,则程序结束。 我遇到的问题是用于猜测的数组,以及重新启动游戏。 此外,程序启动时每隔一段时间都会出错。错误是 线程“main”java中出现异
我正在做一个刽子手游戏。我几乎完成了,但我编写的代码,无论何时,只要隐藏的单词中有两个或两个以上的点需要用猜测的字母替换,都只适用于第一个。例如,如果要查找的单词是“ball”,用户输入“b”和“l”,而不是“b_ll”,那么他将返回的隐藏单词是“b_l_”。这是一种奇怪的练习,因为用户首先键入字母,每次猜测后都没有得到反馈。这是密码。 先谢谢你。