gpt4 book ai didi

java - 比较两个不同字符串中的 Char 时遇到问题

转载 作者:行者123 更新时间:2023-12-01 19:05:36 24 4
gpt4 key购买 nike

该程序应该是一个包含 10 个不同单词的词汇表测试。我的代码正在工作,除了一件事,那就是一个函数,允许用户获得一个输出,说“几乎正确,正确的答案是......”如果某些字符是正确的。

例如;正确答案是房子。

user writes: housq -> output = "Almost correct, the correct answer is..."

user writes hsasa -> output = "Wrong, the correct answer is..."

我的计划是通过 int count 将“几乎正确”置于值 1-3 之间。

但是通过这个循环:例如,它只计算拼写正确的单词

Correct word is house.

input -> house -> count=5

input -> housa -> count=0.

我是 Java 新手,对于可能出现的错误代码,我向大家表示歉意。

     Scanner scanner = new Scanner(System.in);
String inputString = "";
String[] eng = {"car", "house", "run", "blue", "bake", "jump", "swim", "moon", "road", "kind"};
String[] swe = {"bil", "hus", "springa", "blå", "baka", "hoppa", "simma", "måne", "väg", "snäll"};
int result = 0;
int word = 0;
int count = 0;
int low = 2;
int high = 3;

for (int i = 0; i < swe.length; i++) {
System.out.println(swe[i]);
inputString = scanner.nextLine().trim();

if (inputString.equalsIgnoreCase(eng[i])) {
word++;
result++;
System.out.println("Korrekt! " + result + " rätt av " + word + " ord.");

for (int j = 0; j < inputString.length(); j++) {
if (inputString.charAt(j) == eng[i].charAt(j)) {
++count;

}
}
if(count >= low){
System.out.println("Nästan rätt, korrekt svar är " + eng[i]);
word++;
}


if(count > low && count < high){
System.out.println("Nästan rätt, korrekt svar är " + eng[i]);
word++;
}
if (count == 0) {
System.out.println("fel, rätt svar är " + eng[i]);
word++;
}
System.out.println(count);

if (inputString.equalsIgnoreCase("Q")) {
System.out.println("Du svarade på totalt " + word + " glosor och hade " + result + " rätt. Välkommen åter!");

System.exit(0);

}

count = 0;
}
}

}}

最佳答案

我尝试了这样的代码片段,并且得到了正确的输出。这是我尝试过的

String inputString = "housr";
String eng = "house";
int count = 0;
for(int j=0; j < inputString.length(); j++) {
if(inputString.charAt(j) == eng.charAt(j)){
++count;
}}
System.out.println(count);

我的输出为4

你在尝试什么,请粘贴你的逻辑和数据

关于java - 比较两个不同字符串中的 Char 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59562505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com