So I have this part of a program right now where the user can type in a character and it will say at which place that character is:
Scanner scanny = new Scanner(System.in);
char[] arrayOfChars = {'i', 'r', 'i', 'd', 'o', 'c', 'y', 'c', 'l', 'i', 't', 'i', 's'};
char input = scanny.nextLine().charAt(0);
for (int i = 0; i < arrayOfChars.length; i++) {
int place = i + 1;
if (arrayOfChars[i] == input) {
System.out.println("This character " + input + " can be seen at this place: " + place + "...!");
I want to expand is so it can also show how many times that certain character is in the word iridocyclitis. So if the user types an i it will say: the character is seen 4 times. I thought of making an if statement, but since I already have one I can't think of another way to do it.Is there another way I can write it without using an if-statement?
Aucun commentaire:
Enregistrer un commentaire