任何可以帮助我处理 java.util.Scanner 类的人吗?
我无法弄清楚如何使用分隔符方法。
输入:teamA-teamB 4-5
输出:团队A:团队B:4:-5
预期输出:teamA:teamB:4:5
我的一段代码:
public void readResult()
{
String team1, team2;
int goals1, goals2;
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\\s*-\\s*");
if (scanner.hasNext())
{
team1 = scanner.next();
scanner.useDelimiter("\\d*-\\d*");
if(scanner.hasNext())
{
team2 = scanner.next().trim();
scanner.useDelimiter("-");
if(scanner.hasNext())
{
goals1 = scanner.nextInt();
scanner.useDelimiter("\\n");
if(scanner.hasNext())
{
goals2 = scanner.nextInt();
System.out.println(team1 + ":" + team2 + ":" + goals1 + ":" + goals2);
scanner.close();
return;
}
}
}
}
scanner.close();
System.out.println("bad format");
}