Question

In: Computer Science

Java code TIA I need this: Your program makes accommodations for repeating digits. For example if...

Java code

TIA

I need this: Your program makes accommodations for repeating digits. For example if the random numbers generated were 141 in that order. Then the user entered 271 in that order, be sure that the last one does not count a match to the first and third numbers of the random numbers. They only matched one number in this case. Now if the random numbers are 141 in that order and the user enters 113, then they did match 2 numbers. If they enter 114 then they match 3 numbers but not in order.

public class lotteryGame {
//declaring varibales
static int a,b,c,n1,n2,n3,matchCount=0;
static Scanner input = new Scanner (System.in);
public static void matchNumbers(int a,int b,int c) {
Random random=new Random();
//generating three random numbers (between 0 to 10) and storing it in n1,n2 and n3
n1= random.nextInt(10);
n2= random.nextInt(10);
n3= random.nextInt(10);
//comapring three random numbers with user entered numbers
if(n1==a)
matchCount++;
if(n2==b)
matchCount++;
if(n3==c)
matchCount++;
//checking how many numbers matched
if(matchCount==1){
System.out.println("\nYou guessed 1 number correctly");
System.out.println("\n***Congratulations you have won $10000***");
}
else if(matchCount==2){
System.out.println("\nGOOD!!,You guessed 2 numbers correctly");
System.out.println("\nCongratulations you have won $100000");
}

else if(matchCount==3){
System.out.println("\nAmazing!!,You guessed all 3 numbers correctly");
System.out.println("\n***Congratulations you have won $1000000***");
}
else
{
System.out.println("\nYou guessed all 3 numbers in-correctly");
System.out.println("-1");
}
}   
public static void main(String args[])
{
//printing welcome message and also the amount for correct guesses
System.out.println("********WELCOME TO LOTTERY GAME********");
System.out.println("GAME PRICES: ");
System.out.println("FOR ALL THREE CORRECT MATCHES: $1000000");
System.out.println("FOR TWO CORRECT MATCHES: $100000");
System.out.println("FOR ONLY ONE CORRECT MATCHES: $10000");
System.out.println("FOR ALL THREE IN-CORRECT MATCHES: it will show -1");
//reading three numbers from users
System.out.println("\nEnter three numbers(0<=numbers<10): ");
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
//calling method matchNumbers() by passing three parameters
matchNumbers(a,b,c);

}
  
}

Solutions

Expert Solution

//Changes made according to the requirement.


import java.util.*;
public class lotteryGame {
//declaring varibales
static int a,b,c,n1,n2,n3,matchCount=0;
static Scanner input = new Scanner (System.in);
public static void matchNumbers(int a,int b,int c) {
Random random=new Random();
//generating three random numbers (between 0 to 10) and storing it in n1,n2 and n3
n1= random.nextInt(10);
n2= random.nextInt(10);
n3= random.nextInt(10);
//comapring three random numbers with user entered numbers

//Modified
if(n1==a)
{
matchCount++;
n1=99;   //changed it to a number above 10 so that it do not match again and again.
}
else if(n1==b)
{
 matchCount++;
n1=99;   
}
else if(n1==c)
{
matchCount++;
n1=99;
}
if(n2==a)
{
matchCount++;
n2=99;
}
else if(n2==b)
{
 matchCount++;
n2=99;   
}
else if(n2==c)
{
matchCount++;
n2=99;
}
if(n3==a)
{
matchCount++;
n3=99;
}
else if(n3==b)
{
 matchCount++;
n3=99;   
}
else if(n3==c)
{
matchCount++;
n3=99;
}
//checking how many numbers matched
if(matchCount==1){
System.out.println("\nYou guessed 1 number correctly");
System.out.println("\n***Congratulations you have won $10000***");
}
else if(matchCount==2){
System.out.println("\nGOOD!!,You guessed 2 numbers correctly");
System.out.println("\nCongratulations you have won $100000");
}

else if(matchCount==3){
System.out.println("\nAmazing!!,You guessed all 3 numbers correctly");
System.out.println("\n***Congratulations you have won $1000000***");
}
else
{
System.out.println("\nYou guessed all 3 numbers in-correctly");
System.out.println("-1");
}
}   
public static void main(String args[])
{
//printing welcome message and also the amount for correct guesses
System.out.println("********WELCOME TO LOTTERY GAME********");
System.out.println("GAME PRICES: ");
System.out.println("FOR ALL THREE CORRECT MATCHES: $1000000");
System.out.println("FOR TWO CORRECT MATCHES: $100000");
System.out.println("FOR ONLY ONE CORRECT MATCHES: $10000");
System.out.println("FOR ALL THREE IN-CORRECT MATCHES: it will show -1");
//reading three numbers from users
System.out.println("\nEnter three numbers(0<=numbers<10): ");
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
//calling method matchNumbers() by passing three parameters
matchNumbers(a,b,c);

}
  
}

Related Solutions

I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
I need the Java Code and Flowchart for the following program: Suppose you are given a...
I need the Java Code and Flowchart for the following program: Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which cell was flipped. Your program should prompt the user to enter a 6-by-6 array with 0s and 1s and find the first...
I need original java code that completes this program and gets it to print out results...
I need original java code that completes this program and gets it to print out results just like in the example. Also please upload answer in a word document format only. Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times. Each time the search is conducted the number of checks (IE...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT