简体   繁体   中英

Storing value into another class after a switch statement and reuse the variable in another switch statement

So basically in the case 1 of main function, I am trying to store the two values the user input into another class. Then if I go to case 2 immediately, the output will be the sum of the two values that were input earlier. My question is how to change my code such that case 2 and 3 are able to use the values that I have stored in case 1 earlier? Thank you.

Code for main function:

import java.util.Scanner;
public class calculatorfinal
{
  public static void main(String args[])
    {
     int number1,number2,choice,sum,product;
     while(true)
             {
              Scanner scan = new Scanner(System.in);
              operations myoperations=new operations();
              System.out.println("\n1. Get numbers");
              System.out.println("\n2. Addition");
              System.out.println("\n3. Multiplication");
              System.out.println("\n4. Exit");
              choice = scan.nextInt();

              switch(choice)
                    { 
                      case 1:
                            System.out.println("enter the two numbers:");
                            number1=scan.nextInt();
                            number2=scan.nextInt();
                            myoperations.getnumbers(number1,number2);
                            break;
                      case 2:
                            myoperations.addnumbers();
                            break;
                      case 3:
                           myoperations.multiplynumbers();
                           break;
                      case 4:
                           System.exit(0);
                           break;
                   }
             }
     }
}

Code for another class(the operations)

public class operations
 {
  int a,b;   
  public void addnumbers()
   {
    int sum = a+b;
    System.out.println("ans is "+sum);
   }
  public void multiplynumbers()
   {
    int product = a*b;
    System.out.println("ans is "+product);
   }
    public void getnumbers(int number1,int number2)
   {
    a=number1;
    b=number2;
    System.out.println("the first number is "+number1);
    System.out.println("the second number is "+number2);
   }
 }

1.Make your instance variable static to avoid reinitialization

import java.util.Scanner;
class operations
 {
  static int a,b;  //Add static KeyWord 
  public void addnumbers()
   {
    int sum = a+b;
    System.out.println("ans is "+sum);
   }
  public void multiplynumbers()
   {
    int product = a*b;
    System.out.println("ans is "+product);
   }
    public void getnumbers(int number1,int number2)
   {
    a=number1;
    b=number2;
    System.out.println("the first number is "+number1);
    System.out.println("the second number is "+number2);
   }
 }

2.Write operations myoperations=new operations(); line out of while loop

class Main {
  public static void main(String args[])
    {
     int number1,number2,choice,sum,product;
     Scanner scan = new Scanner(System.in);
     operations myoperations=new operations();
     while(true)
             {
              //Scanner scan = new Scanner(System.in);
              //operations myoperations=new operations();
              System.out.println("\n1. Get numbers");
              System.out.println("\n2. Addition");
              System.out.println("\n3. Multiplication");
              System.out.println("\n4. Exit");
              choice = scan.nextInt();

              switch(choice)
                    { 
                      case 1:
                            System.out.println("enter the two numbers:");
                            number1=scan.nextInt();
                            number2=scan.nextInt();
                            myoperations.getnumbers(number1,number2);
                            break;
                      case 2:
                            myoperations.addnumbers();
                            break;
                      case 3:
                           myoperations.multiplynumbers();
                           break;
                      case 4:
                           System.exit(0);
                           break;
                   }
             }
     }
}

You just have to use the initialisation operations myoperations=new operations(); outside of the while loop. Otherwise you override the instance of the class each time. And so your stored values get lost each time you choose a case.

The problem is that you discard your variable myoperations every time the while-loop ends and creating a new myoperations . Your variable has to stay outside the loop like your ints do. Also the Scanner should stay outside, as long as you do not want to let the garbage collector work unnecessarily.

public static void main(String args[]) {
    int number1, number2, choice, sum, product;
    operations myoperations = new operations();
    Scanner scan = new Scanner(System.in);
    while (true) {
        System.out.println("\n1. Get numbers");
        System.out.println("\n2. Addition");
        System.out.println("\n3. Multiplication");
        System.out.println("\n4. Exit");
        choice = scan.nextInt();

        switch (choice) {
        case 1:
            System.out.println("enter the two numbers:");
            number1 = scan.nextInt();
            number2 = scan.nextInt();
            myoperations.getnumbers(number1, number2);
            break;
        case 2:
            myoperations.addnumbers();
            break;
        case 3:
            myoperations.multiplynumbers();
            break;
        case 4:
            System.exit(0);
            break;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM