2013-03-19 30 views
2

因此, 的問題是基本的。在下面的代碼中,當我通過命令行參數java CommandLineDemo 3 5 *時,我當前目錄中的文件名被列出。在谷歌做一些研究我發現我們應該在命令行中提供*作爲'*'傳遞*作爲命令行參數在簡單的Java計算器

我的問題是,如何我的代碼進行修改,使其接受命令行'*'和執行的operand1產品和operand2

class CommandLineDemo { 
    public static void main(String[] args) { 
     int operand1 = Integer.parseInt(args[0]); 
     int operand2 = Integer.parseInt(args[1]); 
     char theOperator = args[2].charAt(0); 
     System.out.print(args[0] + args[2] + args[1] + " = "); 
     switch(theOperator) { 
      case ('+'): 
       System.out.println(operand1 + operand2); break; 
      case ('-'): 
       System.out.println(operand1 - operand2); break; 
      case ('*'): 
       System.out.println(operand1 * operand2); break; 
      case ('/'): 
       System.out.println(operand1/operand2); break; 
      default: 
       System.out.println("Invalid Operator selected"); 
     } 
    } 
} 
+0

嘗試使用 「*」 代替。該報價將被自動刪除 – MadProgrammer 2013-03-19 04:44:08

+0

@MadProgrammer由於一噸.. – 2013-03-19 04:57:05

+0

使用X光而不是* :) – Suranga 2013-03-19 05:05:24

回答

1

可以嘗試通過命令行參數作爲單個字符串(例如:「2 3 +」)與以下修改後的代碼。

import java.util.Arrays; 

class CommandLineDemo { 

    public static void main(String[] args) { 

     String strArray = Arrays.toString(args); 
     strArray = strArray.replace("[", "").replace("]", "").replaceAll("[, ]", ""); 
     String[] splits = strArray.split(""); 

     int operand1 = Integer.parseInt(splits[1]); 
     int operand2 = Integer.parseInt(splits[2]); 
     char theOperator = splits[3].charAt(0); 

     System.out.print(splits[1] + " " + splits[3] + " " + splits[2] + " = "); 

     switch(theOperator) { 
      case ('+'): 
       System.out.println(operand1 + operand2); break; 
      case ('-'): 
       System.out.println(operand1 - operand2); break; 
      case ('*'): 
       System.out.println(operand1 * operand2); break; 
      case ('/'): 
       System.out.println(operand1/operand2); break; 
      default: 
       System.out.println("Invalid Operator selected"); 
     } 
    } 
} 

用法&輸出看起來像這樣:

C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 +" 
2 + 3 = 5 

C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 -" 
2 - 3 = -1 

C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 *" 
2 * 3 = 6 

C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 /" 
2/3 = 0 

C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 a" 
2 a 3 = Invalid Operator selected 

C:\Users\sarath_sivan\Desktop> 
0

*是在shell元字符,這意味着它具有特殊的含義。所以你需要用\來逃避它不需要修改代碼,而只需鍵入\*意味着*

+0

啊.. :(任何幫助嗎? – 2013-03-19 04:51:23

+0

@MadProgrammer ?? – 2013-03-19 04:53:50

+0

@Snacky SRIKANTH是不是我的解決方案不工作了? – codeMan 2013-03-19 04:55:46

0

你不需要擴展你的程序。傳遞*'*'是因爲外殼(或者更準確地說,Linux shell中,有關於在cmd窗口小知識:P)將執行file name expansion,如果傳遞*直接,它將被擴展爲當前目錄中的所有文件。 '*'阻止此操作並將*作爲命令行參數傳遞給您的程序。

+0

但是,當我的代碼被編譯時,通過''*''在交換機中返回默認大小寫 – 2013-03-19 04:50:41

+1

@Snacky它在我的機器上工作。我使用'java CommandLineDemo 3 5'*''。我的shell是bash 4.1.2。順便說一句,你的環境是什麼? Linux或Windows CMD? – 2013-03-19 04:54:23

+0

我正在使用Windows – 2013-03-19 04:58:18