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

时间:2018-02-28 13:36:49

标签: java

所以基本上在main函数的情况1中,我试图将用户输入的两个值存储到另一个类中。然后,如果我立即转到案例2,输出将是之前输入的两个值的总和。我的问题是如何更改我的代码,以便案例2和3能够使用我之前在案例1中存储的值?谢谢。

主要功能代码:

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;
                   }
             }
     }
}

另一个类的代码(操作)

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);
   }
 }

3 个答案:

答案 0 :(得分:0)

  

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();跳出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;
                   }
             }
     }
}

答案 1 :(得分:0)

您只需在while循环之外使用初始化operations myoperations=new operations();。否则,每次都覆盖该类的实例。因此,每次选择案例时,您的存储值都会丢失。

答案 2 :(得分:0)

问题是,每次myoperations结束并创建新while-loop时,您都会丢弃变量myoperations。你的变量必须像你的注册一样保持在循环之外。此外,只要您不想让垃圾收集器不必要地工作,扫描仪就应该留在室外。

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;
        }
    }
}