我希望能够在我的刽子手程序中猜出一个猜出的字母。因此,例如,如果单词是hello,它将作为 显示给播放器,因此,h会被播放器猜到它会表示是,但是然后显示字符串h ***有谁知道我怎么能做到这一点?这是我的代码:
Hangman课程:
/*
* 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);
}
//unstarredString = "Bee", you can do unstarredString.substring(0,1) + oldstr.substring(1,2)
}
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 + '}';
}
}
答案 0 :(得分:1)
首先,我建议您将Hangman代码拆分为两个文件。实施例
Hangman.java
package hangman;
import java.util.Scanner;
/**
*
* @author Adam2_000
*/
public class Hangman {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String selection;
Scanner scan = new Scanner(System.in);
/*
* I moved the variables found here to the Game class
*/
//New instances of class and arrays
Words words = new Words();
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("X: Exit"); // Just for user friendlyness
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 chosen easy:");
new Game(words.easyWords, scan); //All three levels can be called from the same class with the same code.
break; //This will help reduce amount of code, and help when you need to change all three at once.
case 'B':
System.out.println("You have chosen Medium");
new Game(words.mediumWords, scan); //Called with the String[] of words you want to use and the scanner to save memory.
break;
case 'C':
System.out.println("You have chosen Hard");
new Game(words.hardWords, scan);
break;
}
}
}
}
Game.java
package hangman;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Game {
String player = "";
int turn = 12;
List<String> wordBox = new ArrayList<String>();
boolean hangman = false;
Scanner scan;
Random random = new Random();
String letters; // For displaying letters
String mask; // For character Mask
public Game(String[] words, Scanner scanner){
scan = scanner;
int selectA = random.nextInt(words.length);
letters = words[selectA]; //Load word into String for display. This just makes the code more readable.
mask = letters.replaceAll("\\S", "*"); // Mask the words
System.out.println("Random String selected: " + mask);
System.out.println("This word contains " + letters.length() + " letters");
while (hangman == false) {
System.out.println("Turns remaining: " + turn);
System.out.println("Please choose a letter A-Z :");
String ChosenLetter = scan.next();
if (wordBox.contains(ChosenLetter)) {
System.out.println("Letter already chosen please choose another letter");
} else {
wordBox.add(ChosenLetter);
}
if (letters.contains(ChosenLetter)) {
char[] cLetters = letters.toCharArray(); //Load letters and mask to char array for editing
char[] cMask = mask.toCharArray();
for(int i=0; i < cMask.length; i++){
if(cLetters[i] == ChosenLetter.charAt(0)){
cMask[i] = cLetters[i];
}
}
mask = new String(cMask); //Load new mask into String
System.out.println("Wordbox letters are: " + wordBox);
System.out.println("Yes!" + mask); // Prints mask after Yes!
turn--;
} else {
System.out.println("Wordbox letters are: " + wordBox);
System.out.println("No!" + mask); //Prints mask after No!
turn--;
}
if (turn == 0) {
hangman = true;
}
while (hangman == true) {
System.out.println("You lose!");
System.exit(0);
}
}
}
}
接下来,我想告诉你我是如何回答你的问题的。
在上面的代码中,您将看到我已将单词加载到名为mask
和letters
的变量中。然后我在这段代码中使用了这些变量
char[] cLetters = letters.toCharArray();//Load letters and mask to char array for editing
char[] cMask = mask.toCharArray();
for(int i=0; i < cMask.length; i++){
if(cLetters[i] == ChosenLetter.charAt(0)){
cMask[i] = cLetters[i];
}
}
mask = new String(cMask); //Load new mask into String
System.out.println("Wordbox letters are: " + wordBox);
System.out.println("Yes!" + mask); // Prints mask after Yes!
并且没有
System.out.println("No!" + mask);
我希望这有用。并且代码很好。我喜欢你设置菜单的方式。
答案 1 :(得分:0)
您需要为每个级别执行此操作。基本上,它依靠indexOf
来找到单词中单词的位置并替换它。
if (easyWords1[selectC].contains(hardChosenLetter)) {
String word = easyWords1[selectC];
char[] unMasked = replaceAllHard.toCharArray();
unMasked[word.indexOf(hardChosenLetter)] = hardChosenLetter.charAt(0);
replaceAllHard = new String(unMasked);
System.out.println("Wordbox letters are: " + wordBox);
System.out.print("Yes!");
System.out.println(replaceAllHard);
turn--;
} else {
System.out.println("Wordbox letters are: " + wordBox);
System.out.println("No!");
turn--;
}