import javax.swing.*;
public class WinningPercentage
{
public static void main(String[] args)
{
// get input from user
String input = JOptionPane.showInputDialog("Enter record (like 10-2-1)");
// find the position of the first hyphen
int pos1 = input.indexOf('-');
// find the position of the second hyphen
int pos2 = input.indexOf('-', pos1 + 1);
// get wins, then losses, the ties
int wins = Integer.parseInt(input.substring(0, pos1));
int losses;
int ties;
if (pos2 != -1)
{
losses = Integer.parseInt(input.substring(pos1 + 1, pos2));
ties = Integer.parseInt(input.substring(pos2 + 1));
}
else
{
losses = Integer.parseInt(input.substring(pos1 + 1));
ties = 0;
}
// get total games
int games = wins + losses + ties;
// compute winning percentage
double pct = (wins + ties * 0.5) / games;
// display result
JOptionPane.showMessageDialog(null, "Winning pct: " + pct);
}
}
This code can also be downloaded from the file WinningPercentage.java.
a) 1 b) 4 c) 1 d) 9 e) 16 f) 256 g) 25 h) 9 i) 100 j) 100 k) 100 l) 16 m) 100 n) 16