同时比较数组中的字符串和整数
Comparing Strings and Integers in an Array at the same time
我正在做一个练习,要求我读取一个 csv 格式的文件,并询问用户输入他希望程序查找的单词。这是文档的格式,索引 0 和 1 是球队的名称因此字符串和索引 2 和 3 是比赛的分数:
ENCE,Vitality,9,16
ENCE,Vitality,16,12
ENCE,Vitality,9,16
ENCE,Heroic,10,16
SJ,ENCE,0,16
SJ,ENCE,3,16
FURIA,NRG,7,16
FURIA,Prospects,16,1
一开始这个练习要求我写一个程序来读取文档并打印某支球队打了多少场比赛。现在它要我写一个比较分数并打印该特定团队的输赢总数的程序。我试过用一百万种不同的方法来做,有没有一种同时比较字符串和整数的有效方法?
我的代码如下:
import java.nio.file.Paths;
import java.util.Scanner;
public class SportStatistics {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("File:");
String file = scan.nextLine();
System.out.println("Team:");
String team = scan.nextLine();
try ( Scanner reader = new Scanner(Paths.get(file))) {
int totalGames = 0;
int teamPoints = 0;
int otherPoints = 0;
int wins = 0;
int losses = 0;
while (reader.hasNextLine()) {
String info = reader.nextLine();
if (info.isEmpty()) {
continue;
}
String[] parts = info.split(",");
String homeN = parts[0];
String visitorN = parts[1];
int homeP = Integer.valueOf(parts[2]);
int visitorP = Integer.valueOf(parts[3]);
for (String part : parts) {
if (part.equals(team)) {
totalGames++;
}
if(homeN.equals(team)){
teamPoints = homeP;
otherPoints = visitorP;
if(teamPoints > otherPoints){
wins ++;
}else{
losses ++;
}
}
if(visitorN.equals(team)){
teamPoints = visitorP;
otherPoints = homeP;
if(teamPoints > otherPoints){
wins ++;
}else{
losses ++;
}
}
}
}
System.out.println("Games: " + totalGames);
System.out.println(wins);
} catch (Exception e) {
}
}
}
(这种问题使用数据库会更有意义)
所以首先,你想要的是一个游戏class
public class Game {
String team1;
String team2;
int team1Score, team2Score;
public Game(String t1, String t2, int score1, int score2);
}
现在您想制作一个游戏合集(可能是一个列表)
List<Game> games = new ArrayList<>();
那么你要将所有的游戏加入列表
那么你有一个选择。您可以创建一个 Map 并为您的所有团队创建列表,其中包括他们玩过的所有游戏。或者你可以做一个方法,比如
public List<Game> getGamesForTeam(String teamName, List allGames) {
...
}
提取包含团队 teamName
的游戏列表
希望这对您有意义并能帮助您入门
我正在做一个练习,要求我读取一个 csv 格式的文件,并询问用户输入他希望程序查找的单词。这是文档的格式,索引 0 和 1 是球队的名称因此字符串和索引 2 和 3 是比赛的分数:
ENCE,Vitality,9,16
ENCE,Vitality,16,12
ENCE,Vitality,9,16
ENCE,Heroic,10,16
SJ,ENCE,0,16
SJ,ENCE,3,16
FURIA,NRG,7,16
FURIA,Prospects,16,1
一开始这个练习要求我写一个程序来读取文档并打印某支球队打了多少场比赛。现在它要我写一个比较分数并打印该特定团队的输赢总数的程序。我试过用一百万种不同的方法来做,有没有一种同时比较字符串和整数的有效方法?
我的代码如下:
import java.nio.file.Paths;
import java.util.Scanner;
public class SportStatistics {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("File:");
String file = scan.nextLine();
System.out.println("Team:");
String team = scan.nextLine();
try ( Scanner reader = new Scanner(Paths.get(file))) {
int totalGames = 0;
int teamPoints = 0;
int otherPoints = 0;
int wins = 0;
int losses = 0;
while (reader.hasNextLine()) {
String info = reader.nextLine();
if (info.isEmpty()) {
continue;
}
String[] parts = info.split(",");
String homeN = parts[0];
String visitorN = parts[1];
int homeP = Integer.valueOf(parts[2]);
int visitorP = Integer.valueOf(parts[3]);
for (String part : parts) {
if (part.equals(team)) {
totalGames++;
}
if(homeN.equals(team)){
teamPoints = homeP;
otherPoints = visitorP;
if(teamPoints > otherPoints){
wins ++;
}else{
losses ++;
}
}
if(visitorN.equals(team)){
teamPoints = visitorP;
otherPoints = homeP;
if(teamPoints > otherPoints){
wins ++;
}else{
losses ++;
}
}
}
}
System.out.println("Games: " + totalGames);
System.out.println(wins);
} catch (Exception e) {
}
}
}
(这种问题使用数据库会更有意义)
所以首先,你想要的是一个游戏class
public class Game {
String team1;
String team2;
int team1Score, team2Score;
public Game(String t1, String t2, int score1, int score2);
}
现在您想制作一个游戏合集(可能是一个列表)
List<Game> games = new ArrayList<>();
那么你要将所有的游戏加入列表
那么你有一个选择。您可以创建一个 Map
public List<Game> getGamesForTeam(String teamName, List allGames) {
...
}
提取包含团队 teamName
的游戏列表希望这对您有意义并能帮助您入门