Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > simple loop question

Reply
Thread Tools

simple loop question

 
 
matt
Guest
Posts: n/a
 
      07-16-2005
Im trying to write a program that asks a user to type A, B, C, or Q.
When the user types Q, the program ends. When the user types A, B, or
C, the program displays the message "Good Job!" and then asks for
another input. Its for a java course im taking and ive been at it for
an hour and a half with no luck. Any help will be greatly appreciated.

 
Reply With Quote
 
 
 
 
Alan Krueger
Guest
Posts: n/a
 
      07-16-2005
matt wrote:
> Im trying to write a program that asks a user to type A, B, C, or Q.
> When the user types Q, the program ends. When the user types A, B, or
> C, the program displays the message "Good Job!" and then asks for
> another input. Its for a java course im taking and ive been at it for
> an hour and a half with no luck. Any help will be greatly appreciated.


Show what you have so far and describe what's wrong. Otherwise, it
looks like you're asking someone to completely write it for you.
 
Reply With Quote
 
 
 
 
matt
Guest
Posts: n/a
 
      07-16-2005
Well thats the thing, im really not sure how to go about doing this but
here is what I started writing.

public class ABCInput
{
public static void main(String[] arg)throws Exception
{
char response;
do
{
response = (char)System.in.read();
// if(response != 'A' || response != 'B' || response != 'C' ||
response != 'Q')



while(prompt == 'A' || prompt == 'B' || prompt == 'C')
{
System.out.println("Good Job");
response = (char)System.in.read();

I definitly dont want someone to write it for me, that would not
benefit me in any way. Pointers in the right direction would be great
though.

 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      07-16-2005
"matt" <(E-Mail Removed)> writes:

> response = (char)System.in.read();


You need to realize that System.in is (normally) line-buffered, so
nothing gets to your program until the user hits enter. Thhen you get
what they have typed, inluding the line terminator character(s). You
assume you only get one of the four characters.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-16-2005
On 15 Jul 2005 20:56:54 -0700, "matt" <(E-Mail Removed)> wrote or
quoted :

>Im trying to write a program that asks a user to type A, B, C, or Q.


see http://mindprod.com/jgloss/homework.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
matt
Guest
Posts: n/a
 
      07-16-2005
Figured it out, guess I just needed some rest. Thanks to everyone who
replied.


public class ABCInput
{
public static void main(String[] arg)throws Exception
{
char response;
response = (char)System.in.read();
System.in.read();

while(response != 'A' && response != 'B' && response != 'C' &&
response != 'Q')
{
System.out.println("Invalid response please choose again.");
response = (char)System.in.read();
System.in.read();

while(response == 'A' || response == 'B' || response == 'C')
{
System.out.println("Good job! Please try again.");
response = (char)System.in.read();
System.in.read();
while(response == 'Q')
{
break;
}
}
}

}
}

 
Reply With Quote
 
Wibble
Guest
Posts: n/a
 
      07-16-2005
matt wrote:
> Figured it out, guess I just needed some rest. Thanks to everyone who
> replied.
>
>
> public class ABCInput
> {
> public static void main(String[] arg)throws Exception
> {
> char response;
> response = (char)System.in.read();
> System.in.read();
>
> while(response != 'A' && response != 'B' && response != 'C' &&
> response != 'Q')
> {
> System.out.println("Invalid response please choose again.");
> response = (char)System.in.read();
> System.in.read();
>
> while(response == 'A' || response == 'B' || response == 'C')
> {
> System.out.println("Good job! Please try again.");
> response = (char)System.in.read();
> System.in.read();
> while(response == 'Q')
> {
> break;
> }
> }
> }
>
> }
> }
>

At least your not comparing prompt instead of response. The nested
while loops are a little wierd.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-23-2005
On 16 Jul 2005 05:35:46 -0700, "matt" <(E-Mail Removed)> wrote or
quoted :

> while(response != 'A' && response != 'B' && response != 'C' &&
>response != 'Q')
> {
> System.out.println("Invalid response please choose again.");
> response = (char)System.in.read();
> System.in.read();
>
> while(response == 'A' || response == 'B' || response == 'C')
> {
> System.out.println("Good job! Please try again.");
> response = (char)System.in.read();
> System.in.read();
> while(response == 'Q')



This sort of logic is often nicely handled with switch. OO purists
disdain the switch, but the code seems easier to maintain to me.

loop: while ( ... )
{
switch ( response )
{
case 'A':
case 'B':
case 'C':
reward...
break;

case 'Q':
break loop;

default:
complain...
break;
} // end switch
} // end while


note how you repeated the logic "response = (char)System.in.read();"
This is a symptom there is as simpler way to write the code where that
appears only once.

You are writing this as if were c. System.in.read is not getc. In
Java, you have to read a LINE at time, groan, i.e. read a string and
pick out the first char of the line. Best to put that logic in its
own method, a kludged getC that reads the first char off a line,
converts to upper case, and returns 0 if there is no char. You also
need to deal with EOF. See http://mindprod.com/applets/fileio.html
to get it to generate you sample code to deal with the console.


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
newbie question: for loop within for loop confusion takayuki Python 17 06-17-2008 09:49 PM
newbie: for loop within for loop question takayuki Python 2 06-16-2008 03:22 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM



Advertisments