英文:
Comparing Strings and Integers in an Array at the same time
问题
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) {
}
}
}
英文:
I am doing an exercise that asks me to read a file in csv format, and ask user`s input about the word he wants the program to look for. This is the format of the document, index 0 and 1 are name of teams therefore Strings and index 2 and 3 are the scores of the games:
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
At first the exercise asked me to write a program that reads the document and print how many games certain team played. Now it wants me to write one that will compare scores and print the total number of wins and losses of that specific team. I tried to do it in a million different ways, is there an effective way to compare Strings and integers at the same time ?
My code below:
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) {
}
}
}
答案1
得分: 0
以下是翻译好的内容:
(这种情况下,使用数据库会更有意义)
首先,您想要一个游戏类
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<String, List<Game>>
,为所有的队伍创建列表,其中包括他们参与的所有比赛。或者您可以创建一个方法,例如:
public List<Game> getGamesForTeam(String teamName, List allGames) {
...
}
该方法提取包含队伍 teamName 的所有比赛的游戏列表。
希望这对您有意义,并能帮助您入门。
英文:
(This is the kind of problem where using a database would make more sense)
So first, what you want is a Game class
public class Game {
String team1;
String team2;
int team1Score, team2Score;
public Game(String t1, String t2, int score1, int score2);
}
Now you want to make a collection (probably a list) of games
List<Game> games = new ArrayList<>();
Then you want to add all the games to the list
Then you have a choice. You could create a Map<String, List<Game>> and create lists for all your teams that include all the games they played. Or you could make a method, like
public List<Game> getGamesForTeam(String teamName, List allGames) {
...
}
That extracts a list of games that included team teamName
Hopefully this makes sense to you and will get you started
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论