📄 olympics.java
字号:
import java.io.*;
import javagently.*;
import myutilities.*;
class Olympics {
/* Olympic medals program revised J M Bishop Dec 1996
* ---------------------- Java 1.1 October 1997
* updated for Stream and Filer
* May 2000
*
* Reads in and totals medals gained by countries in an
* Olympic Games.
* Uses Filer to open the file.
* Illustrates ending with an exception (the try-for sequence).
*/
Olympics () throws IOException {
System.out.println("**** Olympic medals ****");
System.out.println();
Stream in = new Stream(System.in);
// default the file to the keyboard
Stream fin = new Stream(System.in);
try {
System.out.print("What file for the medals statistics?");
String filename = in.readString();
fin = Filer.open(filename);
}
catch (FileNotFoundException e) {
System.out.println("Five tries up");
System.out.println("Connecting to keyboard by default");
}
String country;
int gold, silver, bronze, total, all = 0;
System.out.println("\nCountry\t\tGold\tSilver\tBronze\tTotal");
try {
for (;;) {
country = fin.readString();
gold = fin.readInt();
silver = fin.readInt();
bronze = fin.readInt();
total = gold + silver + bronze;
System.out.print(country);
if (country.length() < 8) {
System.out.print("\t");
}
System.out.println("\t"+gold+"\t"+silver+"\t"+bronze+
"\t"+total);
all += total;
}
} catch (EOFException e) {
System.out.println(all+" medals won.");
}
}
public static void main (String[] args) throws IOException {
new Olympics ();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -