do while loop, variable can't be found
import java.util.*;
public class Pig{
public static void main (String [] args){
int player1 = 0;
int player2 = 0;
String s = "";
String b = prompt(s);
if (b.charAt(0) =='y'){
player1();
player2();
} else{
System.out.println("Too bad maybe next time!!");
}
}
//Initial Prompt
public static String prompt (String s){
System.out.println ("Welcome to the game of \"pig\".");
System.out.println ("This is a two-player game.");
System.out.println ("The first player rolls a 6-sided die.");
System.out.println ("The player can roll as many times as she/he");
System.out.println ("likes until she/he wishes to stop or gets a 1.");
System.out.println ("If the first player chooses to stop, she/he");
System.out.println ("gets the sum of all her/his rolls added to");
System.out.println ("her/his score. If the first player stops becuase");
System.out.println ("she/he has rolled a one, she/he gets no points");
System.out.println ("for that turn. The first player to reach 100");
System.out.println ("points wins the game.");
System.out.print ("Do you want to play(y/n)? ");
String c = scanner();
return c;
}
// Turn for first player
public static void player1 (){
System.out.println ("PLAYER 1 ************************************ rolling...");
do{
int i = random();
System.out.println("You rolled a "+i);
System.out.print("Do you want to rolla gain? ");
String c = scanner();
String d = c.toLowerCase();
} while (d.charAt(0) == 'y');
System.out.println("A wise choice");
}
//Turn for second player
public static void player2 (){
System.out.println ("PLAYER 2 ************************************ rolling...");
do{
int i = random();
System.out.println("You rolled a "+i);
System.out.print("Do you want to rolla gain? ");
String c = scanner();
String d = c.toLowerCase();
} while (d.charAt(0) == 'y');
System.out.println("A wise choice");
}
//Generates numbers
public static int random (){
Random rand = new Random ();
int random = rand.nextInt (6)+1;
return random;
}
//Scanner input
public static String scanner (){
Scanner sc = new Scanner (System.in);
String yn = sc.next();
String s = yn.toLowerCase();
return s;
}
}
--- Update ---
these are the error messages i received:
ivans-mbp:Documents IvanBaird$ javac Pig.java
Pig.java:43: cannot find symbol
symbol : variable c
location: class Pig
} while (c.charAt(0) == 'y');
^
Pig.java:56: cannot find symbol
symbol : variable d
location: class Pig
} while (d.charAt(0) == 'y');
^
2 errors
Re: do while loop, variable can't be found
Did you post the errors for this code? I can't find this statement in the code:
Code :
while (c.charAt(0) == 'y');
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.
Re: do while loop, variable can't be found
You have declared the variables inside the do while loop. The condition statement of the do while loop is outside the loop and therefore does not have access to the variables declared inside the loop.
Re: do while loop, variable can't be found
sorry i forgot to save the changes i made before trying to compile the second time.
these are the new error codes:
ivans-mbp:Documents IvanBaird$ javac Pig.java
Pig.java:44: cannot find symbol
symbol : variable d
location: class Pig
} while (d.charAt(0) == 'y');
^
Pig.java:58: cannot find symbol
symbol : variable d
location: class Pig
} while (d.charAt(0) == 'y');
^
2 errors
if I'm not supposed to assign the variables inside the loop then how do i initiate the scanner repeatedly without it being in the loop.
Re: do while loop, variable can't be found
Read my post again. I never mentioned assigning values to the variables.