Help Java Code

Discussion in 'Software' started by j3o14089, Feb 25, 2012.

  1. j3o14089

    j3o14089 Private E-2

    So i'm getting a ";" expected error and I can't figure out why. It's giving me the error on my "Else" statement. Right after the word system it's pointing to the ".". I thought it was random error so i changed everything imaginable to try and get it to go away but it won't. So obviously something is wrong and i'm not catching it.

    import java.util.Scanner;

    public class AdditionQuiz
    {
    public static void main(String [] args)
    {
    Scanner input = new Scanner(System.in);

    // Declare and initialize variables

    int Num1 = floor(random * 100) + 1;
    int Num2 = floor(random * 100) + 1;
    int Answer = 0;
    int Answer2 = 0;

    // Prompt user for input

    System.out.println("What is " + Num1 + " + " + Num2);
    Answer = input.nextInt();

    // Check answer and display result

    Answer2 = Num1 + Num2;
    If(Answer2 == Answer);
    System.out.println("Correct " + Num1 + " + " + Num2 + " = " + Answer2);
    Else
    System.out.println("Wrong " + Num1 + " + " + Num2 + " = " + Answer2);
    }
    }
     
  2. j3o14089

    j3o14089 Private E-2

    So i figured out my random number generator was completely wrong. Not sure what I was thinking there but I got that part of it working.
     
  3. PC-XT

    PC-XT Master Sergeant

    If(Answer2 == Answer);
    does nothing, because it only executes the empty statement before the first semicolon. That ends the if statement, so the next line (Correct Answer) always runs. Else is orphaned, because there are two semicolons between it and the if, outside of {}

    It's generally good practice to use {} to avoid that, especially when you are working with code, because it's also easier to add more statements.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds