我在2d數組中搜索3個數字,第三個數字不在數組中,當它向用戶輸出數字不在數組中時,它將打印10次並且即時猜測,因爲for循環上升到10.我怎樣才能得到語句「8675不在陣列」只打印出1次。if循環中的語句多次打印
public class MultiDimensionalArray {
public static void main(String[] args) {
int[][] oned = {
{ 1115, 7307, 1004, 8820, 4322, 2286, 6183, 8455, 5569, 9930 },
{ 1155, 7749, 8582, 1180, 4463, 3107, 8838, 9842, 2308, 3453 },
{ 6229, 5449, 1967, 2501, 9610, 5600, 6996, 7375, 5629, 35 },
{ 6677, 2464, 5017, 5881, 639, 2772, 3465, 8718, 7747, 5621 },
{ 1646, 8533, 4250, 8119, 8163, 1236, 4433, 4093, 7834, 3037 },
{ 7069, 6522, 9604, 1609, 5725, 6255, 438, 274, 7978, 3358 },
{ 6631, 3401, 5975, 108, 3696, 2773, 1697, 9803, 7056, 4996 },
{ 7109, 4895, 5930, 7634, 7070, 5265, 7456, 5223, 9725, 368 },
{ 1201, 7776, 9000, 8654, 9635, 922, 2932, 4814, 1624, 1062 },
{ 7561, 6587, 7398, 4254, 5797, 7325, 4368, 5830, 8937, 5726 },
{ 7740, 8238, 7761, 6142, 4643, 7416, 2062, 5563, 1298, 7899 },
{ 1868, 6088, 3071, 7563, 7780, 2714, 7081, 2565, 3086, 766 },
{ 2284, 9931, 8664, 7248, 6768, 5657, 8404, 807, 7357, 2204 },
{ 9911, 6832, 8167, 546, 2709, 2046, 8465, 4171, 1841, 6106 },
{ 2123, 9005, 406, 6873, 3848, 4760, 2912, 1504, 9052, 270 },
{ 8700, 8182, 1153, 1154, 9288, 8227, 6165, 7257, 7908, 1769 },
{ 7355, 3880, 390, 1496, 6984, 7553, 981, 8049, 6948, 7312 },
{ 830, 4777, 5100, 897, 9941, 8513, 9318, 3146, 5298, 8452 },
{ 6678, 6535, 1471, 5225, 5513, 1912, 624, 8802, 5331, 4675 },
{ 4916, 2517, 4604, 4947, 9973, 9347, 9390, 8633, 60, 8983 },
{ 9977, 2505, 8436, 1285, 472, 568, 8696, 5198, 5630, 5087 },
{ 6287, 4834, 6184, 3761, 7922, 3163, 6836, 6621, 3338, 6575 },
{ 7105, 5863, 5113, 1346, 1223, 7733, 1323, 2301, 3021, 8612 },
{ 2976, 282, 271, 8111, 1320, 3441, 7129, 513, 4564, 7278 },
{ 3916, 7150, 9606, 8058, 7533, 8106, 539, 977, 32, 1074 },
{ 5859, 6361, 7489, 8347, 9441, 8281, 7728, 7944, 5272, 1598 },
{ 6078, 4624, 634, 9183, 7772, 6187, 3565, 4912, 2875, 8405 },
{ 1031, 1679, 8287, 689, 4855, 6386, 8616, 8608, 2842, 4986 },
{ 3321, 5150, 1410, 3159, 1328, 30, 191, 7133, 2797, 5334 },
{ 8610, 5512, 8141, 1398, 5918, 2641, 9014, 4475, 4590, 8672 } };
// Is 8227 in the array?
int number = 8227;
for (int row = 0; row < 10; row++) {
for (int column = 0; column < 30; column++) {
if (oned[column][row] == number)
{
System.out.println("8227 is in the array");
}
}
}
// Is 9911 in the array?
int check = 9911;
for (int row = 0; row < 10; row++) {
for (int column = 0; column < 30; column++) {
if (oned[column][row] == check)
{
System.out.println("9911 is in the array");
}
}
}
// Is 8675 in the array?
int look = 8675;
for (int row = 0; row < 10; row++) {
for (int column = 0; column < 30; column++) {
if (oned[column][row] == look)
{
System.out.println("8675 is in the array");
}
else if (oned[column][row] != look)
{
System.out.println("8675 is not in the array");
}
}
}
}
}
我得到的輸出是
8227 is in the array
9911 is in the array
8675 is not in the array
8675 is not in the array
8675 is not in the array
8675 is not in the array
8675 is not in the array
8675 is not in the array
8675 is not in the array
8675 is not in the array
8675 is not in the array
8675 is not in the array
你只需要一個循環塊來檢查所有的數字 –