1

我有一个代码,它将使用用户输入为我提供数组中某些点的坐标。如果数组中的数字不存在,我将添加什么代码以使代码输出显示找不到地址?我很确定我需要一个 else 语句,但我无法让它工作。这是我现在拥有的代码。

import java.util.Scanner;

public class LabActivityArray 
{
    public static void main (String[] args) 
    {
        Scanner scanner = new Scanner (System.in); 
        int rows; 
        int columns;
        int check1,check2;

        System.out.println("Enter number of rows: "); 

        rows = scanner.nextInt(); 

        System.out.println ("Now enter the number of columns: "); 

        columns = scanner.nextInt(); 

        int[][] array = new int[rows][columns]; 

        System.out.println("Enter the number to start the array: ");

        int value = scanner.nextInt(); 
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                array[i][j]=value++;
                System.out.print(array[i][j] + "   " ); 
            }    
            System.out.println();
        }

        System.out.println("Please give one integer value to be checked in the array: "); 
        check1 = scanner.nextInt(); 

        System.out.println ("Please give a second integer value to be checked in the array: "); 

        check2 = scanner.nextInt(); 

        for ( int i = 0; i < rows; ++i ) 
        {
            for ( int j = 0; j < columns; ++j ) 
            {
                if ( array[i][j] == check1 ) 
                {
                    System.out.print(array[i][j] + " is located at address array[" + i + "," + j + "]");       
                }
                if ( array[i][j] == check2 ) 
                {
                    System.out.print("\n" + array[i][j] + " is located at address array[" + i + "," + j + "]");
                    System.out.println(); 
                }
            }
        }
    }       
}
4

4 回答 4

2

第 1 步: 做一个标志说

boolean check1Found = false;

第 2 步: 如果找到值,则将标志设置为true

if ( array[i][j] == check1 ) 
{
    System.out.print(array[i][j] + " is located at address array[" + i + "," + j + "]"); 
    check1Found = true;     
}

第 3 步:循环完成后,如果该标志仍然存在,则打印一条消息false

if(check1Found == false)
{
    System.out.println("check 1 not found");
}
于 2013-11-08T18:13:15.703 回答
1

您可以添加两个bool最初为 false 的标志,但当找到您搜索的数字时,它们将设置为 true。

 bool foundFlag1 = false;
 bool foundFlag2 = false;

然后

if ( array[i][j] == check2 ) {
    foundFlag2 = true;
    ..
}

并为check1.

如果标志为假,您就知道您找不到这些输入!

于 2013-11-08T18:12:00.887 回答
1

你几乎就在这里。这是伪代码

  1. 初始化Boolean Flag = false;
  2. 在 中搜索号码array。如果找到集合Flag = True
  3. 在中搜索号码后array,检查Flag
  4. 如果Flag = False,打印“找不到地址”
于 2013-11-08T18:13:16.080 回答
1

我会这样做:

boolean check1Flag = false;
boolean check2Flag = false;
for ( int i = 0; i < rows; ++i )
{
    for ( int j = 0; j < columns; ++j )
    {
        if ( array[i][j] == check1 ) 
        {
            System.out.println(array[i][j] + " is located at address array[" + i + "," + j + "]");       
             check1Flag = true;               
        }
        if ( array[i][j] == check2 ) 
        {
             System.out.println(array[i][j] + " is located at address array[" + i + "," + j + "]");
             check2Flag = true;
        }


     }
}
if(!check1Flag)
{
    System.out.println("Can't find " + check1);
}
if(!check2Flag)
{
    System.out.println("Can't find " + check2);
}

找到数组时,标志设置为 true,因此如果其中一个为 false,则无法找到该地址。

于 2013-11-08T18:15:52.350 回答