Java If语句数组地址

时间:2013-11-08 18:04:29

标签: java

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

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你可以添加两个bool标志,这些标志最初是假的,但是当找到你正在搜索的数字时,它们被设置为真。

 bool foundFlag1 = false;
 bool foundFlag2 = false;

然后

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

并对check1执行相同操作。

如果标志为false,则表示您无法找到这些输入!

答案 2 :(得分:0)

你几乎就在这里。这是Pseudocode

  
      
  1. 初始化Boolean Flag = false;
  2.   
  3. array中搜索号码。如果找到设置Flag = True
  4.   
  5. array中搜索号码后,请检查Flag
  6.   
  7. 如果Flag = False,则打印“无法找到地址”
  8.   

答案 3 :(得分:0)

我会这样做:

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,则无法找到该地址。