基本计算器帮助Java

时间:2016-11-13 03:44:42

标签: java project calculator

我正在尝试为我的学校项目创建一个计算器,但由于某些原因,当我尝试编译时,它说我有问题,我不知道如何解决它。我收到错误“此行的多个标记      - 本地变量inputA可能没有      初始化      - 本地变量inputB可能没有      初始化“在这一部分:

     atimesb = (inputA * inputB);
     adividedbyb = (inputA / inputB);
     aplusb = (inputA + inputB);
     aminusb = (inputA - inputB);

错了。

import java.util.Scanner;
public class Calculator {

    public static void main(String[] args) {

         int inputA;
         int inputB;
         int atimesb;
         int adividedbyb;
         int aplusb;
         int aminusb;
         atimesb = (inputA * inputB);
         adividedbyb = (inputA / inputB);
         aplusb = (inputA + inputB);
         aminusb = (inputA - inputB);

         String operation;

         Scanner in = new Scanner(System.in);  

         System.out.println("Enter the first number");  
         inputA = in.nextInt();  

         System.out.println("Enter the operation"); 
         operation = in.next();

         System.out.println("Enter the second number");  
         inputB = in.nextInt();  



         if (operation.equals ("+"))
         {
             System.out.println(inputA + " + " + inputB + "=" + aplusb);
         }
         if (operation.equals ("-"))
         {
             System.out.println(inputA + " - " + inputB + "=" + aminusb);
         }
         if (operation.equals ("/"))
         {
             System.out.println(inputA + " / " + inputB + "=" + adividedbyb);
         }
         if (operation.equals ("*"))
         {
             System.out.println(inputA + " * " + inputB + "=" + atimesb);
         }
         if (operation.equals ("add"))
         {
             System.out.println(inputA + " * " + inputB + "=" + aplusb);
         }
         if (operation.equals ("subtract"))
         {
             System.out.println(inputA + " - " + inputB + "=" + aminusb);
         }
         if (operation.equals ("divide"))
         {
             System.out.println(inputA + " / " + inputB + "=" + adividedbyb);
         }
         if (operation.equals ("multiply"))
         {
             System.out.println(inputA + " * " + inputB + "=" + atimesb);
         }


}
}

如果有人可以帮助我,我会非常感激。

1 个答案:

答案 0 :(得分:2)

在使用之前,您需要读取值。 像这样:

     System.out.println("Enter the first number");  
     inputA = in.nextInt();  

     System.out.println("Enter the operation"); 
     operation = in.next();

     System.out.println("Enter the second number");  
     inputB = in.nextInt();  

     atimesb = (inputA * inputB);
     adividedbyb = (inputA / inputB);
     aplusb = (inputA + inputB);
     aminusb = (inputA - inputB);