在Java字符串中猜测符号

huangapple go评论68阅读模式
英文:

Guessing symbols in a Sting word java

问题

Got a String like:

String str = "###############";

Got guess word, for example:

String guess = "Java";

User must guess word:

User input:

Sava

String should be:

String str = "#a#a###########";

all right symbols placed on their indexes

String is immutable class.

I chose Stringbuilder

 for (int i = 0; i < length ; i++) {
    if (rnd.charAt(i) == guess.charAt(i) && rnd.charAt(i) != '#'){
       sb.append(rnd.charAt(i));
        }
   }

 System.out.println(sb);

 sb.delete(0, sb.length());

Stringbuilder add right symbols not on possition i, but on the last indexes.

Example:

guess word: Java

user input Sala:

System.out.println(sb);

###############aa

How I can achieve needed result?
And what tools should I use?

needed result:

Example:
guess word Java:

user input Sala:


System.out.println(sb);
#a#a###########
英文:

Got a String like:

String str = &quot;###############&quot;;

Got guess word, for example:

String guess = &quot;Java&quot;

User must guess word:

User input:

Sava

Sring should be:

String str = &quot;#a#a###########&quot;;

all right symbols placed on their indexes

String is immutable class.

I chose Stringbuilder

 for (int i = 0; i &lt; length ; i++) {
    if (rnd.charAt(i) == guess.charAt(i) &amp;&amp; rnd.charAt(i) != &#39;#&#39;){
       sb.append(rnd.charAt(i));
        }
   }

 System.out.println(sb);

 sb.delete(0, sb.length());

Stringbuilder add right symbols not on possition &#39;i&#39;, but on the last indexes.

Example:

guess word: Java

user input Sala:

System.out.println(sb);

###############aa

How I can achieve needed result?
And what tools should I use?

needed result:

Example:
guess word Java:

user input Sala:


System.out.println(sb);
#a#a###########

答案1

得分: 1

public class Main {
    public static void main(String[] args) {
        String str = "#a#a###########";
        String guess = "Java";
        String input = "Sala";
        StringBuilder sb = new StringBuilder();
        int i;
        for (i = 0; i < str.length() && i < guess.length() && i < input.length(); i++) {
            // In case of a match, append the matched character
            if (guess.charAt(i) == input.charAt(i)) {
                sb.append(guess.charAt(i));
            } else {// Else append the placeholder symbol from `str`
                sb.append(str.charAt(i));
            }
        }

        // Append the remaining placeholder characters from `str`
        sb.append(str.substring(i));

        // Display
        System.out.println(sb);
    }
}
**Output:**

    #a#a###########
英文:

You can do it as follows:

public class Main {
	public static void main(String[] args) {
		String str = &quot;#a#a###########&quot;;
		String guess = &quot;Java&quot;;
		String input = &quot;Sala&quot;;
		StringBuilder sb = new StringBuilder();
		int i;
		for (i = 0; i &lt; str.length() &amp;&amp; i &lt; guess.length() &amp;&amp; i &lt; input.length(); i++) {
			// In case of a match, append the matched character
			if (guess.charAt(i) == input.charAt(i)) {
				sb.append(guess.charAt(i));
			} else {// Else append the placeholder symbol from `str`
				sb.append(str.charAt(i));
			}
		}

		// Append the remaining placeholder characters from `str`
		sb.append(str.substring(i));

		// Display
		System.out.println(sb);
	}
}

Output:

#a#a###########

答案2

得分: 1

private static String word(){
    String guess = new Scanner(System.in).nextLine();
    return guess;
}

private static void guessWord(String[] arr) {
    int random = new Random().nextInt(arr.length);
    String rnd = arr[random];

    int length = 15;

    StringBuilder sb = new StringBuilder();
    String guess = "";

    int rndLength = length - rnd.length();
    int guessLength = length - guess.length();


    do {
        System.out.println("Enter a word: ");
        guess = word();

        if (sb.length() < length){
            for (int i = 0; i < length ; i++) {
                sb.append("#");
            }
        }

        for (int i = 0; i < length  && i < rnd.length() && i < guess.length();  i++) {
            if (rnd.charAt(i) == guess.charAt(i)){
                sb.setCharAt(i, rnd.charAt(i));
                sb.delete(length, sb.length());
            }
        }

        if (rnd.equals(guess)){
            System.out.println("Guess word: " + rnd);
            break;
        }else if (!rnd.equals(guess)) {
            System.out.println(sb);
        }
    } while (!rnd.equals(guess));
}
英文:

Work like this:

private static String word(){
String guess = new Scanner(System.in).nextLine();
return guess;
}
private static void guessWord(String[]arr) {
int random = new Random().nextInt(arr.length);
String rnd = arr[random];
int length = 15;
StringBuilder sb = new StringBuilder();
String guess = &quot;&quot;;
int rndLength = length - rnd.length();
int guessLength = length - guess.length();
do {
System.out.println(&quot;Enter a word: &quot;);
guess = word();
if (sb.length() &lt; length){
for (int i = 0; i &lt; length ; i++) {
sb.append(&quot;#&quot;);
}
}
for (int i = 0; i &lt; length  &amp;&amp; i &lt; rnd.length() &amp;&amp; i &lt; guess.length();  i++) {
if (rnd.charAt(i) == guess.charAt(i)){
sb.setCharAt(i, rnd.charAt(i));
sb.delete(length, sb.length());
}
}
if (rnd.equals(guess)){
System.out.println(&quot;Guess word: &quot; + rnd);
break;
}else if (!rnd.equals(guess)) {
System.out.println(sb);
}
}while (!rnd.equals(guess));
}

huangapple
  • 本文由 发表于 2020年9月7日 16:25:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63773928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定