View Full Version : while loop
stu_villanti
06-15-2007, 08:48 AM
hi, Ok i am new to java programming. I am currently trying to write a new program which enters awhile loop waiting for s,g,l to be entered to e to exit the loop.
i have been trying to with no luck.
any help of where to start what to declare, programming need.
using methods if that help
thanks :)
kachkaha
06-17-2007, 10:40 AM
oh are u reading complete string at a time or you want to read charector by charector
kachkaha
06-17-2007, 10:48 AM
the code is as following
public static void Main() {
char ch;
do
{
ch = (char) Console.Read(); // get a char
Console.WriteLine(ch);
}
while(ch != 's' && ch != 'g' && ch != 'l');
}
kachkaha
06-17-2007, 10:49 AM
public static void Main() {
char ch;
do
{
ch = (char) Console.Read(); // get a char
Console.WriteLine(ch);
}while(ch != 's' && ch != 'g' && ch != 'l');
}
}
mwinter
06-17-2007, 11:35 PM
The code posted thus far won't work, but what follows is a rough framework that will at least get you started. Hopefully.
public class MyApplication {
public MyApplication() {
}
public static void main(String[] arguments) {
MyApplication application = new MyApplication();
application.start();
}
public void start() {
java.io.Reader reader = new java.io.InputStreamReader(System.in);
while (true) {
int input;
try {
input = reader.read();
} catch (java.io.IOException ioe) {
System.out.println("An unexpected error occurred whilst reading input.");
return;
}
switch (input) {
case 'e':
return;
case 'g':
/* ... */
break;
case 'l':
/* ... */
break;
case 's':
/* ... */
break;
default:
/* ... */
break;
}
}
}
}
Powered by vBulletin® Version 4.2.2 Copyright © 2023 vBulletin Solutions, Inc. All rights reserved.