英文:
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:
-
Prompt user to enter 5 uppercase characters and save as
variable. -
Randomly generate a 5 character string and save as variable.
-
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 mystringLength
is somehow being used forstringDist
.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 ("Please enter 5 Uppercase letters.");
String userString = scan.nextLine();
int leftLimit = 48; // numeral '0'
int rightLimit = 122; // letter '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 ("The user string is: " + userString);
System.out.println ("The random string is: " + randString);
System.out.println ("The distance between " + userString + " and " + randString + " is " + 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论