简体   繁体   English

所以我想看看是否有一种方法可以计算正确的输入并允许多个输入与 boolean 匹配。 我能做到吗?

[英]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.

相关问题 所以,我正在尝试在 android 中做一些简单的事情,它显示了我的错误,我无法弄清楚 - So, I am trying to do simple stuff in android and it shows me error and I'm not able to figure it out 我正在尝试从json主体获取输入,并希望验证所有输入是否正确(如果不是通过异常) - I am trying to take input from a json body and want to validate if all the inputs are correct if not through exception 我想在 Java 中循环,直到用户输入正确的值,但我无法做到 - I want to loop while in Java until user inputs the Right value but I am not able to do 我正在尝试使用jconsole连接到具有身份验证的JMX,但我无法这样做。 但是我无需身份验证就可以连接它 - I am trying to connect to JMX with authentication using jconsole, but I am not able to do so. But I am able to connect it without authentication 正确的实例化类的方法,这样我就可以验证证书中的路径链 - Correct way to instantiate class so I'm able to validate path chains in certificate 如何计算此Java代码中用户输入的数量? - How do I Count the number of user inputs in this java code? 我正在尝试从另一个框架打开一个JPanel。 无法这样做 - I am trying open a JPanel from another frame. Not able to do so 我正在尝试使用resteasy服务在jboss中运行war文件,但无法这样做 - I am trying to run a war file in jboss using resteasy services but not able to do so 我无法理解Scanner,或者应该说控制台的输入 - I am not able to understand Scanner or I should say the inputs from console 我无法将程序中找到的 boolean 结果的正确值分配给 boolean 变量并使用结果检查条件 - I am Not able to assign the correct value of boolean result found in the program , to a boolean variable and to check a condition with the result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM