程序应从文本文件中读取匹配数据。文件中的每一行都包含特定匹配项的数据。文件中每一行的格式如下:
home_team_name : away_team_name : home_team_score : away_team_score
请注意如何使用冒号(:)分隔输入的不同部分(字段定界符)。
以下是文件中典型数据行的示例。
Arsenal : Spurs : 2 : 1
Everton : Liverpool : 1 : 1
Huddersfield : Chelsea: 2 : 1
程序应提示用户输入匹配数据文件的名称,然后应读取,存储和处理存储在文件中的匹配数据的每一行,并将数据以指定格式(即,这不仅是读取和显示存储在文件中的原始数据)。
比赛数据必须以以下格式显示。
Home team Score Away team Score
========= ===== ========= ======
Arsenal 2 Spurs 1
Everton 1 Liverpool 1
Huddersfield 2 Chelsea 1
我已经完成了代码,但是直到问题出现为止,输出才如问题中所示:
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
public class FootallResultsGenerator {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
String[] splitarray=new String[4];
String filename;
String line;
String hometeam;
String homescore;
String awayteam;
String awayscore;
Scanner obj=new Scanner(System.in);
System.out.println("Enter the name of the file");
filename=obj.nextLine();
Scanner filereader=null;
try {
File fileobject=new File(filename);
filereader=new Scanner(fileobject);
System.out.println("Home team\t"+"Score\t"+"Away team\t"+"Score");
System.out.println("==========\t"+"======\t"+"=========\t"+"======");
while(filereader.hasNext())
{
line=filereader.nextLine();
splitarray=line.split(":");
if(splitarray.length==4) {
hometeam=splitarray[0];
homescore=splitarray[2];
awayteam=splitarray[1];
awayscore=splitarray[3];
System.out.println(hometeam+"\t"+homescore+"\t"+awayteam+"\t"+awayscore);
}
}
}
catch(FileNotFoundException e)
{
System.out.println("Error:File Not Found.");
}
}
}
它应该显示:
Home team Score Away team Score
========= ===== ========= ======
Manchester United 2 Spurs 1
Everton 1 Liverpool 1
Huddersfield 2 Chelsea 1
相反显示此内容:
Home team Score Away team Score
========= ===== ========= ======
Manchester United 2 Spurs 1
Everton 1 Everton 1
Huddersfield 2 Chelsea 1
答案 0 :(得分:0)
您可以使用以下格式:
System.out.printf(" %-13s %-10s%-15s%-5s","Home team","Score","Away team","Score\n");
System.out.println("=============== ===== ========= =====");
System.out.printf("%-17s %-2s%15s %9s",hometeam,homescore,awayteam,awayscore);
输出:
Home team Score Away team Score
========= ===== ========= ======
Manchester United 2 Spurs 1
请告诉我您是否正在寻找它,如果不是,我在这里根据您的要求进行编辑。