lottery program without arrays with java - java

hello guys i am really stuck, its about a lottery program that randomly generate a four-digit number and prompt the user to enter a four-digit number without using array and following this rules
-if the user input match the lottery number in exact order the award is $10000
-if the user input match the four-digit in different order the award is $5000
-if the user input match the three-digit number in different order the award is $2000
-if any 1 or 2-digit in the user input match the lottery the award is $500
i did it for a 3-digit number but i don't know how to do it for a four-digit number without arrays.this is what i have done :
import java.util.Scanner;
public class Programming {
public static void main(String[] args) {
// Generate a lottery
int lottery = (int) (Math.random() * 1000);
// Prompt the user to enter a guess
Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (three digits): ");
int guess = input.nextInt();
// Get digits from lottery
int lotteryDigit1 = lottery / 100;
int lotteryDigit2 = (lottery % 100) / 10;
int lotteryDigit3 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 100;
int guessDigit2 = (guess % 100) / 10;
int guessDigit3 = guess % 10;
System.out.println("The lottery number is " + lotteryDigit1
+ lotteryDigit2 + lotteryDigit3);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win $10,000");
} else if ((guessDigit1 == lotteryDigit2 && guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit3)
|| (guessDigit1 == lotteryDigit2
&& guessDigit1 == lotteryDigit3 && guessDigit3 == lotteryDigit1)
|| (guessDigit1 == lotteryDigit3
&& guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit2)
|| (guessDigit1 == lotteryDigit3
&& guessDigit2 == lotteryDigit2 && guessDigit3 == lotteryDigit1)
|| (guessDigit1 == lotteryDigit1
&& guessDigit2 == lotteryDigit3 && guessDigit3 == lotteryDigit2)) {
System.out.println("Match all digits: you win $5,000");
} else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2
|| guessDigit1 == lotteryDigit3 || guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3
|| guessDigit3 == lotteryDigit1 || guessDigit3 == lotteryDigit2
|| guessDigit3 == lotteryDigit3) {
System.out.println("Match one digit: you win $1,000");
} else {
System.out.println("Sorry, no match");
}
}
}
thank you for your help

Another Edit
Sorry for taking a while to get back to you. Been a busy couple days. As requested, here is the complete program. Note that this isn't entirely your original program. I removed a couple things and added a few more in. Perhaps most importantly, I changed the function that I gave you previously. I originally answered your question to try to only give you the four digit match but if I am writing out the whole thing it makes more sense to generalize a bit. Because your lottery awards are based on the number of matching digits, it makes a lot of sense to make a function that counts how many digits match. This simplifies your if-else code a whole lot.
import java.util.Scanner;
public class Programming {
/*
* get the number of matching digits between the guess and the
* answer, ignoring repeated matches
*/
public static int numberDigitsMatch(String guess, String answer) {
int numberMatch = 0;
int currentIndex = 0;
int matchingIndex;
while (currentIndex < guess.length()) {
// check if the current digit of the guess is in the answer
matchingIndex = answer.indexOf(guess.charAt(currentIndex));
if (matchingIndex < 0) {
currentIndex++;
}
else {
currentIndex++;
numberMatch++;
// remove the no longer relevant character from the answer
answer = answer.substring(0, matchingIndex) +
answer.substring(matchingIndex + 1);
}
}
return numberMatch;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// generate the winning number
int lotteryNumber = (int) (Math.random() * 10000);
String lotteryString = "" + lotteryNumber;
System.out.println("The lottery number is " + lotteryString);
// Prompt the user to enter a guess
System.out.print("Enter your lottery pick (four digits): ");
// get the user's guess
int guessNumber = in.nextInt();
String guessString = "" + guessNumber;
// NOTE: these guessDigit numbers are not necessary. I am leaving
// them here so you can see how to pick out individual digits
int guessDigit1 = guessNumber % 10;
int guessDigit2 = (guessNumber / 10) % 10;
int guessDigit3 = (guessNumber / 100) % 10;
int guessDigit4 = (guessNumber / 1000) % 10;
int lotteryDigit1 = lotteryNumber % 10;
int lotteryDigit2 = (lotteryNumber / 10) % 10;
int lotteryDigit3 = (lotteryNumber / 100) % 10;
int lotteryDigit4 = (lotteryNumber / 1000) % 10;
System.out.println("The lottery number is " + lotteryString);
int numMatchingDigits = numberDigitsMatch(guessString, lotteryString);
if (guessNumber == lotteryNumber) {
System.out.println("Exact match: you win $10,000");
}
else if (4 == numMatchingDigits) {
System.out.println("Match all digits: you win $5,000");
}
else if (3 == numMatchingDigits) {
System.out.println("Match three digits: you win $2,000");
}
else if (2 == numMatchingDigits) {
System.out.println("Match two digits: you win $500");
}
else if (1 == numMatchingDigits) {
System.out.println("Match two digit: you win $500");
}
else {
System.out.println("Sorry, no match");
}
}
}
Edit
Looks like I did not understand correctly. I believe there are multiple ways to get a given digit of a number but I think this is the easiest to think about. You divide by 1000 to shift the thousands digit into the 1's place then use the modulus operator to get rid of anything that isn't in the 1's place. This leaves you with nothing but the fourth digit of the number.
public static int getFourthDigit(int num) {
return (num / 1000) % 10;
}
Original
If I understand correctly, your difficulty is with matching different ordered four digit numbers. The most obvious way to do this is to just check all possible orderings of the four digits; there are only 15 where they are not exactly equal. However, typing out 15 conditions is prone to errors and just plain boring. It would be twice as boring and tedious if you instead needed to do this with five digits.
Here is a function that avoids this by using String instead of int. It repeatedly checks the first character of the guess and checks if the character is in the answer. It then removes that matching character from the answer to avoid a case like 1111 and 1112, where every character in the guess is also in the answer but they do not match.
public static boolean digitsMatch(String guess, String answer) {
int matchingIndex;
while (guess.length() > 0) {
// check if the first digit of the guess is in the answer
matchingIndex = answer.indexOf(guess.charAt(0));
// if not, there cannot possibly be four matches
if (matchingIndex < 0) {
return false;
}
// look at the rest of the guess
guess = guess.substring(1);
// and remove the no longer relevant character from the answer
answer = answer.substring(0, matchingIndex) +
answer.substring(matchingIndex + 1);
}
return true;
}

Related

code keep giving me exception. im not sure why

sorry if this is not allowed. it's my first time asking a question. anyways I'm supposed to implement a program that read a grade level base on the text.
"Implement a program that computes the approximate grade level needed to comprehend some text, per the below.
Text: Congratulations! Today is your day. You're off to Great Places! You're off and away! Grade 3"
after finishing the code. every time I compile it, it gives me an exception that I'm dividing by zero. almost like after I ask the user to enter the text, it's not read at all in letter count to it stays at zero. I'm not sure how to go around it.
here's my code below
import java.util.*;
import java.lang.Math;
public class Readability {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int letterCount=0;
int wordCount = 0;
int sentenceCount = 0;
// Just taking the input
System.out.println("Please provide some text as input");
String text = sc.nextLine();
for (int i = 0; i< text.length(); i++) {
// checking letters
if ((text.charAt(i) == 'a' && text.charAt(i) == 'z') || (text.charAt(i) == 'A' && text.charAt(i) == 'Z' ))
{
letterCount++;
}
// any chars separated by space is a word
if (text.charAt(i) == ' ' || text.charAt(i) == '\0')
{
wordCount++;
}
// when you see a . ! or ? count as sentence
if (text.charAt(i) == '!' || text.charAt(i) == '.'|| text.charAt(i) == '?')
{
sentenceCount++;
}
}
double averageWordsPer100 = (letterCount * 100) / wordCount;
double averageSentencePer100 = (sentenceCount * 100) / wordCount;
int readingIndex = (int) Math.round(0.0588 * averageWordsPer100 - 0.296 * averageSentencePer100 - 15.8);
if (readingIndex < 1)
{
System.out.println("Before Grade 1");
}
else if (readingIndex > 16)
{
System.out.println("Grade 16");
}
else
{
System.out.println("Grade " + readingIndex);
}
}
}
I compiled and executed the project and no problem is shown, except when the input has 0 words. To avoid the divided by 0 exception you can add this piece of code:
int readingIndex = 0;
if (wordCount != 0){
double averageWordsPer100 = (letterCount * 100) / wordCount;
double averageSentencePer100 = (sentenceCount * 100) / wordCount;
readingIndex = (int) Math.round(0.0588 * averageWordsPer100 - 0.296 * averageSentencePer100 - 15.8);
}
You can set the initial value of readingIndex to the value you want. Then, if the wordCount is 0, the readingIndex initial value will be used.

How to continue loop based on user input in java program

I have a lottery program that will run bets and randomly generate numbers. When the user reaches $5000 in total wins, that user will have an option to cash out $5000 and use the remaining balance to continue the loop. The user will also be able to end the loop if choosing to not cash out. How do I continue a loop based on user input? If the user writes yes, the loop will continue, but I am not sure how to do that. Can someone please help? If the loop is supposed to continue, $5000 removed from the user's total win because of cashout, the remaining balance will be used to play.
I have tried putting the if statement for user input in multiple places in the main loop, but the user budget keeps increasing, it does not subtract $5000.
java.util.Scanner
while (budget > 0) {
lottery = (int) (Math.random() * 100);
guess = (int) (Math.random() * 100);
budget -= 1; // budget = budget-1;
// Get digits from lottery
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
System.out.println("The computer picked number is " + guess);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win $10,000");
totalWin += 1000;
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
System.out.println("Match all digits: you win $3,000");
totalWin += 300;
} else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2) {
System.out.println("Match one digit: you win $1,000");
totalWin += 100;
} else
System.out.println("Sorry, no match");
budget += totalWin;
totalWin=0;
if (budget > initBudget)
if ((budget-initBudget) >= 5000) {
System.out.println("You have reached the goal of 5000, we can go home!");
System.out.println("Enter 1 if you would like to cash out and use remaining balance to play and 0 if you would like to stop");
int decison = input.nextInt();
if (decison==1){budget=budget-5000;
System.out.println("Budget After play " + budget);
continue;
}break;
}
System.out.println("Budget After play " + budget);
}
So in my code, I have the loop
System.out.println("Enter 1 if you would like to cash out and use remaining balance to play and 0 if you would like to stop");
int decison = input.nextInt();
if (decison==1){budget=budget-5000;
System.out.println("Budget After play " + budget);
continue;
}break;
}
This loop should receive user input and then subtract 5000 from the total win and use the remaining budget to continue play.. but it is only increasing. Don't understand why it is increasing if I have -5000.
(if possible, please help me change the answer to a yes/no rather than input 1)
I assumed you are using java.util.Scanner. Here is the answer;
System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you " +
"would like to stop");
String decison = input.nextLine();
if ("yes".equals(decison))
{
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break; // break the statement.
Erçin Akçay explained how to change the input from an int to a String.
I took a look at your code and made a runnable main method using it, and it works as you would expect. Perhaps the fact that it needs to be 5k from the intial budget is confusing?
if ((budget - initBudget) >= 5000)
Here is the entire class I used to check your code if it is helpful:
import java.util.Scanner;
public class Test {
private static int budget;
private static int lottery;
private static int guess;
private static int totalWin;
private static int initBudget = 4500;
public static void main(String[] args) throws InterruptedException {
Scanner input = new Scanner(System.in);
budget = initBudget;
while (budget > 0) {
lottery = (int) (Math.random() * 100);
guess = (int) (Math.random() * 100);
budget -= 1; // budget = budget-1;
// Get digits from lottery
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
System.out.println("The computer picked number is " + guess);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win $10,000");
totalWin += 1000;
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
System.out.println("Match all digits: you win $3,000");
totalWin += 300;
} else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2) {
System.out.println("Match one digit: you win $1,000");
totalWin += 100;
} else {
System.out.println("Sorry, no match");
}
budget += totalWin;
totalWin = 0;
if (budget > initBudget) {
if ((budget - initBudget) >= 5000) {
System.out.println("You have reached the goal of 5000, we can go home!");
System.out.println(
"Enter 1 if you would like to cash out and use remaining balance to play and 0 if you "
+ "would like to stop");
int decison = input.nextInt();
if (decison == 1) {
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break;
}
}
System.out.println("Budget After play " + budget);
}
}
}
You can change your code like this:
System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you would like to stop");
String decision = input.next();
if ("Yes".equalsIgnoreCase(decision)) { // Cashing Out
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break; // breaking the loop (if user enters anything other than yes)
Here I have used String (to store User's response as a word). If he enters Yes, yes or YES (ignoring the case, it will be considered that user wants to cash out) the loop will not break.

Credit Card Number validity with Luhn's Algorithm (Java)

I'm working on a school assignment that checks whether a credit card number that is entered is valid or not, using Luhn's Algorithm.
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388 5760 1840 2626):
Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
Now add all single-digit numbers from Step 1: 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
Add all digits in the odd places from right to left in the card number: 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
Sum the results from Step 2 and Step 3: 37 + 38 = 75
If the result from Step 4 is divisible by 10 the card number is valid; otherwise, it is invalid. For example, the number 4388 5760 1840 2626 is invalid, but the number 4388 5760 1841 0707 is valid.
I need to write this program using the methods in the code I have written:
public class CreditCardValidation {
public static void main(String[] args, long input) {
Scanner numberinput = new Scanner(System.in);
System.out.println("Enter a credit card number as a long integer: ");
long cardnumber = numberinput.nextLong();
if (isValid(input) == true) {
System.out.println(numberinput + " is valid.");
} else {
System.out.println(numberinput + " is invalid.");
}
}
public static boolean isValid(long number){
int total = sumOfDoubleEvenPlace + sumOfOddPlace;
return (total % 10 == 0) && (prefixMatched(number, 1) == true) &&
(getSize(number)>=13) && (getSize(number)<=16);
}
public static int sumOfDoubleEvenPlace(long number) {
int doubledevensum = 0;
long place = 0;
while (number > 0) {
place = number % 100;
doubledevensum += getDigit((int) (place / 10) * 2);
number = number / 100;
}
return doubledevensum;
}
public static int sumOfOddPlace(long number) {
int oddsum = 0;
while (number <= 9) {
oddsum += (int)(number % 10);
number = number % 100;
}
return oddsum;
}
public static int getDigit(int number) {
if (number <= 9) {
return number;
} else {
int firstDigit = number % 10;
int secondDigit = (int)(number / 10);
return firstDigit + secondDigit;
}
}
public static boolean prefixMatched(long number, int d) {
if ((getPrefix(number, d) == 4)
|| (getPrefix(number, d) == 5)
|| (getPrefix(number, d) == 3)) {
if (getPrefix(number, d) == 3) {
System.out.println("\nVisa Card ");
} else if (getPrefix(number, d) == 5) {
System.out.println("\nMaster Card ");
} else if (getPrefix(number, d) == 3) {
System.out.println("\nAmerican Express Card ");
}
return true;
} else {
return false;
}
}
public static int getSize(long d) {
int count = 0;
while (d > 0) {
d = d / 10;
count++;
}
return count;
}
public static long getPrefix(long number, int k) {
if (getSize(number) < k) {
return number;
} else {
int size = (int)getSize(number);
for (int i = 0; i < (size - k); i++) {
number = number / 10;
}
return number;
}
}
}
I just started learning how to program two months ago so I am fairly new to this. The program doesn't compile and I don't know why and what I have to do to fix this. I know there are similar topics already posted regarding this and I have been using this post to help guide me a bit. Can someone help point a student in the right direction and let me know what I'm doing wrong?
Your program isn't compiling because this line:
int total = sumOfDoubleEvenPlace + sumOfOddPlace;
since sumOfDoubleEvenPlace and sumOfOddPlace are functions, you must use them as such:
int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
In the function isValid you are trying to add two variables which do not exist. However you have defined them as functions and to use them as functions you must call them as functions using
int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);

Enter an integer that is negative and even or positive and odd

I am creating a program that asks the user for several integers and one of the statements ask the user "Enter an integer that is negative and even or positive and odd". How exactly would I go about setting up such a question? I have this so far. I have no idea how exactly I should do this, as my instructions are pretty confusing. This is what the question is for my problem:
4.
Ask the user for an integer that is either negative and even or positive
and odd. Use an if statement and compound conditional.
import java.util.Scanner;
public class ExerciseFour
{
public static void main ( String[] argsv )
{
int choice;
int choiceTwo;
int choiceThree;
Scanner input = new Scanner(System.in);
System.out.println( "Enter a number between 0 and 10" );
choice = input.nextInt();
if ( choice > 0 && choice < 10 )
{ System.out.println( "Valid number" );
}
else
{ System.out.println( "Invalid number" );
return;
}
System.out.println( "Enter a number divisible by 2 or 3?" );
choiceTwo = input.nextInt();
if ( choiceTwo % 2 == 0 && choiceTwo % 3 == 0 )
{ System.out.println( "Valid number" );
}
else
{ System.out.println( "Number not divisible by 2 or 3" );
return;
}
System.out.println( "Enter an integer that is negative and even or positive and odd (Ex. -2 or 7 )" );
choiceThree = input.nextInt();
if ( choiceThree )
{
}
else
{
}
((choiceThree > 0) && (choiceThree % 2 == 1)) || ((choiceThree < 0) && (choiceThree % 2 == 0))
The above is the compound condition you're looking for, which means:
(
(choiceThree > 0) //positive number / greater than zero
&& // AND
(choiceThree % 2 == 1) //odd number: (an odd number divided by two has a remainder of 1)
)
|| // OR
(
(choiceThree < 0) //negative number / less than zero
&&
(choiceThree % 2 == 0) //even number (an even number divided by two has a remainder of 0)
)
EDIT: % is the modulo operator.
The result of a % b is the remainder of the integer division a / b.
The key is to use The modulo operator. An even number is divisible by 2 with no remainder. So:
if (choiceThree < 0) {
if (choiceThree % 2 == 0) {
System.out.println ("Valid");
} else {
System.out.println ("Invalid");
}
} else {
if (choiceThree % 2 != 0) {
System.out.println ("Valid");
} else {
System.out.println ("Invalid");
}
}
This is a bit cumbersome, of course. A more elegant way to express this boolean logic would be by using the exclusive or (xor) operator. This operator returns true if one and only one of its operands evaluate to true:
if (choiceThree > 0 ^ choiceThree % 2 == 0) {
System.out.println ("Valid");
} else {
System.out.println ("Invalid");
}
I think there is a mistake in the second question
choiceTwo % 2 == 0 && choiceTwo % 3 == 0
you may want to write || instead of && becouse you sad devisible to 2 OR 3 ;-)
For your other problem: You have two boolean expressens wich may be true:
Ask the user for an integer that is either negative and even
(choiceThree < 0 && choiceThree % 2 == 0)
or positive and odd.
(choiceThree > 0 && choiceThree % 2 == 1)
Use an if statement and compound conditional.
So just combine these to statements with a logical OR (||)
Create a method that returns true if it's one of those scenarios:
public boolean isCorrectInteger(int number){
if ((number < 0) && (number % 2 == 0)) { //negative and even
return true;
} else if((number < 0) && (number % 2 == 1)) { // positive and odd
return true;
} else { // other cases
return false;
}
}
This can be written in a one bigger condition, I've just split it into two for the sake of a simple example.
Also take into consideration that zero is currently assigned neither to positive nor negative - you can change this as you please by using the <= or >= operators.
Try this:
System.out.println( "Enter an integer that is negative and even or positive and odd (Ex. -2 or 7 )" );
choiceThree = input.nextInt();
if ( (choiceThree>0 && choiceThree%2==1) || (choiceThree<0 && choiceThree%2==0) )
{
System.out.println("Correct");
}
else
{
System.out.printlnt("ERROR");
}

Else without if error in program, not sure what is wrong? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am very new to java, trying to learn from this book for a class and im stuck making a program. Keeps telling me else without if is an error and that it cant find the symbol labeled as guessdigit1 & 2.
It keeps saying guessdigit1 and 2 are their own class when i hover over the error dialog box next to symbol. any idea?
Thank you!
package loterry;
import java.util.Scanner;
public class Loterry {
// This program creates two random numbers, and checks to see if your guess
// makes the lottery win
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter in your guess for loterry, two digits please");
int lottery = (int)(Math.random()*100 /50);
int guess = input.nextInt();
int lotterydigit1= lottery /10;
int lotterydigit2= lottery %10;
// Get digits from guess
int guessdigit1 = guess / 10;
int guessdigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
if (guess == lottery)
System.out.println("Exact Match: you win 10,000");
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2);
System.out.println("Match all digits: you win 3,000");
else if (guessdigit1 == lotterydigit1
|| guessdigit1 == lotterydigit2
|| guessdigit2 == lotterydigit2
|| guessdigit2 == lotterydigit2)
System.out.println("match one digit: you win 1,000");
else
System.out.println("sorry no match");
}
}
here is your problem:
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2);
Remove the semicolon (;)
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2)
Note: It is recommended to always write your if else statement with bracket { } to avoid any possible error in the future, and of course it was in the Java code conventions. (see this answer for clarification on braces issue)
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2){
System.out.println("Match all digits: you win 3,000");
}
remove the semicolon (;) to avoid the complier errors
use the curly braces "{ }" block in the if else statement to avoid the confusions
if (guess == lottery)
System.out.println("Exact Match: you win 10,000");
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2); //->here
Replace
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2);
with
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2)
And you should use {} while writing if else. It makes it clean code and clean code is easy to debug.
Simply remove the semicolon(;) which you have put at the end of your first elseif.
Only statements should have semicolon(;) at its end
Working Code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter in your guess for loterry, two digits please");
int lottery = (int)(Math.random()*100 /50);
int guess = input.nextInt();
int lotterydigit1= lottery /10;
int lotterydigit2= lottery %10;
// Get digits from guess
int guessdigit1 = guess / 10;
int guessdigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
if (guess == lottery)
System.out.println("Exact Match: you win 10,000");
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2)
System.out.println("Match all digits: you win 3,000");
else if (guessdigit1 == lotterydigit1
|| guessdigit1 == lotterydigit2
|| guessdigit2 == lotterydigit2
|| guessdigit2 == lotterydigit2)
System.out.println("match one digit: you win 1,000");
else
System.out.println("sorry no match");
}
Yeah that semicolon after that else if statement is what is messing you up. But seriously though I would advise you to use brackets in the future... It makes the code look neater and thus makes it easier to keep track of things... Trust me it really helps when working on larger programs or working in a group.
Just remome ; after if condition and use {} each statement
See here docs
if (guess == lottery){
//code here
}
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2){
//code here
}
else if (guessdigit1 == lotterydigit1
|| guessdigit1 == lotterydigit2
|| guessdigit2 == lotterydigit2
|| guessdigit2 == lotterydigit2){
//code here
}
else{
//code here
}

Categories

Resources