0

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

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();
     }
  }
}






}
}
4

1 回答 1

1

您检查错误,对于第二种if情况,发现:以下条件似乎也与您的评论相矛盾"if neither value was found"

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

您应该!从检查中删除。和is一样,将导致found和将导致。found2false(found == false && found2 == false)true!(true)false

再次进行第三次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
 {
 }
于 2013-11-10T20:04:10.583 回答