Предполагается, что моя программа позволяет пользователю вводить данные о судимости 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