In Java.
I have a character array that I need to look into and see if it
contains certain characters and print something,
and if it doesn't contain those characters I need to print
something.
Example:
char[] array = {w, ' ', K, Q, k, q, ' ', -, 0, ' ', 1};
if the array has 'K' print White
if the array has 'Q' print white
if the array has 'k' print Black
if the array has 'q' print black
if the array doesn't have K Q k or q, print none/
thank you in advance.
public class CharArrayCheck { public static void main(String[] args) { char[] array = {'w', ' ', 'K', 'Q', 'k', 'q', ' ', '-', '0', ' ', '1'}; int count = 0; for (int i = 0; i < array.length; i++) { if (array[i] == 'K') { System.out.println("White"); ++count; } else if (array[i] == 'Q') { System.out.println("white"); ++count; } else if (array[i] == 'k') { System.out.println("Black"); ++count; } else if (array[i] == 'q') { System.out.println("black"); ++count; } } if (count == 0) { System.out.println("none"); } } }
Get Answers For Free
Most questions answered within 1 hours.