vendredi 8 juillet 2016

BufferedReader doesn't wait till input is done

I have a problem with my BufferedReader which suppose to read all the time till I tell do not do it. But it is not. Why in program is shutting down and doesn't wait till I will type "true or false"?

 public static void main(String args[]) throws IOException{


            double a;
            double b;
            char ch=' ';
            try(BufferedReader br=new BufferedReader(new InputStreamReader((System.in))))
            {while (true)

            {
                System.out.println("Введите a: ");
                a = Double.parseDouble(br.readLine());
                System.out.println("Введите b: ");
                b = Double.parseDouble(br.readLine());

                System.out.println("Введите символ арифметической операции, которую хотите произвести с выражением a{}b: * / + -.");
                ch = (char) br.read();
                switch (ch) {
                    case '+':
                        System.out.println(a + b);
                        break;
                    case '-':
                        System.out.println(a - b);
                        break;
                    case '*':
                        System.out.println(a * b);
                        break;
                    case '/':
                        System.out.println((b != 0) ? (a / b) : "Нельзя делить на 0!");
                        break;
                    default:
                        System.out.println("Вы ввели не поддерживаемые символы!");
                        break;
                }
                System.out.println("Хотите повторить ввод? (true, false)");

                String str = br.readLine();
                System.out.println("str " + str);
                if (!Boolean.parseBoolean(str)) {
                    break;
                }
            }


            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }

Aucun commentaire:

Enregistrer un commentaire