gpt4 book ai didi

java - 搜索数组的问题

转载 作者:行者123 更新时间:2023-11-29 04:18:48 25 4
gpt4 key购买 nike

我的程序假设允许用户输入 8 个罪犯的犯罪记录;他们的姓名、罪行和定罪日期。输入后,它会询问用户他们正在搜索的特定犯罪类型,并打印出所有被判有罪的罪犯的完整统计数据,这些罪犯属于警官要求的类别(只有三种犯罪:盗窃、纵火和攻击)。

我的问题是,当我要求查找其中一项犯罪时,程序只显示 1 项犯罪记录,而它们是具有相同犯罪的其他几项记录。

这是我的代码:

//Include Libraries
import java.io.*;
import java.util.Scanner;

//Class
public class Police {

//Declare variables, Arrays, and Strings for future use in the program
static String name[] = new String[8]; //Name Array that can hold 8 names
static String crime[] = new String[8]; //Position Array that can hold 8 crimes
static int year[] = new int[8]; //Age Array that can hold 8 years
static int x, i; //x and i are counters

//Main Method
public static void main(String[] args) throws IOException {

//Open BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

/*
* The program will now loop the menu
* options shown below.
* It will loop until the user exits
* the program.
*/

//variable for menu
int menuChoice = 1;
//Display introduction
System.out.print("Hello and Welcome to Police Java Database\r");

while (menuChoice != 3) {
//Display menu choices
System.out.print("Enter Menu Choice\n");
System.out.print("**********************");
System.out.print("\r(1) => Enter Criminal Data \n");
System.out.print("(2) => Display Matching Crime \n");
System.out.print("(3) => Exit Program \r");

//try-catch statement to read input
try {

menuChoice = Integer.parseInt(br.readLine());
} catch (IOException ie) {
ie.printStackTrace();
}
//switch statement to loop the menu choices
switch(menuChoice) {
case 1:
//Case 1 is the store input method
inputInfo();
break;
case 2:
//Case 2 is the sort input method
inputSearch();
break;
case 3:
//exit program for case 3
return;
}

}

/*
* The program will now begin to store
* each input information from the user
* about each criminal in Arrays.
*/

}
//Method to store input info
public static void inputInfo() throws IOException {

//Open BufferedReader
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

//Open Scanner
Scanner p = new Scanner(System.in);

//loop to request to fill Arrays
for (x = 0; x < 8; x++) {

//Ask for criminal name input
System.out.print("Enter criminal name: ");
//Read input and store name in an Array
name[x] = in.readLine();

//Ask for crime input
System.out.print("Enter crime offence: ");
//Read input and store crime in an Array
crime[x] = in.readLine();

//Ask for year the crime was committed
System.out.print("Enter year of conviction: \r");
//Read input and store year in an Array
year[x] = p.nextInt();


}

/*
* The program will now begin to search
* the stored information for matching crime.
*/

}


//Method to select and find info
public static void inputSearch() throws IOException {

//Open BufferedReader
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
//declare variables that'll be used to search crime input
int flag;
boolean found;
String searchcrime;
flag = 0;
found = false;

//Ask what crime the user is searching for
System.out.print("What is the crime you're searching for: ");
//Read the input
searchcrime =sc.readLine();
//Display which crime they selected
System.out.print("You searched for criminals with the offence of: " + searchcrime + "\r");

//Compare all input for asked crime
for (x = 0; x < 8; x++) {

if (searchcrime.compareTo(crime[x])==0) {

flag = x;
found = true;
}
}


//If the input is not found, show error
if (found == false) {

System.out.print("Error! Crime not found");
}
else {

//Display each category of records; names, crime, year
System.out.println(" Name " + " -----" + " Crime " + "-----" + " Year ");
//Display matching crime with criminal name and year of conviction.
System.out.println( name[flag] + " --- " + crime[flag] + " --- " + year[flag]);

}


}

这是输入样本:

Bugs Malone, Theft, 1981
Slippery Sal, Theft, 1991
Al Copone, Assault, 1947
Eddie Striker, Arson, 1969
Tom Tuner, Arson, 2005
Gared Gipson,Assault, 1998
Jary Lucky, Arson, 2013
Lizzy Patty, Theft, 2009

这是输出示例:

What is the crime you're searching for: Arson
You searched for criminals with the offence of:Arson
Name ----- Crime ----- Year
Jary Lucky --- Arson --- 2013

最佳答案

您只保留找到的最后一个匹配项的索引。您应该在循环内打印匹配项:

            for (x = 0; x < 8; x++) {
if (searchcrime.compareTo(crime[x])==0) {
if (!found) { // print title before first match
System.out.println(" Name " + " -----" + " Crime " + "-----" + " Year ");
}
System.out.println( name[x] + " --- " + crime[x] + " --- " + year[x]);
found = true;
}
}
if (!found) {
System.out.print("Error! Crime not found");
}

关于java - 搜索数组的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50549429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com