admin管理员组

文章数量:1398093

I'm making a console calculator in java, but I can't make it so that I can enter an infinite number of numbers and strings alternately, how can this be implemented?

import java.util.InputMismatchException;
import java.util.Scanner;

public class Calculate {
    private static boolean stopper = true;
    private static double result;

    public static void calculate(Scanner scanner) {
        while (stopper) {
            try {
                double a = scanner.nextDouble();
                scanner.nextLine();
                String symbol = scanner.nextLine();
                scanner.nextLine();
                double b = scanner.nextDouble();
                // подумай как убрать white-space символы
                if (symbol.equals("=")) {
                    stopper = false;
                    System.out.println(result);
                } else if (symbol.equals("+")) {
                    result = a + b;
                } else if (symbol.equals("-")) {
                    result = a - b;
                }
            } catch (InputMismatchException e) {
                System.out.println("Неправильный ввод данных");
                break;
            }
        }
    }
}

I tried to do this, but nothing came out, I also tried other ways, but nothing works either. Help please

本文标签: doublehow to provide infinite input of numbers and strings alternately in javaStack Overflow