简体   繁体   English

在switch语句之后将值存储到另一个类中,并在另一个switch语句中重用该变量

[英]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. 因此,基本上在主要功能的情况1中,我试图将用户输入的两个值存储到另一个类中。 Then if I go to case 2 immediately, the output will be the sum of the two values that were input earlier. 然后,如果我立即进入情况2,输出将是之前输入的两个值的总和。 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? 我的问题是如何更改代码,使案例2和3能够使用之前在案例1中存储的值? 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 1,将实例变量设为静态以避免重新初始化

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(); 2.写操作myoperations = new operations(); line out of while loop 在while循环中排队

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(); 您只需要使用初始化operations myoperations=new operations(); outside of the while loop. 在while循环之外。 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 . 问题是,每次while-loop结束时,您都会丢弃变量myoperations并创建新的myoperations Your variable has to stay outside the loop like your ints do. 您的变量必须像int一样保持在循环之外。 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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