Java If语句数组地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19865656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:51:02  来源:igfitidea点击:

Java If statement Array Address

java

提问by please delete me

I have a code that will give me the coordinates of certain points in an array using user input. What code would I add to make the code output say that the address could not be found if the number in the array is not there? I'm pretty sure I need an else statement but I can't get it to work. Here is the code I have right now.

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

回答by masotann

You could add two boolflag which are false at first but when the numbers youre searching are found, they are set to true.

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

 bool foundFlag1 = false;
 bool foundFlag2 = false;

Then

然后

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

and do the same for check1.

并为check1.

If the flags are false, you know that you couldn't find those inputs!

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

回答by Sam I am says Reinstate Monica

step 1:make a flag say

第 1 步:做一个标志说

boolean check1Found = false;

step 2:if you find the value, set the flag to true

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

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

step 3:after your loop is finished, print a message if that flag is still false

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

if(check1Found == false)
{
    System.out.println("check 1 not found");
}

回答by Prateek

You are almost right here. Here is the Pseudocode

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

  1. Initialize Boolean Flag = false;
  2. Search for number in array. if found set Flag = True.
  3. After searching the number in array, check Flag.
  4. If Flag = False, print "the address could not be found"
  1. 初始化 Boolean Flag = false;
  2. 在 中搜索号码array。如果找到 set Flag = True
  3. 在 中搜索号码后array,检查Flag
  4. 如果Flag = False,打印“找不到地址”

回答by Dom

I would do this:

我会这样做:

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

The flags are set to true when the array is found, so if either are false than that address could not be found.

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