This code works as is, but I need to break it up into two classes. I have tried
ID: 3558491 • Letter: T
Question
This code works as is, but I need to break it up into two classes. I have tried a couple times but can't get it to work.
import javax.swing.JOptionPane;?
public class LotteryTicket {
public static void main(String[] args) {
int g1 =0; //user's input
int g2 =0;
int g3 =0;
int num1; //random numbers
int num2;
int num3;
int order1 =0; //check order
int matchCount =0;
final double>
final double TWO_MATCHING = 100;
final double THREE_NO_ORDER = 1000;
final double THREE_EXACT = 1000000;
//get the input data from the user
g1 = Integer.parseInt(JOptionPane.showInputDialog("Enter Your First Number"));
g2 = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Second Number"));
g3 = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Third Number"));
num1= ((int)(Math.random() * 100) % 9 + 0); // randomly picks a number from 0 to 9
num2= ((int)(Math.random() * 100) % 9 + 0); // randomly picks a number from 0 to 9
num3= ((int)(Math.random() * 100) % 9 + 0); // randomly picks a number from 0 to 9
JOptionPane.showMessageDialog(null, "Your 3 Numbers are: " + g1+ "," + g2 + "," + g3 + " "
+ "The Winning Lottery Numbers Are: " + num1 + "," + num2 + "," + num3);
if (g1 == num1 || g1 == num2 || g1 == num3 || g2 == num2 || g2 == num3 || g3 == num3)
{
matchCount = matchCount + 1;
}
if (g2 == num1 || g2 == num3 && num1!=num3 && num2!=num1 && num2!=num3)
{
matchCount = matchCount + 1;
}
if (g3 == num1|| g3 == num2 && num2!=num1 && num3!=num1 && num3!=num2)
{
matchCount = matchCount + 1;
}
?
if (matchCount==1){
JOptionPane.showMessageDialog(null, "You had one matching number and won: $" + String.format("%.2f",ONE_MATCHING));}
else if (matchCount==2) {
JOptionPane.showMessageDialog(null, "You had two matching numbers and won: $" +String.format("%.2f", TWO_MATCHING));}
else if ((matchCount == 3) && ((g1==num1 && g2==num2 && g3==num3))){
JOptionPane.showMessageDialog(null, "You had three matching numbers in exact order and won: $" + String.format("%.2f",THREE_EXACT));}
else if (matchCount==3){
JOptionPane.showMessageDialog(null, "You had three matching numbers and won: $" + String.format("%.2f",THREE_NO_ORDER));}
else {
JOptionPane.showMessageDialog(null, "Sorry, you had no matches ");
}
}
}
Explanation / Answer
import javax.swing.JOptionPane;
class random_number
{
static int foo()
{
return ((int)(Math.random() * 100) % 9 + 0);
}
}
public class LotteryTicket extends random_number{
public static void main(String[] args) {
int g1 =0; //user's input
int g2 =0;
int g3 =0;
int num1; //random numbers
int num2;
int num3;
int order1 =0; //check order
int matchCount =0;
final double>
final double TWO_MATCHING = 100;
final double THREE_NO_ORDER = 1000;
final double THREE_EXACT = 1000000;
//get the input data from the user
g1 = Integer.parseInt(JOptionPane.showInputDialog("Enter Your First Number"));
g2 = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Second Number"));
g3 = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Third Number"));
num1= foo();// randomly picks a number from 0 to 9
num2= foo(); // randomly picks a number from 0 to 9
num3= foo(); // randomly picks a number from 0 to 9
JOptionPane.showMessageDialog(null, "Your 3 Numbers are: " + g1+ "," + g2 + "," + g3 + " "
+ "The Winning Lottery Numbers Are: " + num1 + "," + num2 + "," + num3);
if (g1 == num1 || g1 == num2 || g1 == num3 || g2 == num2 || g2 == num3 || g3 == num3)
{
matchCount = matchCount + 1;
}
if (g2 == num1 || g2 == num3 && num1!=num3 && num2!=num1 && num2!=num3)
{
matchCount = matchCount + 1;
}
if (g3 == num1|| g3 == num2 && num2!=num1 && num3!=num1 && num3!=num2)
{
matchCount = matchCount + 1;
}
if (matchCount==1){
JOptionPane.showMessageDialog(null, "You had one matching number and won: $" + String.format("%.2f",ONE_MATCHING));}
else if (matchCount==2) {
JOptionPane.showMessageDialog(null, "You had two matching numbers and won: $" +String.format("%.2f", TWO_MATCHING));}
else if ((matchCount == 3) && ((g1==num1 && g2==num2 && g3==num3))){
JOptionPane.showMessageDialog(null, "You had three matching numbers in exact order and won: $" + String.format("%.2f",THREE_EXACT));}
else if (matchCount==3){
JOptionPane.showMessageDialog(null, "You had three matching numbers and won: $" + String.format("%.2f",THREE_NO_ORDER));}
else {
JOptionPane.showMessageDialog(null, "Sorry, you had no matches ");
}
}
}