我如何使用嵌套循环来帮助确定我是否有 2 对不同的匹配骰子
How can i use a nested loops to help identify if i have 2 different matching pairs of dices
所以我刚上了一堂关于循环和嵌套循环的课。我的教授说嵌套循环可以帮助我们完成一些任务,比如知道我们是否用 4 个骰子掷出 2 个不同的匹配对 (4242) 我对它的工作原理有点困惑。
所以我开始研究它,这就是我能够创造的。
public boolean 4matchDice(String dice){
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
}
}
我使用布尔值作为 return,因为它会告诉我们是否有 2 个不同的匹配对。
问题是,我在循环中放了什么?这是最让我困惑的。
如果您只是比较两组各有两个骰子,这就足够了:
public boolean match4Dice(int first, int second, int third, int fourth) {
if ((first == third && second == fourth) || (first == fourth && second == third)) {
return true;
}
return false;
}
但是,如果您要比较 2 组和任意数量的骰子,则以下算法更适合您。
public boolean matchDice(String firstDiceSet, String secondDiceSet) {
// validate input, string must contain numbers from 1 - 6 only.
// lenghts of strings firstDiceSet & secondDiceSet must be equal
// works for any number of dice in each set.
// The dice only match if all numbers in the firstDiceSet all present in the secondDiceSet also.
// Let us count the matching numbers to check if this true.
int numberOfMatches = 0;
for (int i = 0; i < firstDiceSet.length(); i++) {
for (int j = 0; j < secondDiceSet.length(); j++) {
if (firstDiceSet[i] == secondDiceSet[j]) { // and not used
// increment number of matches
// mark secondDiceSet[j] as used, so that you do not count the same match twice.
// account for cases where firstDiceSet = "33" and the secondDiceSet = "35"
}
}
}
// your dice set match if the following condition is true
return (numberOfMatches == secondDiceSet.length());
}
嗨,我刚刚编写了一个将“4242”作为输入的解决方案,尽管我认为 sindhu_sp 的方法更实用。我只是想向您展示另一个示例,以帮助您学习 java!
public static boolean fourMatchDice(String dice){
int match = 0;
for (int i = 0; i < dice.length(); i++){
for (int j = i+1; j < dice.length(); j++){
if (dice.toCharArray()[i] == dice.toCharArray()[j]){
System.out.println("does " + dice.toCharArray()[i] + " = " + dice.toCharArray()[j]);
match ++;
}
}
}
if(match == 2) *EDIT* //Change to (match >= 2) if 4 of the same pair is allowed.
return true;
return false;
}
public static void main(String[] args) {
System.out.println(fourMatchDice("4242"));
}
输出:
does 4 = 4
does 2 = 2
true
这是我想出的解决方案,似乎为我 运行.
的所有测试用例返回了正确的结果
public static boolean matchDice(String dice) {
char[] diceArray = dice.toCharArray();
int pairs = 0;
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < dice.length(); j++) {
if (diceArray[i] == diceArray[j]) {
diceArray[i] = 'X';
diceArray[j] = 'Y';
pairs++;
}
}
}
return (pairs > 1);
}
所以我刚上了一堂关于循环和嵌套循环的课。我的教授说嵌套循环可以帮助我们完成一些任务,比如知道我们是否用 4 个骰子掷出 2 个不同的匹配对 (4242) 我对它的工作原理有点困惑。
所以我开始研究它,这就是我能够创造的。
public boolean 4matchDice(String dice){
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
}
}
我使用布尔值作为 return,因为它会告诉我们是否有 2 个不同的匹配对。
问题是,我在循环中放了什么?这是最让我困惑的。
如果您只是比较两组各有两个骰子,这就足够了:
public boolean match4Dice(int first, int second, int third, int fourth) {
if ((first == third && second == fourth) || (first == fourth && second == third)) {
return true;
}
return false;
}
但是,如果您要比较 2 组和任意数量的骰子,则以下算法更适合您。
public boolean matchDice(String firstDiceSet, String secondDiceSet) {
// validate input, string must contain numbers from 1 - 6 only.
// lenghts of strings firstDiceSet & secondDiceSet must be equal
// works for any number of dice in each set.
// The dice only match if all numbers in the firstDiceSet all present in the secondDiceSet also.
// Let us count the matching numbers to check if this true.
int numberOfMatches = 0;
for (int i = 0; i < firstDiceSet.length(); i++) {
for (int j = 0; j < secondDiceSet.length(); j++) {
if (firstDiceSet[i] == secondDiceSet[j]) { // and not used
// increment number of matches
// mark secondDiceSet[j] as used, so that you do not count the same match twice.
// account for cases where firstDiceSet = "33" and the secondDiceSet = "35"
}
}
}
// your dice set match if the following condition is true
return (numberOfMatches == secondDiceSet.length());
}
嗨,我刚刚编写了一个将“4242”作为输入的解决方案,尽管我认为 sindhu_sp 的方法更实用。我只是想向您展示另一个示例,以帮助您学习 java!
public static boolean fourMatchDice(String dice){
int match = 0;
for (int i = 0; i < dice.length(); i++){
for (int j = i+1; j < dice.length(); j++){
if (dice.toCharArray()[i] == dice.toCharArray()[j]){
System.out.println("does " + dice.toCharArray()[i] + " = " + dice.toCharArray()[j]);
match ++;
}
}
}
if(match == 2) *EDIT* //Change to (match >= 2) if 4 of the same pair is allowed.
return true;
return false;
}
public static void main(String[] args) {
System.out.println(fourMatchDice("4242"));
}
输出:
does 4 = 4
does 2 = 2
true
这是我想出的解决方案,似乎为我 运行.
的所有测试用例返回了正确的结果 public static boolean matchDice(String dice) {
char[] diceArray = dice.toCharArray();
int pairs = 0;
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < dice.length(); j++) {
if (diceArray[i] == diceArray[j]) {
diceArray[i] = 'X';
diceArray[j] = 'Y';
pairs++;
}
}
}
return (pairs > 1);
}