Rewrite using the Scanner class import javax.swing.JOptionPane; /** */ public cl
ID: 3766795 • Letter: R
Question
Rewrite using the Scanner class
import javax.swing.JOptionPane;
/**
*/
public class SoccerTeams
{
public static void main(String[] args)
{
final int MIN_PLAYERS = 9;
final int MAX_PLAYERS = 15;
int players;
int teamSize;
int teams;
int leftOver;
String input;
input = JOptionPane.showInputDialog("Enter the number of " + "players per team.");
teamSize = Integer.parseInt(input);
while (teamSize < MIN_PLAYERS || teamSize > MAX_PLAYERS)
{
input = JOptionPane.showInputDialog("The number must" + MIN_PLAYERS + " and no more than " + MAX_PLAYERS + ". Enter " + "the number of players.");
teamSize = Integer.parseInt(input);
}
input = JOptionPane.showInputDialog("Enter the available " + "number of players.");
players = Integer.parseInt(input);
while (players < 0)
{
input = JOptionPane.showInputDialog("Enter 0 or " + "greater.");
players = Integer.parseInt(input);
}
teams = players / teamSize;
leftover = players % teamSize;
JOptionPane.showMessageDialog(null, "There will be " + teams + " teams with " + leftOver + " players left over.");
System.exit(0);
}
}
Explanation / Answer
import javax.swing.JOptionPane;
import java.util.*;
/**
*/
public class SoccerTeams
{
public static void main(String[] args)
{
final int MIN_PLAYERS = 9;
final int MAX_PLAYERS = 15;
int players;
int teamSize;
int teams;
int leftOver;
String input = "";
//input = JOptionPane.showInputDialog("Enter the number of " + "players per team.");
System.out.println("Enter the number of " + "players per team.");
Scanner in = new Scanner(System.in);
teamSize = in.nextInt();
//teamSize = Integer.parseInt(input);
while (teamSize < MIN_PLAYERS || teamSize > MAX_PLAYERS)
{
System.out.println("The number must" + MIN_PLAYERS + " and no more than " + MAX_PLAYERS + ". Enter " + "the number of players.");
teamSize = in.nextInt();
}
System.out.println("Enter the available " + "number of players.");
players = in.nextInt();
while (players < 0)
{
System.out.println("Enter 0 or " + "greater.");
players = in.nextInt();
}
teams = players / teamSize;
leftover = players % teamSize;
JOptionPane.showMessageDialog(null, "There will be " + teams + " teams with " + leftOver + " players left over.");
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.