java - 比较字符串中各个字母的循环

标签 java string loops

我正在尝试比较用户的字母和短语。我希望它能沿着每个字母走下去。我有“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;
                }

            }
        }
    }
}
}

最佳答案

有很多方法可以做到这一点。这是使用流而不是显式循环的一种方法:

String answer = "Somewhere over the rainbow";
String guess = "Somewhere over the xxxxxxx";
long correct = IntStream.range(0, Math.min(answer.length(), guess.length()))
        .filter(i -> answer.charAt(i) == guess.charAt(i)).count();
int total = Math.max(answer.length(), guess.length());
System.out.format("(%d/%d) = %d%%%n", correct, total, correct*100/total);

输出:

(19/26) = 73%

关于java - 比较字符串中各个字母的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50120864/

相关文章:

java - REST 如何使用 SOAP 作为协议(protocol)?

C++ 字符串实例化与 strncpy/memcpy

c - for循环不会停止

java - 使用 Java、Slick2D 和平铺 map 编辑器进行碰撞检测

java - Sql事件与Servlet中的ScheduledExecutorService

java - Collections.shuffle(list) 不会打乱我的列表

java - 用java获取文件的第一行

c# - 将具有零字符元素的字符数组转换为 C# 字符串

java - 返回错误的数组索引

JavaScript:将变量与数组数组中的元素匹配