【问题标题】:Using an array to store multiple variables from user input使用数组存储来自用户输入的多个变量
【发布时间】:2016-12-18 18:03:04
【问题描述】:

我对 Java 比较陌生,想知道如何将变量与单行用户输入分开存储。

在提示用户输入以下格式的足球结果的那一刻

home_name : away_name : home_score : away_score

我正在使用 while 循环继续询问用户输入,直到他们输入“停止”

(while (input != "stop))

一旦循环中断,我希望我的程序输出各种数据,例如总游戏数,但我很难存储home_nameaway_name 等。特别是如果用户希望输入多行结果。

【问题讨论】:

  • (假设用户希望输入100行匹配结果)
  • 请发布您拥有的任何代码,并指出您遇到的具体问题。它会帮助我们帮助
  • 标记为 Java,因为问题是关于 java.......
  • boolean stop = true; while (stop) { System.out.println("Please enter results in the following format" + " home_name : away_name : home_score : away_score" + ", or enter stop to quit"); String response = scan.nextLine(); if (response.equals("stop")) { stop = false; } }
  • 暂时差不多,我想知道如何将每行输入拆分成单独的部分分别存储

标签: java arrays while-loop user-input


【解决方案1】:

存储“记录”的两种主流方式是:

  • 地图
  • 数据对象

地图更通用:

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");

您可以将地图添加到列表中:

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

...并检索它们:

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

数据对象更适合您的数据格式,但您必须自己编写类。

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

现在你可以使用这个类了:

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

您也可以从列表中添加和检索这些:

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

这很简单,但是拥有这样的公共字段被认为是不好的做法 - 最好通过方法来获取它们。为简洁起见,这是一个只有一个字段的数据类:

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

     public String name() {
         return name;
     }
 }

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

您可以对Match 中的所有字段使用相同的技术。

(一种常见的风格是命名一个像getName()这样的方法,并且还有一个setName()。我使用了不同的风格并使对象不可变,努力树立一个好榜样!)


数据对象的一个​​优点是它对不同的字段有不同的类型:homeName 是一个字符串,homeScore 是一个整数。 Map 中的所有字段都是字符串。您可以通过使用 Map&lt;String,Object&gt; 来解决此问题,但作为消费者,您必须在阅读时转换为正确的类型。

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

数据对象允许编译器进行大量编译时检查,帮助您了解您的代码是否正确。如果你使用地图,直到运行时才会发现。

【讨论】:

  • 我明白了一点,但是当输入以下格式 = home_name : away_name : home_score : away_score.我不想为每个输入单独提示用户。
  • 例如,我当前的基本结构是这样的... boolean stop = true; while (stop) { System.out.println("请按以下格式输入结果" + " home_team_name : away_team_name : home_team_score : away_team_score" + ", 或者输入 stop 退出");字符串响应 = scan.nextLine(); if (response.equals("stop")) { stop = false; } }
  • 看起来您问了一个关于存储变量的问题,而实际上您想知道如何拆分字符串。谷歌“Java 拆分字符串”。
【解决方案2】:

针对每个输入分别提示用户。

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();

【讨论】:

  • 我希望用户只输入以下格式 =home_name : away_name : home_score : away_score,而不是为每个变量单独输入。
猜你喜欢
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多