为什么我的字符串距离始终为5?

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

Why is my character string distance always 5?

问题

import java.util.Random;
import java.util.Scanner;

public class StringDiff {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        System.out.println("请输入5个大写字母作为用户字符串。");
        String userString = scan.nextLine();

        int leftLimit = 48; // 数字 '0'
        int rightLimit = 122; // 字母 'z'
        int stringLength = 5;
        Random random = new Random();

        String randString = random.ints(leftLimit, rightLimit + 1)
                .filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
                .limit(stringLength)
                .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
                .toString();
        {
            int i = 0, stringDist = 0;
            while (i < userString.length()) {
                if (userString.charAt(i) != randString.charAt(i))
                    stringDist++;
                i++;
            }
            System.out.println("用户字符串为:" + userString);
            System.out.println("随机字符串为:" + randString);
            System.out.println("用户字符串与随机字符串之间的距离为:" + stringDist);
        }
    }
}
英文:

I have a project that has three requirements:

  1. Prompt user to enter 5 uppercase characters and save as
    variable.

  2. Randomly generate a 5 character string and save as variable.

  3. Compute the distance between the two strings.

    I thought I had it and it looked like it worked, but then I noticed that the computed distance between the two strings is always 5.
    I am guessing that my stringLength is somehow being used for stringDist.

    If you could look it over and lead me in the right direction as to what I'm missing, I would greatly appreciate it.

import java.util.Random;
import java.util.Scanner;

public class StringDiff {

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println (&quot;Please enter 5 Uppercase letters.&quot;);
		String userString = scan.nextLine();
		
		int leftLimit = 48; // numeral &#39;0&#39;
	    int rightLimit = 122; // letter &#39;z&#39;
	    int stringLength = 5;
	    Random random = new Random();
	 
	    String randString = random.ints(leftLimit, rightLimit + 1)
	      .filter(i -&gt; (i &lt;= 57 || i &gt;= 65) &amp;&amp; (i &lt;= 90 || i &gt;= 97))
	      .limit(stringLength)
	      .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
	      .toString();
	    { 
	        int i = 0, stringDist = 0; 
	        while (i &lt; userString.length()) 
	        { 
	            if (userString.charAt(i) != randString.charAt(i)) 
	                stringDist++; 
	            	i++; 
	        } 
	         System.out.println (&quot;The user string is: &quot; + userString);
	         System.out.println (&quot;The random string is: &quot; + randString);
	         System.out.println (&quot;The distance between &quot; + userString + &quot; and &quot; + randString + &quot; is &quot; + stringDist);
} 

答案1

得分: 0

因为您正在将一个由用户输入的五个字符的字符串与一个随机生成的字符串进行比较,所以相应字符相同的位置很可能不存在,这将使得距离等于五。从这个意义上说,您的代码没有问题。如果您反复运行该程序,最终会得到一个小于五的距离。

英文:

Since you're comparing a five character user-entered string with a randomly generated string, there's a good chance that there no positions where the corresponding characters are the same, which would make the distance equal to five. So in that sense, there's nothing wrong with your code. If you repeatedly run the program, you'll eventually get a distance less than five.

huangapple
  • 本文由 发表于 2020年9月27日 12:33:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64084825.html
匿名

发表评论

匿名网友

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

确定