restart void main?
class Randoms{
publicstaticvoid main ( String args[] )throws java.io.IOException{
System.out.print("Choose a button and click it...");
char choice = (char) System.in.read();
if (choice =='a')
System.out.println("LYKE TEH OMFG U WIN!");
if (choice !='a')
System.out.println("You lose! Who in the hell would pick " + choice +"?");
}
}
alright at the end of the main void i want it so if you pick the wrong answer it restarts from the beginning...how would i go about doing that
this is what i have now
class Randoms {
public static void main ( String args[] ) throws java.io.IOException {
char choice;
do {
System.out.print("Choose a button and click it...");
choice = (char) System.in.read();
if (choice == 'a')
System.out.println("LYKE TEH OMFG U WIN!");
if (choice != 'a')
System.out.println("Loser! Who in the hell would pick " + choice + "?");
} while (choice != 'a');
}
}
but the loser message shows up 3 times for some reason
> no i know; the loop works fine
No, it doesn't. It's being called three times for three different characters every time you type a character and then hit enter. If you did it on Linux, it would only be called twice.
Add this line immediately after you read choice from System.in
System.out.println((int)choice);
jverda at 2007-7-12 20:39:25 >

what about using scanner like this?
class Randoms
{
private static Scanner scan = new Scanner(System.in);
public static void main(String args[]) throws java.io.IOException
{
char choice;
do
{
System.out.print("Choose a button and click it...");
choice = scan.nextLine().charAt(0);
.............
}
while (choice != 'a');
}
}
Or is this not a smart way to do this?
Message was edited by:
petes1234
> what about using scanner like this?
> > class Randoms
> {
> private static Scanner scan = new
> Scanner(System.in);
>
> public static void main(String args[]) throws
> java.io.IOException
>{
>char choice;
>do
>{
> System.out.print("Choose a button and click
> it...");
>choice = scan.nextLine().charAt(0);
> .............
>}
> while (choice != 'a');
>}
>
>
>
> Or is this not a smart way to do this?
>
> Message was edited by:
> petes1234
That would be what jverd was suggesting in reply #4.