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
Java If statement Array Address
提问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 bool
flag 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
你几乎就在这里。这是伪代码
- Initialize
Boolean Flag = false;
- Search for number in
array
. if found setFlag = True
.- After searching the number in
array
, checkFlag
.- If
Flag = False
, print "the address could not be found"
- 初始化
Boolean Flag = false;
- 在 中搜索号码
array
。如果找到 setFlag = True
。- 在 中搜索号码后
array
,检查Flag
。- 如果
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,则无法找到该地址。