簡體   English   中英

"如何將兩個 char 數組相互比較,以查看輸入的數組是否比另一個具有多個特定變量?"

[英]How do you compare two char arrays with each other to see if the inputted one has more than one specific variable than the other?

好的,所以我正在編寫一個程序,它會告訴用戶他們在命令行中輸入的單詞是否是等值線,我遇到了困難。

我該如何將用戶輸入的 char 數組與另一個進行比較,以查看它是否比另一個多於一個字母?

我有這個,我很困惑從這里去哪里。

char[] alphabet = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[] wordLetters = word.toCharArray(); 
boolean blnResult = Arrays.equals(alphabet,wordLetters);

我確定我的做法完全錯誤,但是我應該如何比較兩者以獲得結果呢?

如果我正確理解您的問題,這就是我的處理方式:

String isogram = "salad";
boolean isIsogram = !isogram.matches( ".*(.).*\\1.*" );
System.out.println( isIsogram );

或效率低下,但如果必須使用數組:

String isogram = "salad";
boolean isIsogram = true;
for ( int i = 0; i < isogram.length(); i++ ) {
    for ( int j = 0; j < isogram.length(); j++ ) {
        if ( i != j && isogram.charAt( i ) == isogram.charAt( j ) && ( Character.isLetter( isogram.charAt( i ) ) ) ) {
            isIsogram = false;
        }
    }
}
System.out.println( isIsogram );

等距圖基本上是沒有重復字母的單詞或短語。

代碼形式的函數簽名或程序輸出期望將有助於更好地回答。

但是,考慮到您正在使用一個返回true或false的函數:Set是實現它的方法。 繼續將字符放在Set或HashMap中,如果在插入set時字符已經存在於set中,則返回響應為false,否則返回true。

如何計算兩個字符數組的差異? 例如標志 = ['A', 'B', 'C']; flag2 = ['A']; 那么輸出應該是1

暫無
暫無

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

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