java do while loop keeps looping after condition is met -


i new java programmer , writing program sets 3 model numbers 3 printers. if user inputs wrong values want continue asking user model number. got work if first value user inters the number 1 of 3 printers. if first value not 1 of possible values , second input still keeps repeating loop.

package printing;  import java.util.scanner;  public class newclass {      public static void main(string[] args) {          int count = 0;          string machine1 = "546";         string machine2 = "892";         string machine3 = "127";           scanner s = new scanner(system.in);          system.out.print("model number:");         string modelnumber = s.nextline();         // increment count if first input value wrong         if (!s.equals(machine1) || !s.equals(machine2) || !s.equals(machine3))             count++;          // if user inputs right value         while (true) {             if (modelnumber.equals(machine1)) {                 system.out.println("machine 1 online");                 break;             }             if (modelnumber.equals(machine2)) {                 system.out.println("machine 2 online");                   break;             }             if (modelnumber.equals(machine3)) {                 system.out.println("machine 3 online");                 break;             }              // keep looping if user not input values machine1, machine2 or machine3             {                 system.out.println("try again");                 system.out.print("model number:");                 string modelnumberfalse = s.nextline();                 /* each time user gets value wrong count variable goes 1 ,                    loop breaks when count reaches 3 */                 count++;                 if (count == 3)                     break;             } while (!s.equals(machine1) || (!s.equals(machine2)) || (!s.equals(machine3)) && (count < 2));          }     } } 

also each time user inputs wrong value want count variable increment until reaches 3 , while loop breaks keeps asking model number after i've entered wrong values more 3 times.

there several problems. line wrong:

while(!s.equals(machine1) || (!s.equals(machine2)) || (!s.equals(machine3)) && (count < 2)); 

s scanner, not string, isn't valid comparison. substituting modelnumber s gives:

while(!modelnumber.equals(machine1) || (!modelnumber.equals(machine2)) || (!modelnumber.equals(machine3)) && (count < 2)); 

this can't false unless modelnumber, machine1, machine2, , machine3 same value.

also testing count messing , redundant since you're testing , breaking within loop.

it should be

while(!modelnumber.equals(machine1)      && (!modelnumber.equals(machine2))      && (!modelnumber.equals(machine3))); 

see demorgan's laws. applying rule gives

while(!(modelnumber.equals(machine1)     || modelnumber.equals(machine2)     || modelnumber.equals(machine3))) 

which may easier read.

also, if substitute "return" "break;" along making change do-while condition, works. there else going on. calling break in inner do-while causes control return top of outer while loop. adding boolean flag set before break , tested in outer while loop 1 way solve this. or use return.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -