【发布时间】:2018-05-01 17:18:08
【问题描述】:
我正在尝试比较用户的字母和短语。我希望它走下每一个字母。我有“if ((userGuessLength / phraseLength * 100.0) > 75.0)”作为占位符来比较字符串长度,但需要它来比较字符串中的每个字母。
我将如何修改它来做到这一点?
示例:
彩虹上方的某个地方。 - 实际短语
在 xxxxxxx 的某个地方。 - 用户猜测
19 个字符是正确的(包括空格) 26 个字符是实际的短语长度
(19/26) * 100 = 73 百分比(他们错过了)
这是我的整个程序:
package classprojects;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.Random;
public class WordGame {
public static void main(String[] args) {
String userInput = " "; // userInput
Random rand = new Random();
Scanner word = new Scanner(System.in);
int n = rand.nextInt(14);
String underscores = ""; // holder for changing characters to *
String update = ""; // this is updated from underscores and displayed
String[] phrases = new String[15];
// Phrases used in Array
phrases[0] = "dog eat dog world";
phrases[1] = "a penny for your thoughts";
phrases[2] = "at the drop of a hat";
phrases[3] = "ball is in your court ";
phrases[4] = "back to the drawing board ";
phrases[5] = "barking up the wrong tree";
phrases[6] = "beat around the bush";
phrases[7] = "best of both worlds";
phrases[8] = "bite off more than you can chew";
phrases[9] = "blessing in disguise";
phrases[10] = "cant judge a book by its cover";
phrases[11] = "costs an arm and a leg";
phrases[12] = "curiosity killed the cat";
phrases[13] = "dont put all your eggs in one basket";
phrases[14] = "elvis has left the building";
double phraseLength = phrases[n].length();
//loop for changing characters in array to * and replacing correct letter from the userInput
while (true) {
for (int l = 0; l < 5; l++) {
for (int i = 0; i < phraseLength; i++) {
char theChar = phrases[n].charAt(i);
if (theChar == ' ') {
underscores += ' ';
update = underscores;
} else
for (int j = 0; j < userInput.length(); j++) {
char compare = userInput.charAt(j);
if (theChar == compare) {
underscores += compare;
update = underscores;
break;
} else if (j == userInput.length() - 1) {
underscores += "*";
update = underscores;
}
}
}
//prints the updated phrase with * and letters that were correctly guessed, and prompts user to continue to enter letters
System.out.println(phrases[n]);
System.out.println("Start by Entering a Letter to Guess the Phrase: " + update);
System.out.println("Enter A Letter: ");
//takes the keyboard input and makes it all lower case and
//restarts underscores so phrase does not stack
String letterGuess = word.next();
String letterGuessL = letterGuess.toLowerCase();
userInput += letterGuessL;
underscores = "";
}
//Display after 5 turns
int userReply = JOptionPane.showConfirmDialog(null, "Do you know the phrase? If not continue guessing.",
"Guess", JOptionPane.YES_NO_OPTION);
//if yes then it takes the length of your guess and divides it by the phrase and check if its over 75%
if (userReply == 0) {
String userGuess = JOptionPane.showInputDialog("What is your guess?");
double userGuessLength = userGuess.length();
if ((userGuessLength / phraseLength * 100.0) > 75.0) {
JOptionPane.showMessageDialog(null, "Correct, you WIN! That was the correct phrase: " + phrases[n]);
break;
} else {
int userReply2 = JOptionPane.showConfirmDialog(null,
"Sorry that is wrong, Do you want to continue going?", "Wrong", JOptionPane.YES_NO_OPTION);
if (userReply2 == 0) {
} else {
JOptionPane.showMessageDialog(null, "The game is OVER. The correct phrase was: " + phrases[n]);
break;
}
}
}
}
}
}
【问题讨论】:
-
向我们展示您尝试解决此问题的方法。它有效吗?如果没有,什么不起作用?
-
为什么不把正确的字符串放入
char[],然后对用户输入做同样的事情。然后,您可以使用索引进行直接比较,并且可以为每个正确的字母增加一个计数器。 -
我尝试在我的代码中使用类似的 for 循环,它用 * 替换每个字母,而不是只替换 count
-
你有我可以看到 Ascalonian 的示例或基本代码吗?
-
其实你不需要char[]。在 for 循环
if (correctString.charAt(i) == inputString.charAt(i)) { correctCounter++;}或其他东西中怎么样 - 当然,请记住字符串长度