Search code examples
javawhile-loopgoto

How do you activate a line of code that happened before?


I am new to Java coding and am practising by making a calculator. I want it so that when I type Restart it does the the operation selection part.

import java.util.Objects;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        boolean MO=true;

while(MO==true)
    MO=false;
        System.out.println("Please enter your operation");
        System.out.println("1 - Addition");
        System.out.println("2 - Subtraction");
        System.out.println("3 - Multiplication");
        System.out.println("4 - Division");
        Scanner scanner = new Scanner(System.in);
        int op = scanner.nextInt();
        System.out.println(op);
        scanner.reset();
        // Addition
        if(op == 1) {
            System.out.println("Enter a starting value!");
            int as =   scanner.nextInt();
            scanner.reset();

            boolean ab = true;
            while(ab) {
               ab=false;
            System.out.println("Please enter a number to add or type 'End' to end or 'Clear' to reset or 'Restart' to exit the mode");
            String an = scanner.next();
            if(Objects.equals(an, "End")) {
                System.out.println("Quitting"); }
            else if(Objects.equals(an,"Clear")) {
             int  ae = 0;
                System.out.println("Cache clear!");
ab = true;} else if(Objects.equals(an,"Restart")) {
                System.out.println("Restarting!");
                MO=true;
                break;
            }else {
 int ra = Integer.parseInt(an);


               int ae = as+ra;
                System.out.println(ae);
                as = ae;
            }}
        }}

Using a while(probably incorrectly) , expecting a success , but failure occurs ;(


Solution

  • You forgot the curly brackets for your while loop while(MO)! this means that it will only execute the first line after the statement which is while(MO==true) MO=false. Here is the fixed code.

    import java.util.Objects;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            boolean MO=true;
    
            while(MO==true) {
                MO=false;
            System.out.println("Please enter your operation");
            System.out.println("1 - Addition");
            System.out.println("2 - Subtraction");
            System.out.println("3 - Multiplication");
            System.out.println("4 - Division");
            Scanner scanner = new Scanner(System.in);
            int op = scanner.nextInt();
            System.out.println(op);
            scanner.reset();
            // Addition
            if(op == 1) {
                System.out.println("Enter a starting value!");
                int as =   scanner.nextInt();
                scanner.reset();
    
                boolean ab = true;
                while(ab) {
                    ab=false;
                    System.out.println("Please enter a number to add or type 'End' to end or 'Clear' to reset or 'Restart' to exit the mode");
                    String an = scanner.next();
                    if(Objects.equals(an, "End")) {
                        System.out.println("Quitting"); }
                    else if(Objects.equals(an,"Clear")) {
                        int  ae = 0;
                        System.out.println("Cache clear!");
                        ab = true;} else if(Objects.equals(an,"Restart")) {
                            System.out.println("Restarting!");
                            MO=true;
                            break;
                        }else {
                            int ra = Integer.parseInt(an);
    
    
                            int ae = as+ra;
                            System.out.println(ae);
                            as = ae;
                        }}
            }}
        }
    }