提问人:Natebd 提问时间:12/31/2022 最后编辑:Jim GarrisonNatebd 更新时间:12/31/2022 访问量:45
所以我试图看看是否有办法计算正确的输入并允许多个输入与布尔匹配。我能做到吗?
So I'm trying to see if there is a way to count correct inputs and allow multiple inputs with boolean match. Am I able to do this?
问:
因此,我尝试在代码中的多个位置添加 count ++,并研究某种方法来允许多个输入无济于事。我是否在放置时遗漏了某些内容,或者我是否需要完全重写代码以完成我想要完成的任务?这不是布尔匹配的情况吗?如果这是一个菜鸟问题,非常迷茫和抱歉。感谢您的投入。
Scanner scanner = new Scanner(System.in);
System.out.println("Insert any State Capital");
String currentInput = scanner.nextLine();
boolean match = false;
String [] capitals = stateCapitals[1];
for (String capital:capitals) {
if (capital.equalsIgnoreCase(currentInput)) {
match = true;
break;
}
}
if (match) {
System.out.println ("Correct");
}
else
System.out.println ("incorrect");
}
}
问题和我尝试过的在上面。另外,如果搞砸了,对格式表示歉意。首次使用堆栈溢出。
答:
0赞
isilia
12/31/2022
#1
请参阅下面的代码,如果这解决了您的问题,请告诉我。
import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
ArrayList<String> inputList = new ArrayList<>();
ArrayList<String> capitalList = new ArrayList<>(List.of("Delhi","Kabul","Dhaka"));
for (int i = 0; i < 5; i++)
{
String currentInput = scanner.nextLine();
inputList.add(currentInput);
}
int correct_response_count = 0;
int wrong_response_count = 0;
for (int i = 0; i < inputList.size(); i++)
{
if (capitalList.contains(inputList.get(i)))
{
correct_response_count++;
}
else
{
wrong_response_count++;
}
}
System.out.println("The correct count of answers is: " + correct_response_count);
System.out.println("The wrong count of answers is: " + wrong_response_count);
}
}
输入
India
Delhi
Australia
Kabul
Bangladesh
输出
The correct count of answers is: 2
The wrong count of answers is: 3
评论
Correct
incorrect
String [] capitals = stateCapitals[1];
capitals
String