[英]Android compare booleans
我正在制作一個簡單的應用,您必須在其中切換按鈕/布爾值以嘗試猜測正確的組合。 我想知道最簡單的方法來比較切換布爾值和“正確”組合。 例如,如果用戶具有:
布爾值1:正確
布爾值2:假
布爾值3:真
但是正確的組合是:
布爾值1:正確
布爾值2:正確
布爾值3:真
我希望用戶看到一條消息,說您有3個正確的2個。 我有
public void go(View view){
if (bool1 == true && boo12 == true && bool3 == true) {
// do this
} else {
// display the correct amount of booleans the user has correct.
}
}
為正確的組合創建一個BitSet
(設置與“ true”相對應的位,清除與“ false”相對應的位)。
當您要檢查用戶的輸入時, BitSet
通過按下的按鈕創建一個BitSet
(設置為“ true”,清除“ false”)。
可以使用correctValue.equals(usersAttempt)
檢查正確性。
可以通過執行usersAttempt.xor(correctValue)
獲得計數,然后usersAttempt.cardinality()
將返回錯誤值的數量。
這需要最少的編碼量。 例如:
// Correct: [true,true,true]
BitSet correct = new BitSet(3);
correct.set(0); // <= true
correct.set(1); // <= true
correct.set(2); // <= true
// User's attempt (buttonN.isChecked() is just placeholder, drop in whatever
// code you actually use to get the state of your buttons):
BitSet attempt = new BitSet(3);
attempt.set(0, button0.isChecked()); // <= true in your example
attempt.set(1, button1.isChecked()); // <= false in your example
attempt.set(2, button2.isChecked()); // <= true in your example
// Check answer (produces false in your example):
boolean matchIsPerfect = attempt.equals(correct);
// Get the count (produces 1 in your example):
attempt.xor(correct);
int incorrectCount = attempt.cardinality();
// To get the correct count just subtract 'incorrectCount' from total.
// Another way to check if attempt is correct is 'if (incorrectCount == 0)'.
// Note that the remaining bits set in 'attempt' after the above xor()
// will correspond to the individual inputs that weren't correct.
這將使您支持任何大小,代碼清晰明了,您無需自己執行任何邏輯。 請注意,如果您有一系列按鈕,或者可以訪問給定索引的用戶輸入,則可以簡化按鈕->用戶的嘗試設置。
如果條件不一定總是為真,則說bool3必須為假,則可以使用類似的方法。 否則,正如其他答案所述,數組可能會最好地工作。
int correctCount = 0;
if (bool1) correctCount++;
if (bool2) correctCount++;
if (!bool3) correctCount++;
if (correctCount == 3) {
// do this
} else {
Toast.makeText(context, String.valueOf(correctCount) + " out of 3 correct", Toast.LENGTH_LONG).show();
}
假設您的“正確”組合將同時包含true
和false
值,則以下方法將返回匹配數目,如果數組長度不匹配,則返回-1
:
private int getMatches(boolean[] toggleValues, boolean[] matchPattern)
{
if(toggleValues.length != matchPattern.length)
return -1;
int matches = 0;
for (int j = 0; j < toggleValues.length; j++)
{
if(toggleValues[j] == matchPattern[j])
{
matches++;
}
}
return matches;
}
例如,如果您有3個ToggleButton
s, tb1
, tb2
和tb3
,則以下內容將返回2:
tb1.setChecked(true);
tb2.setChecked(false);
tb3.setChecked(false);
final boolean[] matchPattern = {true, false, true};
final boolean[] toggleValues = {tb1.isChecked(), tb2.isChecked(), tb3.isChecked()};
int matches = getMatches(toggleValues, matchPattern);
此方法將使您無需更改代碼即可輕松更改匹配模式,例如,通過從文件或SharedPreferences
將存儲的值讀取到matchPattern
數組中。
您可以執行此操作,因為布爾值的數量可以大於3。您可能要使用for循環(簡便方法)並將其迭代到值的結尾,然后將計數數量返回到值k。 這里k->布爾值的真實數量,b是包含布爾值數組的數組。
public void go(View view){
boolean b[]={bool1,bool2,bool3,bool4,bool5,bool6,bool7,bool8,bool9,bool10};
int k=0;
for(int i=0;i<b.length;i++)
{
if(b[i]==true)
{
k++;
}
}
if(k==b.length)
{
do task
}
else
{
Toast.makeText(getApplicationContext(), k+ "out of "+b.length+ "correct",Toast.LENGTH_LONG).show();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.