java - Guess Attempts loop issue -
okay have loop isssues first if enter invalid character want program loop , re-ask user input
public boolean isvalid(){ if (letter1.equals('a')|| letter1.equals('b')|| letter1.equals('c')|| letter1.equals('d')|| letter1.equals('e')){ boolean isvalid = true; } else if(letter2.equals('a')|| letter2.equals('b')|| letter2.equals('c')|| letter2.equals('d')|| letter2.equals('e')){ boolean isvalid = true; } else if(letter3.equals('a')|| letter3.equals('b')|| letter3.equals('c')|| letter3.equals('d')|| letter3.equals('e')){ boolean isvalid = true; } else { boolean isvalid = false; } while (isvalid == false){ system.out.println("invalid input try again"); system.out.println("enter guess #1"); guess = keyboard.nextline(); guess.tolowercase(); letter1 = guess.charat(0); letter2 = guess.charat(1); letter3 = guess.charat(2); } return isvalid; }
now other problem i'm trying create guess counter user limited 6 guesses , after sixth guess user loses. i'm not sure how i'd loop checkgues in because while loop ins't working.
public void getguess(){ guessatt = 0; { if(letter1 == a){ la1 = true; } else if(letter1 == b || letter1 == c){ la2 = true; } else if (letter1 != && letter1 != b && letter1 != c){ la3 = true; } //letter2 if(letter2 == b){ lb1 = true; } else if(letter2 == || letter2 == c){ lb2 = true; } else if (letter2 != && letter2 != b && letter2 != c){ lb3 = true; } //letter3 if(letter3 == c){ lc1 = true; } else if(letter3 == || letter3 == b){ lc2 = true; } else if (letter3 != && letter3 != b && letter3 != c){ lc3 = false; } //output if(la1 == true && lb1 == true && lc3 == true){ system.out.println("2 right letter in right place"); system.out.println("0 right letter in wrong place"); } else if(la1 == true && lb3 == true && lc1 == true){ system.out.println("2 right letter in right place"); system.out.println("0 right letter in wrong place"); } else if(la1 == true && lb2 == true && lc2 == true){ system.out.println("1 right letter in right place"); system.out.println("2 right letter in wrong place"); } else if(la1 == true && lb2 == true && lc3 == true){ system.out.println("1 right letter in right place"); system.out.println("1 right letter in wrong place"); } else if(la1 == true && lb3 == true && lc2 == true){ system.out.println("1 right letter in right place"); system.out.println("1 right letter in wrong place"); } else if(la2 == true && lb2 == true && lc2 == true){ system.out.println("0 right letter in right place"); system.out.println("3 right letter in wrong place"); } else if(la2 == true && lb2 == true && lc1 == true){ system.out.println("1 right letter in right place"); system.out.println("2 right letter in wrong place"); } else if(la2 == true && lb1 == true && lc2 == true){ system.out.println("1 right letter in right place"); system.out.println("2 right letter in wrong place"); } else if(la3 == true && lb3 == true && lc3 == true){ system.out.println("0 right letter in right place"); system.out.println("0 right letter in wrong place"); } else if(la3 == true && lb1 == true && lc3 == true){ system.out.println("1 right letter in right place"); system.out.println("0 right letter in wrong place"); } else if(la3 == true && lb3 == true && lc1 == true){ system.out.println("1 right letter in right place"); system.out.println("0 right letter in wrong place"); } guessatt++; } while (guessatt < 7); }
this of code incase,
import java.util.scanner; import ja va.util.random; public class codebreaker { private character letter1; private character letter2; private character letter3; private boolean done; private boolean isvalid; private boolean la1,la2,la3,lb1,lb2,lb3,lc1,lc2,lc3; private string guess; public int a, b, c; private int i,j,guessatt; static scanner keyboard = new scanner (system.in); public static void main(string[] args) { codebreaker code = new codebreaker(); code.codeletter(); code.codebreaker(); code.getguess(); code.display(); } public void codeletter(){ random r1 = new random(); = r1.nextint(5) + (byte)'a'; random r2 = new random(); b = r2.nextint(5) + (byte)'a'; random r3 = new random(); c = r3.nextint(5) + (byte)'a'; } public void codebreaker(){ system.out.println("welcome codebreaker"); system.out.println("you have 6 tries find secret 3 letter code"); system.out.println("the letters range e"); system.out.println("good luck"); system.out.println("the code can have repeat letters"); system.out.print("\n"); system.out.println("enter guess #1"); guess = keyboard.nextline(); guess.tolowercase(); letter1 = guess.charat(0); letter2 = guess.charat(1); letter3 = guess.charat(2); } public boolean isvalid(){ if (letter1.equals('a')|| letter1.equals('b')|| letter1.equals('c')|| letter1.equals('d')|| letter1.equals('e')){ boolean isvalid = true; } else if(letter2.equals('a')|| letter2.equals('b')|| letter2.equals('c')|| letter2.equals('d')|| letter2.equals('e')){ boolean isvalid = true; } else if(letter3.equals('a')|| letter3.equals('b')|| letter3.equals('c')|| letter3.equals('d')|| letter3.equals('e')){ boolean isvalid = true; } else { boolean isvalid = false; } while (isvalid == false){ system.out.println("invalid input try again"); system.out.println("enter guess #"+ guessatt); guess = keyboard.nextline(); guess.tolowercase(); letter1 = guess.charat(0); letter2 = guess.charat(1); letter3 = guess.charat(2); } return isvalid; } public void getguess(){ guessatt = 0; { if(letter1 == a){ la1 = true; } else if(letter1 == b || letter1 == c){ la2 = true; } else if (letter1 != && letter1 != b && letter1 != c){ la3 = true; } //letter2 if(letter2 == b){ lb1 = true; } else if(letter2 == || letter2 == c){ lb2 = true; } else if (letter2 != && letter2 != b && letter2 != c){ lb3 = true; } //letter3 if(letter3 == c){ lc1 = true; } else if(letter3 == || letter3 == b){ lc2 = true; } else if (letter3 != && letter3 != b && letter3 != c){ lc3 = false; } //output if(la1 == true && lb1 == true && lc3 == true){ system.out.println("2 right letter in right place"); system.out.println("0 right letter in wrong place"); } else if(la1 == true && lb3 == true && lc1 == true){ system.out.println("2 right letter in right place"); system.out.println("0 right letter in wrong place"); } else if(la1 == true && lb2 == true && lc2 == true){ system.out.println("1 right letter in right place"); system.out.println("2 right letter in wrong place"); } else if(la1 == true && lb2 == true && lc3 == true){ system.out.println("1 right letter in right place"); system.out.println("1 right letter in wrong place"); } else if(la1 == true && lb3 == true && lc2 == true){ system.out.println("1 right letter in right place"); system.out.println("1 right letter in wrong place"); } else if(la2 == true && lb2 == true && lc2 == true){ system.out.println("0 right letter in right place"); system.out.println("3 right letter in wrong place"); } else if(la2 == true && lb2 == true && lc1 == true){ system.out.println("1 right letter in right place"); system.out.println("2 right letter in wrong place"); } else if(la2 == true && lb1 == true && lc2 == true){ system.out.println("1 right letter in right place"); system.out.println("2 right letter in wrong place"); } else if(la3 == true && lb3 == true && lc3 == true){ system.out.println("0 right letter in right place"); system.out.println("0 right letter in wrong place"); } else if(la3 == true && lb1 == true && lc3 == true){ system.out.println("1 right letter in right place"); system.out.println("0 right letter in wrong place"); } else if(la3 == true && lb3 == true && lc1 == true){ system.out.println("1 right letter in right place"); system.out.println("0 right letter in wrong place"); } guessatt++; } while (guessatt < 7); } public boolean done(){ if (la1 == true && lb1 == true && lc1 == true){ done = true; } else{ done = false; } return done; } public void display(){ if (done == true){ system.out.println("you win"); } else{ system.out.println(" try again"); } } }
Comments
Post a Comment