通过输入与数据关联的名称来查找数据

时间:2014-12-08 23:44:28

标签: java arrays sorting

我在这里为跑步者开发一个简单的统计程序。它仅获取一个种族的数据。在stackoverflow的帮助下,我有第一部分的代码运行! 我们的想法是,在比赛结束后,我们可以输入跑步者的名字和他们的时间。我希望通过搜索他们的名字来搜索一个人的运行时间(然后你会看到跑步者的名字和他们的时间显示在屏幕上。是否可以用Java做到这一点??谢谢Cassy。

这是我的代码......

import java.util.Scanner;


public class Trying
{

public static void main (String[] args)

    {
    int num;

    Scanner input= new Scanner (System.in);

    System.out.println("******************************************************************* ");
    System.out.println("Welcome to Running Race Time Statistical Analysis Application");
    System.out.println("******************************************************************* \n");


    System.out.println("Please input number of participants (2 to 10)");

    num=input.nextInt();

    // If the user enters an invalid number... display error message...
    while(num<2|| num >10)
    {
        System.out.println("Error invalid input! Try again! \nPlease input a valid number of participants (2-10)...");

        num=input.nextInt();
    }

    // declare arrays
    double resultArray [] = new double [num]; // create result array with new operator
    String nameArray [] = new String [num];// create name array with new operator
    // Using the num int will ensure that the array holds the number of elements inputed by user


    // loop to take in user input for both arrays (name and result)
    double count = 0;
    for (int i = 0 ; i < nameArray.length ; i++)
        {
            System.out.println ("Please enter a race participant Name for runner " + (i+1) );
            nameArray[i] = input.next();

            System.out.println ("Please enter a race result (time between 0.00 and 10.00) for runner " + (i+1) );
            resultArray[i] = input.nextDouble();

            count += resultArray[i]; // This count variable is used later to calculate average from total
        }
}
}

2 个答案:

答案 0 :(得分:1)

如果您只是希望用户能够搜索名称,请使用以下命令:

String entered;
while(true) {
    entered = input.nextLine();
    if(entered.equalsIgnoreCase("exit")) break;
    for(int i = 0; i < nameArray.length; i++) {
        if(entered.equals(nameArray[i])) {
            System.out.println("Name: " + nameArray[i] + "\tTime: " + resultArray[i]);
        }
    }
}

答案 1 :(得分:0)

实现查找的最简单方法是使用HashMap或其他类型的Map。它也比一对阵列更有效。诀窍是将所有名称和时间存储在HashMap中,代码类似于此。

Map<String, Double> raceTimes = new HashMap<>();

for (int i = 1; i <= participants; i++) {
    // ... some code here to enter the name and the time

    raceTimes.put(name, time);
}

如果您有姓名,那么从HashMap中检索时间就很容易了。代码看起来像这样。

Double time = raceTimes.get(nameEntered);
if (time != null) {
    System.out.println(nameEntered + " has a time of " + time);
} 

如果用户输入的名称与其中一个参与者不对应,则进行空检查。实际查找只是一个表达式raceTimes.get(nameEntered) - 这比使用两个数组更简单,并循环查找所需的值。

我冒昧地将所有这些结合在一起,为您提供完整的解决方案。你可以看到我还改变了其他一些事情。

  • 我已经将其分解为单独的方法,因为main变得难以管理。
  • 我已更改参与者姓名的提示,使用nextLine,因为next只会读取一个单词。这将使参与者的名字由几个单词组成。但是,在进行此更改后,我必须在调用nextLinenextInt后向nextDouble添加额外的电话,因为对nextIntnextDouble的调用不会#39;在数字后面读取换行符 - 所以你需要一个额外的nextLine来读取换行符。

我的整个代码如下。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class RaceTimes{

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        System.out.println("******************************************************************* ");
        System.out.println("Welcome to Running Race Time Statistical Analysis Application");
        System.out.println("******************************************************************* \n");

        int participants = enterNumberOfParticipants(input);
        Map<String, Double> raceTimes = enterTimes(input, participants);
        lookupTimes(input, raceTimes);
        input.close();
    }

    private static int enterNumberOfParticipants(Scanner input) {
        System.out.println("Please input number of participants (2 to 10)");
        int num = input.nextInt();

        while (num < 2 || num > 10) {
            System.out.println("Error invalid input! Try again! \nPlease input a valid number of participants (2-10)...");
            num = input.nextInt();
        }
        input.nextLine();
        return num;
    }

    private static Map<String, Double> enterTimes(Scanner input, int participants) {
        Map<String, Double> raceTimes = new HashMap<>();

        // loop to take in user input for both arrays (name and result)
        double totalTime = 0;
        for (int i = 1; i <= participants; i++) {
            System.out.println("Please enter a race participant Name for runner " + i);
            String name = input.nextLine();

            System.out.println("Please enter a race result (time between 0.00 and 10.00) for runner " + i);
            Double time = input.nextDouble();
            input.nextLine();
            totalTime += time;

            raceTimes.put(name, time);
        }
        System.out.println("Average time is " + totalTime / participants);
        return raceTimes;
    }

    private static void lookupTimes(Scanner input, Map<String, Double> raceTimes) {
        while(true) {
            System.out.println("Please enter a name that you'd like to know the time for, or type quit.");
            String nameEntered = input.nextLine();
            if(nameEntered.equalsIgnoreCase("quit")) {
                return;
            }
            Double time = raceTimes.get(nameEntered);
            if (time != null) {
                System.out.println(nameEntered + " has a time of " + time);
            } else {
                System.out.println("There is no participant called " + nameEntered);
            }
        }
    }
}