A
A
Alexander Kostrikin2018-08-15 20:39:06
Java
Alexander Kostrikin, 2018-08-15 20:39:06

Why is the program not working properly?

I wrote a simple program that should ask for 1 character until a period is entered. If a period is entered, the number of times Enter was pressed (i.e., the number of attempts to guess the character) is displayed. If the user enters "q", then the program exits.

class Point {
    public static void main(String args[])
        throws java.io.IOException {
        char choice;
        int count;

        count = 0;

        do {
            System.out.print("Введите любой символ: ");
            choice = (char) System.in.read();
            System.out.println();
            count++;
        } while (choice != '.' | choice != 'q');

        switch(choice) {
            case '.':
                System.out.println("Вы нажали Enter " + count + " раз.");
                break;
            case 'q':
                break;
        }
    }
}

Can you please explain why it doesn't work the way you want it to?
C:\Users\Alex\Desktop\Projects\Point>java Point
Введите любой символ: .

Введите любой символ:
Введите любой символ:
Введите любой символ: q

Введите любой символ:
Введите любой символ:
Введите любой символ:

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EVGENY T., 2018-08-16
@AlexKost5

It seems to me that the error is here. Try PS And for the future, use the Scanner class - it was specially invented for such cases.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question