简体   繁体   中英

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. 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? 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

The correct count of answers is: 2
The wrong count of answers is: 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM