簡體   English   中英

如何檢查'*'是否存在於java中的數組中?

[英]How to check if '*' exists in an array in java?

我正在嘗試在Maven 中創建一個計算器,但在嘗試實現乘法時遇到了問題。 當我將launch.jsonargs 數組launch.json2 * 6 ,它什么也沒返回。我也試過 - "\\\\*但什么也沒發生。此外,當我將乘boolean mul = Arrays.stream(args).anyMatch("".equals);值設置為boolean mul = Arrays.stream(args).anyMatch("".equals); ,它起作用了。

這是我的代碼 -

import java.util.Arrays;
/**
 * Hello world!
 */

public final class App {
    

    /**
     * @param args The arguments of the program.
     */


    public static void main(String[] args) {
        boolean plus = Arrays.stream(args).anyMatch("+"::equals);
        boolean minus = Arrays.stream(args).anyMatch("-"::equals);
        boolean div = Arrays.stream(args).anyMatch("/"::equals);
// here -> 
boolean mul = Arrays.stream(args).anyMatch("*"::equals);
/**
Or should I put it like this ->
boolean mul = Arrays.stream(args).anyMatch("\\*"::equals);
*/
        if (plus == true) {
            String plus1 = args[0];
            String plus2 = args[2];
            int plus1_int = Integer.parseInt(plus1);
            int plus2_int = Integer.parseInt(plus2);
            System.out.println(plus1_int + plus2_int);
        }else {
            ;
        }

        if (minus == true) {
             String min1 = args[0];
             String min2 = args[2];
             int min1_int = Integer.parseInt(min1);
             int min2_int = Integer.parseInt(min2);
             System.out.println(min1_int - min2_int);
        }else {
            ;
        }
        

        if (div == true) {
            String div2 = args[2];
            String div1 = args[0];
            int div1_int = Integer.parseInt(div1);
            int div2_int = Integer.parseInt(div2);
            System.out.println(div1_int / div2_int);
          }else {
            ;
        }
 // and here ->
        if (mul == true) {
            String mul1 = args[0];
            String mul2 = args[2];
            int mul1_int = Integer.parseInt(mul1);
            int mul2_int = Integer.parseInt(mul2);
            System.out.println(mul1_int * mul2_int);
        }else { 
            ;
        }

        
        

    }
}

我做錯了什么嗎? 提前致謝!

*當您在命令行上使用它時,它指的是工作目錄中存在的文件或目錄。 因此,您的 args 而不是2 , * , 6而是2 , file1 , directory1 , etc , 6

解決方案是通過引號或雙引號將其轉義。

2 '*' 6

或者

2 "*" 6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM