Java If 语句数组地址



我有一个代码,可以使用用户输入为我提供数组中某些点的坐标。我会添加什么代码来使代码输出说如果数组中的数字不存在,则找不到地址?我很确定我需要一个 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(); 
                }
            }
        }
    }       
}

第 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");
}
您可以

添加两个bool标志,这些标志起初为假,但当您正在搜索的数字被找到时,它们被设置为true。

 bool foundFlag1 = false;
 bool foundFlag2 = false;

然后

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

并对check1做同样的事情.

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

你快到这里了。这是伪代码

  1. 初始化Boolean Flag = false;
  2. array 中搜索数字。如果找到设置为 Flag = True
  3. array中搜索号码后,选中Flag
  4. 如果Flag = False,请打印"找不到地址"

我会这样做:

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,因此如果任一为假,则找不到该地址。

最新更新