need help with an if else in java

Genkora

Frog blast the vent core!
Reaction score
92
alright, I am making a program in java that will allow a user to bet on a number 2-12 and then roll two six sided dice, and then it will display their winnings (which will depend on the odds of rolling the number bet on, 2 and 12 will earn you the most, while 7 gets the least). I have already figured out how to get the probability of rolling any number and I know it works, however I am getting an error on my "else"

here is my code:

Code:
import java.util.Scanner;
import java.util.Random;

public class Dice
{
	public static void main (String[] args)
	{
		Random dice = new Random();
		Scanner scan = new Scanner(System.in);
		int roll1, roll2, mid = 7, numw, numx, numy, numz;
		double val, prob1, prob2, prob3;
		prob3 = 36;
		System.out.print("Two six sided dice will be rolled, choose a number to bet on between 2 and 12: ");
		numx = scan.nextInt();
		if (numx > mid);
		{
			numw = numx;
			numy = numw - mid;
			numz = numy * 2;
			prob1 = (numw - numz) - 1;
			prob2 = prob1/prob3;
		}
		else
		{
			numw = numx;
			prob1 = numw - 1;
			prob2 = prob1/prob3;
		}
		System.out.println("\n"+numw+" "+numy+" "+numz+" "+prob1+" "+prob2);
	}
}

If I just leave out the else and everything in it, it returns the correct values (assuming I enter something higher than 7). However, when I try to compile with the else, I get an error saying:

'else' without 'if'

I don't understand what the problem is because the 'if' is clearly there. I also tried just using two 'if's, but I kept getting logical errors with numbers higher than 7 and I couldn't figure out why (in that case it always returned values as if numbers higher than 7 went through the second 'if' anyway).

It is probably something simple I am just not seeing, but I have only been coding for about a week.
 

Slapshot136

Divide et impera
Reaction score
471
Code:
if (numx > mid);

there should be no ;

; basically signifies the end of 1 "thing", and { } is also 1 "thing", and thus you get the ; (nothing) performed as the if condition, then the { } is always performed (it's not within the if, just regular code), and then the else that has no matching if (there was regular code between the if code and the else)
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Code:
import java.util.Scanner;
import java.util.Random;

public class Dice
{
	public static void main (String[] args)
	{
		Random dice = new Random();
		Scanner scan = new Scanner(System.in);
		int roll1, roll2, mid = 7, numw, numx, numy, numz;
		double val, prob1, prob2, prob3;
		prob3 = 36;
		System.out.print("Two six sided dice will be rolled, choose a number to bet on between 2 and 12: ");
		numx = scan.nextInt();
		if (numx > mid)[COLOR="Red"][B];[/B][/COLOR]
		{
			numw = numx;
			numy = numw - mid;
			numz = numy * 2;
			prob1 = (numw - numz) - 1;
			prob2 = prob1/prob3;
		}
		else
		{
			numw = numx;
			prob1 = numw - 1;
			prob2 = prob1/prob3;
		}
		[COLOR="red"][B]System.out.println("\n"+numw+" "+numy+" "+numz+" "+prob1+" "+prob2);[/B][/COLOR]
	}
}

#1. No semicolon there.
#2. Both numw and numy are not initialized. Set them to 0 or something.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top