在 java 中尝试线性搜索,但循环似乎没有迭代
Trying Linear Search in java but it seems loop is not iterating
我正在尝试使用 java 的线性搜索来查找二维数组中的名称并打印与其相关的详细信息,但是代码直接进入最后一个循环而没有搜索
密码是
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Source {
String customerDetails[][]=new String[5][3];
Source()
{
customerDetails[0][0]="1001";
customerDetails[0][1]="Raj";
customerDetails[0][2]="Chennai";
customerDetails[1][0]="1008";
customerDetails[1][1]="Akshay";
customerDetails[1][0]="Pune";
customerDetails[2][0]="1002";
customerDetails[2][1]="Simrath";
customerDetails[2][2]="Amristar";
customerDetails[3][0]="1204";
customerDetails[3][1]="Gaurav";
customerDetails[3][2]="Delhi";
customerDetails[4][0]="1005";
customerDetails[4][1]="Ganesh";
customerDetails[4][2]="Chennai";
}
public static void main(String args[] ) throws Exception {
Source nc = new Source();
Scanner sc = new Scanner(System.in);
String key = sc.nextLine();
boolean found = false;
for(int i=0;i<5;i++){
for(int j=0;j<3;j++){
if(nc.customerDetails[i][j].equals(key)){
found = true;
System.out.println(nc.customerDetails[i][0] + '\n' + nc.customerDetails[i][1] + '\n' + nc.customerDetails[i][2]);
break;
}
}
if(!found){
System.out.println("No Record Found");
break;
}
}
}
}
我想在数组中找到 Gaurav 并打印
1204
高拉夫
德里
public class test
{
String customerDetails[][] = new String[5][3];
test()
{
customerDetails[0][0] = "1001";
customerDetails[0][1] = "Raj";
customerDetails[0][2] = "Chennai";
customerDetails[1][0] = "1008";
customerDetails[1][1] = "Akshay";
customerDetails[1][2] = "Pune";
customerDetails[2][0] = "1002";
customerDetails[2][1] = "Simrath";
customerDetails[2][2] = "Amristar";
customerDetails[3][0] = "1204";
customerDetails[3][1] = "Gaurav";
customerDetails[3][2] = "Delhi";
customerDetails[4][0] = "1005";
customerDetails[4][1] = "Ganesh";
customerDetails[4][2] = "Chennai";
}
public static void main(String args[]) throws Exception
{
test nc = new test();
Scanner sc = new Scanner(System.in);
String key = sc.nextLine();
boolean found = false;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
if (nc.customerDetails[i][j].equals(key))
{
found = true;
System.out.println(nc.customerDetails[i][0] + '\n' + nc.customerDetails[i][1] + '\n' + nc.customerDetails[i][2]);
break;
}
}
}
if (!found)
{
System.out.println("No Record Found");
// break;
}
}
}
这应该有效。 if(!found)
应该在循环外检查所有记录。
我正在尝试使用 java 的线性搜索来查找二维数组中的名称并打印与其相关的详细信息,但是代码直接进入最后一个循环而没有搜索
密码是
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Source {
String customerDetails[][]=new String[5][3];
Source()
{
customerDetails[0][0]="1001";
customerDetails[0][1]="Raj";
customerDetails[0][2]="Chennai";
customerDetails[1][0]="1008";
customerDetails[1][1]="Akshay";
customerDetails[1][0]="Pune";
customerDetails[2][0]="1002";
customerDetails[2][1]="Simrath";
customerDetails[2][2]="Amristar";
customerDetails[3][0]="1204";
customerDetails[3][1]="Gaurav";
customerDetails[3][2]="Delhi";
customerDetails[4][0]="1005";
customerDetails[4][1]="Ganesh";
customerDetails[4][2]="Chennai";
}
public static void main(String args[] ) throws Exception {
Source nc = new Source();
Scanner sc = new Scanner(System.in);
String key = sc.nextLine();
boolean found = false;
for(int i=0;i<5;i++){
for(int j=0;j<3;j++){
if(nc.customerDetails[i][j].equals(key)){
found = true;
System.out.println(nc.customerDetails[i][0] + '\n' + nc.customerDetails[i][1] + '\n' + nc.customerDetails[i][2]);
break;
}
}
if(!found){
System.out.println("No Record Found");
break;
}
}
}
}
我想在数组中找到 Gaurav 并打印 1204 高拉夫 德里
public class test
{
String customerDetails[][] = new String[5][3];
test()
{
customerDetails[0][0] = "1001";
customerDetails[0][1] = "Raj";
customerDetails[0][2] = "Chennai";
customerDetails[1][0] = "1008";
customerDetails[1][1] = "Akshay";
customerDetails[1][2] = "Pune";
customerDetails[2][0] = "1002";
customerDetails[2][1] = "Simrath";
customerDetails[2][2] = "Amristar";
customerDetails[3][0] = "1204";
customerDetails[3][1] = "Gaurav";
customerDetails[3][2] = "Delhi";
customerDetails[4][0] = "1005";
customerDetails[4][1] = "Ganesh";
customerDetails[4][2] = "Chennai";
}
public static void main(String args[]) throws Exception
{
test nc = new test();
Scanner sc = new Scanner(System.in);
String key = sc.nextLine();
boolean found = false;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
if (nc.customerDetails[i][j].equals(key))
{
found = true;
System.out.println(nc.customerDetails[i][0] + '\n' + nc.customerDetails[i][1] + '\n' + nc.customerDetails[i][2]);
break;
}
}
}
if (!found)
{
System.out.println("No Record Found");
// break;
}
}
}
这应该有效。 if(!found)
应该在循环外检查所有记录。