[英]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?
So I've tried adding count ++ in multiple places in my code along with researching some way to allow multiple inputs to no avail.因此,我尝试在我的代码中的多个位置添加 count++ 以及研究某种允许多个输入无济于事的方法。 Am I missing something on placement or would I need to rewrite the code entirely for what I am wanting to accomplish?我是否遗漏了一些关于放置的东西,或者我是否需要为我想要完成的事情完全重写代码? Is this not a boolean match situation?这不是boolean的匹配情况吗? Very lost and sorry if this is a noob question.如果这是一个菜鸟问题,我会非常迷茫和抱歉。 Appreciate the input.感谢输入。
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");
}
}
Problem and what I've tried is above.问题和我试过的都在上面。 Also, apologies on formatting if it's messed up.另外,如果格式搞砸了,请道歉。 First time using stack overflow.第一次使用堆栈溢出。
Please see the below code and let me know if this solves your issue.请查看下面的代码,如果这能解决您的问题,请告诉我。
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);
}
}
Input输入
India
Delhi
Australia
Kabul
Bangladesh
Output Output
The correct count of answers is: 2
The wrong count of answers is: 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.