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
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); } }