Ergebnis speichern!

XuniI

Mitglied
Hallo Leute,
ich habe mir letztens einen Taschenrechner programmiert. (Kein GUI)
Ich eine Funktion eingebaut, wo er abfragt, ob man weiter rechnen will. Nun möchte ich
eine Abfrage programmieren:

Er soll mich fragen, ob ich mit dem Ergebnis, das ich berechnet habe weiter rechnen will oder von neu starten.

Kann mir jemand vllt. ein snippet schicken, womit ich das am besten machen kann?

Hier der bisherige Code (Verbesserungen sind immer willkommen):

Java:
public static void main(String[] args) {

        calculator();
        reset();
    }

    public static double calculator() {

        Scanner sc = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
        double result=0;
        String falseVariable;
        double v1;
        System.out.println("Please log in your first number: ");
        while(!sc.hasNextDouble()){
            falseVariable = sc.nextLine();
            System.out.println(falseVariable + " is not a number!");
        }
        v1 = sc.nextDouble();

        System.out.println("Now choose your math operator: | '+' for addition | '-' for subtraction | '*' for multiplication |  '/' for division | '^' for power |");
        String login = sc2.nextLine();

        if (!login.equals("+") && !login.equals("-") && !login.equals("*") && !login.equals("/") && !login.equals("^")) {
            System.out.println("This operator is unavailable!");
            while (!login.equals("+") && !login.equals("-") && !login.equals("*") && !login.equals("/") && !login.equals("^")) {
                System.out.println("Choose : | '+' for addition | '-' for subtraction | '*' for multiplication |  '/' for division | '^' for power |");
                login = sc2.nextLine();
            }
        }

        System.out.println("Please log in your second number: ");
        while(!sc.hasNextDouble()){
            falseVariable = sc.nextLine();
            System.out.printf(falseVariable+" is not a number!");
        }
        double v2 = sc.nextDouble();

        char choice = login.toLowerCase().charAt(0);
        switch (choice) {
            case '+':
                result = v1 + v2;
                break;
            case '-':
                result = v1 - v2;
                break;
            case '*':
                result = v1 * v2;
                break;
            case '/':
                if (v2 == 0) {
                    System.out.println("It is not possible to divide by 0!");
                    return result;
                }
                result = v1 / v2;
                break;
            case '^':
                result = (long) Math.pow(v1, v2);
                if (result >= 9223372036854775807L) {
                    System.out.println("Your result reached the maximum of all numbers -> 9223372036854775807");

                    return result;
                }
                break;

        }
        try {
            Thread.sleep(250);
        } catch (Exception e) {

        }
        System.out.println("Your result is "+result);

        return result;
    }


   public static void reset(){
        Scanner sc3 = new Scanner(System.in);
        String s;
        boolean reset = true;
        do {
            System.out.println("Do you want to use another operation? Y/N");
            s = sc3.nextLine();
            if (s.equalsIgnoreCase("y")) {
                try{
                    Thread.sleep(1000);
                }catch (Exception e){}
                calculator();
            } else if (s.equalsIgnoreCase("n")) {
                System.exit(0);
            }

        } while (reset);
    }
 
mach ne neue Methode new() ind der alles drin steht von Z.9 - Z.19

zieh die zeilen aus dem calculator() raus.
wenn du das programm startest,dann rufst du nicht mehr nur calculator() auf, sondern new() und calculator()
dann noch eine fallunterscheidung in deine schlussabfrage (neue operation?) rein und dann sollte das funktionieren

Gruß eMmiE
 

Zurück
Oben