简体   繁体   English

使用数组存储来自用户输入的多个变量

[英]Using an array to store multiple variables from user input

I am relatively new to Java and would like to know how to store variables separately from a single line of user input. 我对Java还是比较陌生,想知道如何与单行用户输入分开存储变量。

At the minute the user is prompted to enter football results in the following format 在此刻提示用户输入以下格式的足球结果

home_name : away_name : home_score : away_score

and I am using a while loop to continue to ask user for input until they enter "stop" 而且我正在使用while循环继续询问用户输入内容,直到他们输入“停止”为止

(while (input != "stop))

Once the loop is broken I would like my program to output a variety of data such as total games played, but I'm struggling to store the home_name , away_name etc.. especially if the user wishes to enter multiple lines of results. 一旦循环中断,我希望程序输出各种数据,例如玩的总游戏数,但是我正努力存储home_nameaway_name等。尤其是如果用户希望输入多行结果。

Two mainstream ways to store a "record" are: 存储“记录”的两种主流方式是:

  • Maps 地图
  • Data objects 数据对象

A map is more generic: 映射更为通用:

Map<String,String> match = new HashMap<>();
match.put("home_name", "Alvechurch Villa");
match.put("away_name", "Leamington");
match.put("home_score", "0");
match.put("away_score", "6");

You can add a map to a list: 您可以将地图添加到列表中:

List<Map<String,String>> matches = new ArrayList<>();
matches.add(list);

... and retrieve them: ...并检索它们:

Map<String,String> match = matches.get(0);
System.out.println(match.get("away_score"));

A data object is more tuned to your data format, but you have to write the class yourself. 数据对象更适合您的数据格式,但是您必须自己编写类。

public class Match {
    public String homeName;
    public String awayName;
    public int homeScore;
    public int awayScore;
}

Now you can use this class: 现在您可以使用此类:

 Match match = new Match();
 match.homeName = "Studley";
 // etc.

You can add and retrieve these from lists too: 您也可以从列表中添加和检索这些:

 List<Match> matches = new ArrayList<>();
 matches.add(match);
 Match aMatch = matches.get(0);

This is simple, but it's considered bad practice to have public fields like this - it's better to get at them via methods. 这很简单,但是拥有这样的公共领域被认为是不好的做法-最好通过方法来了解它们。 For brevity, here's a data class with only one field: 为简便起见,这是一个仅包含一个字段的数据类:

 public class Player {
     private String name;
     public Player(String name) {
         this.name = name;
     }

     public String name() {
         return name;
     }
 }

 Player neilStacey = new Player("Neil Stacey");

You can use the same technique with all the fields in Match . 您可以对Match所有字段使用相同的技术。

(A common style is to name a method like this getName() , and also to have a setName() . I have used a different style and made the object immutable , in an effort to set a good example!) (一种常见的样式是为诸如getName()这样的方法命名,并具有一个setName() 。为了使用一个好的例子,我使用了一种不同的样式并使该对象变得不可变 !)


One advantage of the data object is that it has different types for different fields: homeName is a String, homeScore is an integer. 数据对象的一个​​优点是,对于不同的字段,它具有不同的类型: homeName是一个String, homeScore是一个整数。 All the fields in the Map are Strings. 映射中的所有字段都是字符串。 You can get around this by using Map<String,Object> but then as a consumer you have to cast to the right type when you read. 您可以使用Map<String,Object>来解决此问题,但是作为消费者,您在阅读时必须转换为正确的类型。

 String homeName = (String) match.get("home_name");

Data objects allow the compiler to do a lot of compile-time checking that helps you know your code is correct. 数据对象使编译器可以进行很多编译时检查,以帮助您了解代码是否正确。 If you use a map, you won't find out until runtime. 如果使用地图,则只有在运行时才能找到。

Prompt the user separately for each input. 为每个输入分别提示用户。

System.out.println("home_name: ");
String hN = scan.next();
System.out.println("away_name: ");
String aN = scan.next();
System.out.println("home_score: ");
String hS = scan.next();
System.out.println("away_score: ");
String aS = scan.next();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM