2011-08-21 161 views
3

我刚刚学过java,但是从我从C++学来的旧经验,我想我可以编写一个命令行计算器,它只需一行支持所有4个基本操作符。但我有一些问题。CommandLine Java计算器

这是我的代码:

import java.util.Scanner; 

public class omg { 
    public static void main(String args[]) { 
     int fnum,snum,anum = 0; 
     String strtype; //The original calculation as string 
     char[] testchar; //Calculation as chararray 
     char currentchar; //current char in the char array for the loop 
     int machinecode = 0; //Operator converted to integer manually 
     String tempnumstr; //first and second numbers temp str to be converted int 
     int operatorloc = 0; //operator location when found 
     char[] tempnum = new char[256]; 
     Scanner scan = new Scanner(System.in); // The scanner obviously 
     System.out.println("Enter The Calculation: "); 
     strtype = scan.nextLine(); 
     testchar = strtype.toCharArray(); //converting to char array 
     for(int b = 0; b < testchar.length; b++) //operator locating 
     { 
      currentchar = testchar[b]; 
      if(currentchar == '+') { 
       machinecode = 1; 
       operatorloc = b; 
      } 
      else if(currentchar == '-') { 
       machinecode = 2; 
       operatorloc = b; 
      } 
      else if(currentchar == '*') { 
       machinecode = 3; 
       operatorloc = b; 
      } 
      else if(currentchar == '/') { 
       machinecode = 4; 
       operatorloc = b; 
      } 
     } 
     for(int t = 0;t < operatorloc;t++) { //transferring the left side to char 
      tempnum[t] = testchar[t]; 
     } 
      tempnumstr = tempnum.toString(); //converting char to string 
      fnum = Integer.parseInt(tempnumstr); //parsing the string to a int 
     for(int temp = operatorloc;temp < testchar.length;temp++) { //right side 
      for(int t = 0;t<(testchar.length-operatorloc);t++) { 
       tempnum[t] = testchar[temp]; 
      } 
     } 
     tempnumstr = tempnum.toString(); //converting to char 
     snum = Integer.parseInt(tempnumstr); //converting to int 
     switch(machinecode) { //checking the math to be done 
     case 1: 
      anum = fnum + snum; 
      break; 
     case 2: 
      anum = fnum - snum; 
      break; 
     case 3: 
      anum = fnum * snum; 
      break; 
     case 4: 
      anum = fnum/snum; 
     } 
     System.out.println(anum); //printing the result 
    } 
} 

这是我的代码,但是当我运行它,它会问我的计算,然后给这个错误。

Exception in thread "main" java.lang.NullPointerException 
    at omg.main(omg.java:38) 

有可能是一个更好,更容易的方式做到这一点,我想听到一个更好的方式和我的方式修复。提前

+2

你的C(++)的习惯是可见的。 Java中的类应以大写字母开头。变量中的每个单词也应以大写字母开头(例如:machineCode)。变量通常在使用时声明和初始化。并非所有在方法的开始。 machineCode应该是一个枚举而不是int。 –

+1

只有在你的第二篇文章中有正确代码缩进的格式化代码才能使用! –

回答

6

气垫船Full Of Eels已经向你指出了NullPointerException.的原因除此之外,我看到了很多可以在你的代码中改进的东西。以下是我想做到这一点:

import java.util.Scanner; 

public class SimpleCalculator { 

    public static void main(String[] args) { 
     System.out.println("Please enter your calculation"); 
     Scanner scanner = new Scanner(System.in); 
     int left = scanner.nextInt(); 
     String op = scanner.next(); 
     int right = scanner.nextInt(); 
     System.out.println(compute(left, op, right)); 
    } 

    private static int compute(int left, String op, int right) { 
     switch (op.charAt(0)) { 
     case '+': 
      return left + right; 
     case '-': 
      return left - right; 
     case '*': 
      return left * right; 
     case '/': 
      return left/right; 
     } 
     throw new IllegalArgumentException("Unknown operator:" + op); 
    } 
} 

注意,扫描仪假定有空格前后的操作后。

输出示例:

Please enter your calculation 
1 + 2 
3 

详细的改进:

  1. 变量可以声明,其中首先使用它们。在Java中习惯使用(更短的代码长度,不需要重复变量名称)。
  2. Scanner除了读取整行外,还提供了标记。没有必要重新发明轮子。
  3. char(是一个整数类型)可以是switch编辑。
+2

尼斯1+。只是我不想提及的一个小小建议:与它们一起处置资源总是一个好习惯,这包括Scanner对象,在您使用它时应该关闭它。这真的不会对这个计划有所帮助,这就是为什么我讨厌提及它的原因,但这是一个很好的习惯,因为有时候它会影响它。 :) –

+0

你的方式是令人难以置信的更好,但我仍然在学习java :)。我只是想尝试这种方式。 – Learath2

7

感谢您声明:

char[] tempnum = null; 

但是,你在哪里设置=一个非空值?因此,无论何时您尝试使用它,就像它是一个完全启动的物体一样,您将得到一个NPE抛出。

编辑:你的代码还有其他问题,包括调用数组中的toString(),它将返回数组的默认值toString - 不是你想要的那种情况。

所以,与其这样:

tempnumstr = tempnum.toString(); 

你可能想是这样的:

tempnumstr = new String(tempnum); 

或可能

tempnumstr = new String(tempnum).trim(); // get rid of trailing whitespace if needed 

编辑2:你似乎有两个字符数组中你的程序,tempnum和testchar,一个填充字符和一个没有。他们两个的目的是什么?考虑用一些评论加密你的代码,这样我们可以更好地理解它,并且能够更好地帮助你。

+0

我修复了NPE并评论了我的代码,但现在我在'java.lang.Integer.parseInt(Unknown Source)' – Learath2

+0

@ Leararth2:您遇到了新问题,我们需要查看您的更新代码。由于这与原始问题无关并且保持清洁,所以最好在SO上提出一个新问题。 –

0

在38行尝试存取权限的变量tempnum被初始化为null你必须初始化变量tempnum这样的: tempnum = new char[n] 其中n是数组的长度

2

你的问题是这样的线:

tempnum[t] = testchar[t];

如先前宣布它为空这将抛出一个NullPointerException:char[] tempnum = null;

您需要将其更改为char[] tempnum = new char[size];,将其初始化为空数组以保存类型char。其中size是任何整数。

1
char[] tempnum = null; 

应设置类似

char[] tempnum = new char[4]; 

基本上在38行使用时为空。

0

您忘记分配tempNum,当您尝试在数组上下文中使用它时,结果为NUllPointerException

char[].toString()不会做你所期望的(它返回一个数组对象的散列码),使用数组的内容创建一个字符串使用new String(char[])

0

首先,它错误在这行:tempnum[t] = testchar[t]; 原因:tempnum没有指向任何东西(空) 修复:tempnum = testchar;tempnum = new char[testchar.length]