根据找到的值的数量,程序应该以某种方式做出反应。
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();
}
}
}
}
}