0

I'm working on this program that asks for model numbers of cars infinitely until the person inputs 0 to break the loop. When i run it and input a number it just infinitely loops either your car is defective or it is not defective until it crashes. I'm pretty stuck right now any help would be greatly appreciated.

Scanner input = new Scanner(System.in);

System.out.print("Enter a model number or 0 to quit: ");
modelNum = input.nextInt();

while (modelNum != 0) {

    if (modelNum >= 189 && modelNum <= 195) {
        System.out.println("Your car is defective it must be repaired");
    } else if (modelNum == 189 || modelNum == 221) {
        System.out.println("Your car is defective it must be repaired");
    } else if (modelNum == 780) {
        System.out.println("Your car is defective it must be repaired");
    } else if (modelNum == 119 || modelNum == 179) {
        System.out.println("Your car is defective it must be repaired");

    } else {
        System.out.println("Your car is not defective");
    }
    if (modelNum == 0) {
        System.out.println("end");
        break;
    }
}

5 Answers 5

3

It's because you never ask the user for another input. You should do so before the end of the loop.

Sign up to request clarification or add additional context in comments.

Comments

0

Include the this part into your loop:

Scanner input = new Scanner(System.in);    

   System.out.print("Enter a model number or 0 to quit: ");
   modelNum = input.nextInt(); 

1 Comment

Thank you i had it in my loop earlier but was puzzled why it was coming back as not initialized, maybe it was because i didn't initialize it in my variables lol thanks!
0

You have to ask for a new value to be evaluated:

while (modelNum != 0) {
    // if conditions
    modelNum = input.nextInt();
}

Also note that:

if (modelNum == 0) {
    System.out.println("end");
    break;
}

won't be necessary because if the last value is 0 the condition in the while loop will be false and won't loop again.

Last thing: why you have all those if-else-if when they all do the same thing (print "Your car is defective it must be repaired"). This will be enough:

while (modelNum != 0) {
    if ((modelNum >= 189 && modelNum <= 195) || modelNum == 221 || modelNum == 780 || modelNum == 119 || modelNum == 179) {
        System.out.println("Your car is defective it must be repaired");
    } else {
        System.out.println("Your car is not defective");
    }
    modelNum = input.nextInt();
}

Comments

-1

if you enter 0, the loop will break, therefore the last if statement will never run.

1 Comment

That's correct but actually is not the answer to this question.
-1

This loop just tells you if the car is defective or not depending on the model number, but you never tell the program to exit the loop if the car is defective. To do so you have to put break statements into each if statement of the loop.

Moreover this statement is useless:

if(modelNum == 0) { System.out.println("end"); break;

since if u enter 0 the loop won't start.

3 Comments

The problem has nothing to do with placing break statements inside each if statement. If you do so then the while loop will iterate just once, and that's not what the OP is asking.
@JuanCarlosMendoza you are right, I misread the first part of the question, but I think he also wants his loop to stop if the car is defective, since he wrote: "When i run it and input a number it just infinitely loops either your car is defective or it is not defective until it crashes"
"I'm working on this program that asks for model numbers of cars infinitely until the person inputs 0". If you put a break statement this won't be infinite.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.