java - Problem with do while loop in JavaSE -


i'm new programming consider me great newbie. dilemma: want repeat code each time respond yes. use "do while loop" because statement comes first , boolean condition should evaluated @ last.

the code:

import java.util.scanner;  class whysoserious {      public static void main(string[] args) {         scanner sc = new scanner(system.in);          do{         string go = yes;          int setone = 0;         int settwo = 0;          system.out.println("enter 2 numbers: ");          setone = sc.nextint();         settwo = sc.nextint();         int set = setone + settwo;           system.out.println("what " +setone+ " + " +settwo+ " ? ");         int putone = 0;         putone = sc.nextint();          if (putone == set)              system.out.println("correct!");          else             system.out.println("wrong answer - correct answer is: "+set+"");          system.out.println("continue?");         cont = sc.nextline();         } while (cont == go);     } } 

i asked add cmd-prompt line.

  • c:\test>javac whysoserious.java whysoserious.java:15: cannot find symbol symbol : variable yes location: class whysoserious string go = yes; ^ whysoserious.java:38: cannot find symbol symbol : variable cont location: class whysoserious cont = sc.nextline(); ^ whysoserious.java:39: cannot find symbol symbol : variable cont location: class whysoserious } while (cont == go); ^ whysoserious.java:39: cannot find symbol symbol : variable go location: class whysoserious } while (cont == go); ^ 4 errors

i error each time try compile it. knowledge behind want repeat code every time user enters yes or no continue. observe code works without {. @ point, i'm stuck while.

multiple problems:

  • you never declare cont or yes compiler doesn't know are

  • you declare go within loop, out of scope in while expression

  • comparing strings == doesn't need here -- might have 2 different strings same contents. use equals instead.

the error messages compiler should tell first 2 -- pay attention compiler saying.

easiest fix: add string cont; before do, rid of go , make test while ("yes".equals(cont)); still exit if user enters "yes" or "y" or " yes" or other variation, however.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -