真正的初学javascript用户在这里,创建一个刽子手游戏。我试图向用户展示正确的猜测时遇到了困难。 根据我的理解,我只是用破折号掩盖了这个秘密词,所以当我猜到一个正确的字母时,我试图让它变得不被掩盖。 我想我需要在某个地方使用charAt,不过要说实话,我只是想不通。
我的代码仍然是非常基本的,我没有做太多其他因为如果你看不到猜测没有多少点写出游戏的其余部分,但这里是我到目前为止的代码...请记住这仍然是一个非常未完成的项目。
package hangmangame;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Matt
*/
public class HangmanGame {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
char letter = 0; //declares and initailise letter
String marks = ""; //declares and initailise string for dashes
String [] words = { "gluttony", "lust", "greed", "pride", "despair", "wrath", "vainglory", "rhythm", "delicious", "better", "jacuzzi" , "ironman", "captainamerica", "thor", "hulk", "spiderman", "antman", "batman"}; //declares and initailise array of words to guess
String word = words[(int) (Math.random() * words.length)]; //chooses random word from the word array
for (int i=1;i<=word.length(); i++) // for method for displaying the correct word as dashes
{
marks += "-"; //dashes to represent the correct word.
}
System.out.println("lets play hangman, your word is " + marks + "\n" + "enter a letter to guess the word");
Scanner input = new Scanner(System.in);
letter = input.next(".").charAt(0); //assign inputted letter to letter variable
if ((word).contains(""+letter)) //if statement to excute if guessed letter is in word
// i imagine this is where i put some sort of code to show that guessed letter?
System.out.println("You guessed a letter!" + marks); //display for correct letter
答案 0 :(得分:0)
String类包含一个indexOf(String c)
方法,该方法返回第一次出现的子字符串的索引。例如,如果word
为delicious
,则word.indexOf("i")
将返回3
,即子字符串"i"
的第一个索引。
但是子串"i"
的所有其他出现怎么办?好吧,indexOf
方法被轻易覆盖以帮助解决这个问题。它的另一个版本indexOf(String ch, int fromIndex)
包含一个起始索引。继续我们之前的示例,如果您这次要求word.indexOf("i", 4)
,那么如果我们打折每个字符串5
,那么字符串"i"
中的子字符串"delicious"
的第一个索引指数在第四个之前。
可以这样想,indexOf与charAt相反。 charAt接受一个索引并给你一个字符,indexOf接受一个字符或字符串并给你第一个索引。
答案 1 :(得分:0)
以下是一些可以帮助您入门的代码。从用户那里获取整行而不是使用next()是很重要的...除非你是一位经验丰富的编码员并且理解下一个()迭代输入的方式,我强烈建议使用nextLine(),因为它是更容易使用。
char letter = 0; //declares and initailise letter
String [] words = { "gluttony", "lust", "greed", "pride", "despair", "wrath", "vainglory", "rhythm", "delicious", "better", "jacuzzi" , "ironman", "captainamerica", "thor", "hulk", "spiderman", "antman", "batman"}; //declares and initailise array of words to guess
String word = words[(int) (Math.random() * words.length)]; //chooses random word from the word array
String [] marks = new String[word.length()];
for (int i=0;i<word.length(); i++) // for method for displaying the correct word as dashes
{
marks[i] = "-"; //dashes to represent the correct word.
}
HashSet<Character> lettersGuessed = new HashSet<>(); //keep track of letters guessed
Scanner input = new Scanner(System.in);
String userInput = "";
String currentWord = "";
while(true)
{
System.out.print("Word is - ");
currentWord = "";
for(int i = 0; i < marks.length; i++)
{
System.out.print(marks[i]);
}
System.out.print("\nGuess a letter - ");
userInput = input.nextLine(); //always grab lines
if(userInput.length() != 1)
{
System.out.println("Invalid guess - " + userInput);
}
else if(lettersGuessed.contains(userInput.charAt(0)))
{
System.out.println("You already guess that character - " + userInput);
}
else if(word.contains(userInput))
{
lettersGuessed.add(userInput.charAt(0));
currentWord = "";
for(int i = 0; i < word.length(); i++)
{
if(word.charAt(i) == userInput.charAt(0))
{
marks[i] = "" + userInput.charAt(0);
}
currentWord += marks[i];
}
}
if(currentWord.equals(word))
break;
}
System.out.println("You guessed it! The word was " + word + "!");
输出
Word is - ----
Guess a letter - l
Word is - --l-
Guess a letter - h
Word is - h-l-
Guess a letter - l
You already guess that character - l
Word is - h-l-
Guess a letter - u
Word is - hul-
Guess a letter - tg
Invalid guess - tg
Word is - hul-
Guess a letter - k
You guessed it! The word was hulk!
答案 2 :(得分:0)
我认为如果将单词和标记存储为字符数组,这将会更加容易:
char letter = 0; //declares and initailise letter
String [] words = { "gluttony", "lust", "greed", "pride", "despair", "wrath", "vainglory", "rhythm", "delicious", "better", "jacuzzi" , "ironman", "captainamerica", "thor", "hulk", "spiderman", "antman", "batman"}; //declares and initailise array of words to guess
String word = words[(int) (Math.random() * words.length)]; //chooses random word from the word array
char[] chosenWord = new char[word.length()];
char[] marks = new char[word.length];
for (int i=0;i< word.length(); i++) // for method for displaying the correct word as dashes
{
chosenWord[i] = word.charAt(i);
marks[i] = '-';
}
System.out.println("lets play hangman, your word is " + new String(marks) + "\n" + "enter a letter to guess the word");
Scanner input = new Scanner(System.in);
letter = input.next(".").charAt(0); //assign inputted letter to letter variable
if ((word).contains(""+letter)){ //if statement to excute if guessed letter is in word
for(int i =0; i < chosenWord.length; i++){
if(chosenWord[i] == letter){
marks[i] = chosenWord[i]
}
}
// i imagine this is where i put some sort of code to show that guessed letter?
System.out.println("You guessed a letter!" + new String(marks));
}
我没有语法检查这个,但你应该知道我在做什么。
答案 3 :(得分:0)
这就是我所做的。 我认为在这里使用数组要好得多。
public static void main(String[] args) {
String[] words = {"gluttony", "lust", "greed", "pride", "despair", "wrath", "vainglory", "rhythm", "delicious", "better", "jacuzzi", "ironman", "captainamerica", "thor", "hulk", "spiderman", "antman", "batman"}; //declares and initailise array of words to guess
String word[] = (words[(int) (Math.random() * words.length)]).split(""); //chooses random word from the word array and creates a array of letters
String[] marks = new String[word.length];
Arrays.fill(marks,"-"); // creates and fills an array with dashes
Scanner in = new Scanner(System.in);
String letter = "";
int counter = 0;
while(Arrays.toString(marks).contains("-")) {
counter++;
System.out.println("This is your word!: " + String.join("", marks));
System.out.print("Guess a letter ");
letter = String.valueOf(in.next(".").charAt(0));
for (int i = 0; i < word.length; i++) {
if(word[i].equals(letter)){
marks[i] = word[i];
}
}
}
System.out.println("Congratulations your word is " + String.join("",marks) + "You did it in " + counter + "trials");
}
}