- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用我的第一种编程语言 Java,但我无法正确猜测以正确打印。当输入错误的字母时,它会打印“错误!”但是当猜到正确的字母时,它会打印“正确!”和“错误!”...任何帮助表示赞赏。谢谢。 :)
这些单词是从数组中提取的。
import java.util.Scanner;
import java.util.Random;
public static void main(String[] args) {
System.out.println("Welcome to Hangman! Below you can find the rules of the game.");
//Welcome message
System.out.println();
//Spacing
System.out.println("How to play:");
System.out.println();
//Spacing
System.out.println("Hangman is a word guessing game. Guess the word by suggesting letters within a certain number of guesses.");
System.out.println("The word to guess is represented by a row of dashes, representing each letter of the word.");
System.out.println("If you guess a letter correctly, it replaces a dash and you can continue to guess until the word is uncovered.");
System.out.println("However, if you guess wrong you lose a try. You have a maximum of 5 tries. After guessing 10 words, you win! Good luck!");
//Rules of game
System.out.println();
//Spacing
Scanner in = new Scanner (System.in);
//Scanner created
String[] intro = {"after", "think", "could", "thank", "round", "clump", "plane", "beach", "towel", "tiger", "goat", "monkey", "house"};
String[] beginner = {"around", "amount", "grown", "focus", "cedar", "flute", "unicorn", "fruit", "basket", "burger", "chips", "juice"};
String[] intermediate = {"about", "bring", "clean", "mother", "locker", "hunter", "drink", "eight", "spray", "untie", "cents"};
String name;
int age;
//Variables initialized
System.out.println("Please enter your name:");
//Prompt for name
name = in.next();
//Input for name
System.out.println("Please enter your age:");
//Prompt for age
age = in.nextInt();
//Input for age
if (age >= 4 && age < 7){
System.out.println("You have been placed in the Intro level!");
Random rand = new Random();
//Random created
int randomWord = rand.nextInt(12)+0;
//Stores the position of array
String word2guess = intro[randomWord];
//String is the value stored in the generated position of array
char[] word2Char = word2guess.toCharArray();
//Stores the word in a char array
char[] wordLength = new char [word2Char.length];
//Creates an array for word length
for (int i = 0; i < word2Char.length; i++){
wordLength[i] = '_';
}
//Replaces each letter of the word with an underscore
for (int i = 0; i < word2Char.length; i++){
System.out.print(wordLength[i] + " ");
}
//Prints the word in underscore format with a space between each letter
int wrongGuesses = 0;
int correctGuesses = 0;
boolean letterFound = false;
boolean nextWord = true;
while (nextWord){
while(correctGuesses != word2Char.length){
System.out.println();
//Spacing
System.out.println("Please enter a letter:");
char guess = in.next(".").charAt(0);
//Input for only ONE letter
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
break;
}else{
letterFound = false;
}
}
if(letterFound == false && wrongGuesses <= 4){
wrongGuesses++;
System.out.println("Wrong! You have "+ (5 - wrongGuesses)+" attempts remaining. Please try again.");
}else{
correctGuesses++;
System.out.println("Correct!");
}
for (int i = 0; i < word2Char.length; i++){
System.out.print(wordLength[i] + " ");
}
System.out.println();
//Spacing
if (correctGuesses == word2Char.length){
System.out.println("Congratulations! You guessed the word. The word was: "+word2guess);
nextWord = true;
}
if (wrongGuesses == 5){
System.out.println();
//Spacing
System.out.println("Game Over! You have no attempts remaining. The word was: "+word2guess);
nextWord = false;
break;
}
}
}
最佳答案
这一段代码:
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
correctGuesses++;
System.out.println("Correct!");
}
}
if(!letterFound){
wrongGuesses++;
System.out.println("Wrong!");
}
需要是这样的:
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
break;
}else{
letterFound = false;
}
}
if(letterFound == false){
wrongGuesses++;
System.out.println("Wrong!");
}else{
correctGuesses++;
System.out.println("Correct!");
}
问题是在 for 循环中,即使 letterFound
为 true,循环也会继续检查每个字符。只有当有人正确猜出最后一个字符时它才会起作用。因此,我所做的是添加一个 break
语句,以便每当 letterFound
为 true 时,for 循环就会退出。
关于java - Hangman Java 游戏打印错误和正确的猜测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41212928/
我的问题在于处理大型 CSV 文件中的数据。 我正在寻找基于在该列中找到的值来确定(即猜测)该列的数据类型的最有效方法。我可能正在处理非常困惑的数据。因此,该算法应该具有一定的容错性。 这是一个例子:
我正在开发一个基于 map 的网络应用程序,我想为用户提供“使用当前位置”的功能 我可以轻松检查地理位置 api 的可用性,如下所示: if (navigator && navigator.geolo
我有一大组日期时间字符串,可以放心地假设它们的格式都相同。例如,我可能有一组日期“7/1/13 0:45”、“5/2/13 6:21”、“7/15/13 1:24”、“7/9/13 12” :41",
我正在尝试对是否可以接收 C2DM 消息进行最佳猜测。 我创建了一个应用程序,它依赖于在物理上无法访问时将信息推送到手机。我知道 C2DM 不能保证传递,但我至少想知道何时可以传递消息;如果不是,我们
我正在编写一个系统,以便用户可以编辑他发布的内容。简化它是一个存储在数据库中的文本区域/输入字段和一个检索它的页面。问题是,我认为编码不正确,因为字符串存储在数据库中,如“É”或其他东西(phpmya
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
只是好奇,但匹配 Guid 的概率是多少? 从 SQL 服务器说一个 Guid:5AC7E650-CFC3-4534-803C-E7E5BBE29B3D 它是阶乘吗?:(36 * 32)! = (11
这个问题已经有答案了: Guessing algorithm does not seem to work, guessing number by Python (3 个回答) 已关闭 5 年前。 程序
我正在创建一个非常简单的计算器,但我需要它在每次击键时进行更新。我似乎找不到该特定类别中的任何内容。有人能指出我正确的方向吗? 我正在寻找类似 A*1.325 + B*3.76 的内容,其中 B 是下
环顾四周this似乎是最接近我的问题的答案。然而它会导致其他问题...... 这是我的情况: $element.insertBefore($container); 此行有一个警告,指出 insertB
当我收到此页面时 http://booking.airasia.com/css/AKBase/Cultures/en-GB/far-min.css与 Node 的 http , toString方法给
我在代码中发现了这一点,但不知道哪个实例接收到。 var guess = require ('myModule1') ('myMmodule2') 最佳答案 看来 myModule1 导出了一个函数,
WARNING: No name was provided for external module 'moment' in output.globals – guessing 'momentImpor
我是一名优秀的程序员,十分优秀!