如果语句在数组中使用布尔值

时间:2013-11-10 20:01:55

标签: java arrays boolean boolean-expression

根据找到的值数量,程序应该以某种方式作出反应。

import javax.swing.*;
import java.util.*;


public class labActivityArrays{

public static void main(String[] args){

//1

//ask for number of rows
String rowS;
rowS = JOptionPane.showInputDialog("Number of rows: ");
int row =  Integer.parseInt(rowS);

//ask for number of columns
String columnS;
columnS = JOptionPane.showInputDialog("Number of columns: ");
int column = Integer.parseInt(columnS);

//ask for initial value
String initialS;
initialS = JOptionPane.showInputDialog("Initial value: ");
int initial =  Integer.parseInt(initialS);

//define the new array
int[][] arr = new int [row][column];

int temp = initial; 

for(int i=0; i < row; i++){
  for(int j=0; j < column; j++){
     arr[i][j] = temp;
     temp += 1;
  }
}
//print the array
for(int i=0; i < row; i++){
  for(int j=0; j < column; j++){
     System.out.print(arr[i][j] + " ");
  }
  System.out.println();
}

//2

//ask for the first value to check for
String check1S;
check1S = JOptionPane.showInputDialog("First value to check for: ");
int check1 =  Integer.parseInt(check1S);

//ask for the second value to check for
String check2S;
check2S = JOptionPane.showInputDialog("Second value to check for: ");
int check2 =  Integer.parseInt(check2S);

//check for the first value  
boolean found = false;
int n = -1;
int o = -1;

while(!found && ++n < row){
  o = -1;
  while(!found && ++o < column){
     found = (arr[n][o] == check1);
  }
}

//print whether the value is present or not
if(found){
  System.out.println("Found " + check1 + " at index [" + n + "][" + o + "].");
}
else{
  System.out.println(check1 + " is not in this array.");
}

//check for the second value
boolean found2 = false; 
int m = -1; 
int p = -1;

while(!found2 && ++m < row){
  p = -1;
  while(!found2 && ++p < column){
     found2 = (arr[m][p] == check2);
  }
} 
//print whether the value is present or not
if(found2){
  System.out.println("Found " + check2 + " at index [" + m + "][" + p + "].");
}
else{
  System.out.println(check2 + " is not in this array.");
}

有问题的部分是以下代码。除了找到的布尔值似乎永远不对,所以它没有正确响应。

//3

//if only one value was found
if((found == true && found2 == false) || (found == false && found2 == true)){
  for(int i=0; i < row; i++){
     for(int j=0; j < column; j++){
        System.out.print(arr[i][j] + " ");
     }
     System.out.println();
  }
}

//if neither value was found
if(!(found == false && found2 == false)){
  String askAgain;
  askAgain = JOptionPane.showInputDialog("Since no user values were found, would you like to search for 2 more values? (yes/no) ");
     if(!(askAgain.equals("yes") || askAgain.equals("no"))){
        askAgain = JOptionPane.showInputDialog("Since no user values were found, would you like to search for 2 more values? (yes/no) ");
     }
  if(askAgain.equals("yes")){
     //ask for the first value to check for
     check1S = JOptionPane.showInputDialog("First value to check for: ");
     check1 =  Integer.parseInt(check1S);

     //ask for the second value to check for
     check2S = JOptionPane.showInputDialog("Second value to check for: ");
     check2 =  Integer.parseInt(check2S);

     found = false;
     n = -1;
     o = -1;

     //check for the first value  
     while(!found && ++n < row){
        o = -1;
        while(!found && ++o < column){
           found = (arr[n][o] == check1);
        }
     }     

     //print whether the value is present or not
     if(found){
        System.out.println("Found " + check1 + " at index [" + n + "][" + o + "].");
     }
     else{
        System.out.println(check1 + " is not in this array.");
     }

     //check for the second value
     found2 = false; 
     n = -1; 
     o = -1;

     while(!found2 && ++n < row){
        o = -1;
        while(!found2 && ++o < column){
           found2 = (arr[n][o] == check2);
        }
     }
     //print whether the value is present or not
     if(found2){
        System.out.println("Found " + check2 + " at index [" + n + "][" + o + "].");
     }
     else{
        System.out.println(check2 + " is not in this array.");
     }
  }
}

//if both values were found
if(found == false && found2 == false){
  String swap;
  swap = JOptionPane.showInputDialog("Would you like the two values to be swapped? (yes/no)");
     if(!(swap.equals("yes") || swap.equals("no"))){
        swap = JOptionPane.showInputDialog("Would you like the two values to be swapped? (yes/no)");
     }
  if(swap.equals("yes")){
     int one = arr[n][o];
     int two = arr[m][p];
     arr[n][o] = two;
     arr[m][p] = one;

     for(int i=0; i < row; i++){
        for(int j=0; j < column; j++){
           System.out.print(arr[i][j] + " ");
        }
        System.out.println();
     }
  }
}






}
}

1 个答案:

答案 0 :(得分:1)

您错误地检查了,对于发现的第二个if案例:以下条件似乎与您的评论"if neither value was found"

相矛盾
//if neither value was found
if(!(found == false && found2 == false))

您应该从检查中删除!。由于foundfound2都是false(found == false && found2 == false)会产生true!(true)会产生false。< / p>

再次进行第三次if检查,与您自己的评论一起显示:

//if both values were found
if(found == false && found2 == false)

你可能想要它:

//if both values were found
if(found == true && found2 == true)

然而,在bitwise operators(^, &, |)的帮助下,我只会写下三个if条件,如下所示:

 if(found ^ found2 == true) // at least one of them is true: found
 {
   // do blah blah 
 }
 else if(found & found2 == true) // both of them is true: found
 {
     // do blah blah
 }
 else // if above two is false this one obviously true: none of them found
 {
 }