Question about IF Statement
Hey guys,
Just have a question here about IF Statement. Basically when I type in the word "end" I need the program to terminate.
However, with this code, the program terminates no matter what string/characters I enter.
Just wondering would anyone know how I would solve this ?
Thanks
Code java:
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Doorknob
{
public static void main (String args[])
{
int x=0, answer=0;
int number;
Scanner input=new Scanner (System.in);
do{
ProjectGenerator c = new ProjectGenerator();
System.out.println("");
System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
while(!input.hasNextInt())
{
if (input.nextLine().equals("end"));
{
System.exit(0);
}
System.out.println("Incorrect. Please enter only digits and try again.");//www.stackoverflow.com
System.out.println("");
System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
input.next();
}
int result = input.nextInt();
answer = (c.getRandomInt()*c.getRandomInt2());
if (result == answer)
{
System.out.println("Well done");
}
else
while (result != answer)
{
System.out.println("Unfortunately, that's not correct. Please try again.");
System.out.println("");
System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
while(!input.hasNextInt())
{
System.out.println("Incorrect. Please enter only digits and try again.");//www.stackoverflow.com
System.out.println("");
System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
input.next();
}
result = input.nextInt();
if (result == answer)
{
System.out.println("Well done");
}
}
x++;
}
while(x <= 5);
}
}
Re: Question about IF Statement
Quote:
the program terminates no matter what
Where and how does the program terminate? System.exit() or by returning?
Add some println statements to print out what is entered and saved in variables
and also to show program execution flow.
You need to add the -Xlint option to the javac command to see what problems there are:
Quote:
javac.exe -Xlint Doorknob.java
Re: Question about IF Statement
Thanks for the reply.
It terminates by returning.
All the user types in is just the answer to the multiplication question asked.
I've never came across the -Xlint option before ..
Re: Question about IF Statement
Quote:
I've never came across the -Xlint option before .
Did you try it? It will give a message telling you want is wrong with your code.
Re: Question about IF Statement
I'm not getting any errors when I compile my code.
Is that what you mean ?
Re: Question about IF Statement
Did you use the -Xlint option with the javac command? It should show you a problem when you use it.
I always use it and I get a message from the compiler with your code. You need to change how you use the compiler so that you also get the message.