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

时间:2018-05-01 17:18:09

标签: 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;
                }

            }
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

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

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%

答案 1 :(得分:0)

  private static double getCorrectness(String actualPhrase, String userGuess) {
    char [] actual = actualPhrase.toCharArray();
    char [] guess = userGuess.toCharArray();
    int length = Math.min(actual.length, guess.length);
    for(int counter = 0;counter<length ;counter++) {
        //case sensitive
        if(actual[counter]!=guess[counter]) {
            return counter*100.0/actual.length;
        }
    }
    return length*100.0/actual.length;
}