-13

This is my code that gives user random equations to test their knowledge and no errors are showing up. When I run the code it just doesn't compile in Eclipse. When I do in intelliJ Idea it runs but no code appears and I can't do anything.

package logical;
import java.util.Random;
import java.util.Scanner;

public class logical {
@SuppressWarnings("resource")
public static void main (String args[]){

    int qn1, qn2; //Numbers for the question
    int ua; //The users answer
    int answer; //The actual answer
    int counter = 0; //Controls the number of questions that can be answered
    int tally = 0; //Number of correct responses
    String again = "y";
    Scanner in = new Scanner(System.in);
    Random dice = new Random();
    while(again == ("y") || again == ("Y")){
    for(counter=1;counter>=5;counter++){
    qn1 = 1+dice.nextInt(40);
    qn2 = 1+dice.nextInt(40);
    answer = qn1 + qn2;
        System.out.printf("What is %d + %d",qn1,qn2);
        ua = in.nextInt();
        if(ua == answer){
            System.out.println("That is correct!");
            counter++;
            tally++;
        }
        else{
            System.out.println("Sorry, that is wrong! The correct answer is " + answer);
        }
        System.out.println("Would you like to try again?(Y/N)");
        again = in.next();
        if(again=="y" || again=="Y"){
            counter=0;
        }else {
            System.out.println("Thanks for playing your results were " + tally + " out of 5");
        }
        }

    }
    System.out.println("Thanks for taking the math challenge!");
}
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

4 Answers4

0

Your code is an infinite loop.
This starts out as true:

while(again == ("y") || again == ("Y")){

Then this, which is the only thing inside the while loop:

    for(counter=1;counter>=5;counter++){

immediately finishes, because counter starts at 1, and you are expecting it to be over 5.

So if you run this, the while loop will loop forever, doing nothing.

  1. Compare strings with .equals or .equalsIgnoreCase, not ==.

  2. Fix your for loop.

  3. Indent your code properly.

khelwood
  • 55,782
  • 14
  • 81
  • 108
0

The problem is with the for loop

for(counter=1;counter>=5;counter++){...}

The value counter=0 is initialized. But,loop will get executed only after counter value is 5 or more than 5 and hence will not enter into the loop. But you were incrementing "counter" value inside the loop. Hence, output not printed.

FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
amjeremiad
  • 85
  • 1
  • 2
  • 10
0

The real culprit in the code is following:-

String again = "y";
        ////////DECLARATION///////////////
        Scanner in = new Scanner(System.in);
        Random dice = new Random();
        ////////NOW THE MAGIC HAPPENS/////
        while(again == ("y") || again == ("Y")){
            for(counter=1;counter>=5;counter++){

Few points I would like to mention:

  1. Boolean condition under while loop never gets chance to change.
  2. counter variable under for loop always fail.
  3. So the execution keeps hoping between while and for loop and stuck into never ending process.

I hope, this will make it clear.

Saroj
  • 69
  • 3
  • 11
-1
while(again == ("y") || again == ("Y")){
for(counter=1;counter>=5;counter++){
Zbyszggo
  • 1
  • 2
  • 2
    Welcome to Stack Overflow. While this code may solve the question, [including an explanation](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – Pawara Siriwardhane Jun 06 '21 at 03:20