I am tired and lost, sitting here on a Friday night trying to finish my program. The program prompts the user to enter a series of animal names and sounds, and print the corresponding verses from the song "Old MacDonald had a farm". The program will continue printing verses until the user enters "no more" instead of an animal name and sound. This is what I have so far: -
import java.util.*;
-
/**
-
* Program that outputs lyrics to Old Macdonald, by asking the user to enter a type of animal
-
* and it's noise or no more to end the program
-
*/
-
public class OldMacDonald
-
{
-
-
public static void main (String[] args)
-
{
-
Scanner stdin = new Scanner(System.in);
-
-
System.out.println("Let's sing \"Old MacDonald had a farm\": ");
-
System.out.println("Please enter an animal and a noise, or no more to stop playing.");
-
String animal = stdin.nextLine();
-
-
while (!animal.equals("no more"))
-
{
-
break;
-
}
-
-
String noise = stdin.next();
-
String stop = stdin.next();
-
-
-
printFirstLast();
-
printMiddleVerse(animal, noise);
-
printFirstLast();
-
}
-
-
public static void printFirstLast()
-
{
-
System.out.println("Old MacDonald had a farm, E-I-E-I-O.");
-
}
-
-
public static void printMiddleVerse(String animal, String noise)
-
{
-
System.out.println("And on that farm he had some " + animal + ", E-I-E-I-O ");
-
System.out.println("With a " + noise + "-" + noise + " here, and a " + noise + "-" + noise + " there");
-
System.out.println("Here a " + noise + ", there a " + noise);
-
System.out.println("Everywhere a " + noise + "-" + noise);
-
}
-
-
}
-
16 4587
Well, aside of recommending that you change your logic a bit to
while (sound read in != "no more")
{
do all your parsing and printing
}
aside of that, I missed your question. Were you having trouble with part of it? Looks like you have a good start so far...
Well, aside of recommending that you change your logic a bit to
while (sound read in != "no more")
{
do all your parsing and printing
}
aside of that, I missed your question. Were you having trouble with part of it? Looks like you have a good start so far...
We are supposed to use equals() for comparing strings not the == != etc.
you should no what you are taking into the progam...
suppose you read-in "animal" and "noise";
ask user to enter them in one line separated by space eg: cow moooo
so it possible to enter: no more--like this!!
//read like this
input = studin.nextline()
//check
you should no what you are taking into the progam...
suppose you read-in "animal" and "noise";
ask user to enter them in one line separated by space eg: cow moooo
so it possible to enter: no more--like this!!
while(true)
{
//prompt user user to enter animail and noise in one line separated by space or type "no more" to exit
//read like this
input = studin.nextline()
//check
if(!input.equals("no more")) break;//get out of while
//otherwise use STRING TOKENIZER
StringTokenizer data = StringTokeniezer(input);//check spelling for StringTok...
animal = data.nextToken();
noise = data.nextToken();
///do the rest of print>>>call you function
}
Hope i'm not making a lot of work for you....
Enjoy.
you should no what you are taking into the progam...
suppose you read-in "animal" and "noise";
ask user to enter them in one line separated by space eg: cow moooo
so it possible to enter: no more--like this!!
while(true)
{
//prompt user user to enter animail and noise in one line separated by space or type "no more" to exit
//read like this
input = studin.nextline()
//check
if(!input.equals("no more")) break;//get out of while
//otherwise use STRING TOKENIZER
StringTokenizer data = StringTokeniezer(input);//check spelling for StringTok...
animal = data.nextToken();
noise = data.nextToken();
///do the rest of print>>>call you function
}
Hope i'm not making a lot of work for you....
Enjoy.
We are not able to use String Tokenizer, we are supposed to use while loops and equals() and that it pretty much it.
-
while (!animal.equals("no more"))
-
{
-
break;
-
}
-
This little loop doesn't make sense; think about it: suppose you typed 'cow'
for the animal so it doesn't equal "no more" and the body of the loop is run:
it breaks out of the loop immediately.
Now suppose you typed 'no more' so the body of the loop isn't executed.
kind regards,
Jos
We are supposed to use equals() for comparing strings not the == != etc.
Yes, that is pseudocode. As you had it correctly in your first snippet, I figured you would be able to make that leap, considering I had a variable (if taken literally) called 'sound read in' with spaces, I was trying to convey the logic, not the literal code.
Well, aside of recommending that you change your logic a bit to
while (sound read in != "no more")
{
do all your parsing and printing
}
aside of that, I missed your question. Were you having trouble with part of it? Looks like you have a good start so far...
I think I have been concentrating to hard on this and have not been letting it come to me, I am stuck and trying to implement all the suggestions I have found but no been able to understand.
I switched the code around a bit but it is still waiting for input even after I entered "no more" -
while (!"no more".equals(animal))
-
{
-
System.out.println("Thanks for playing!");
-
break;
-
}
-
I switched the code around a bit but it is still waiting for input even after I entered "no more" -
while (!"no more".equals(animal))
-
{
-
System.out.println("Thanks for playing!");
-
break;
-
}
-
This doesn't make any difference from what I explained in my previous reply.
Basically it reads: while you haven't typed "no more" print a goodbye message
once because you'll break out of the loop immediately.
That while loop should be the outermost structure in your program because you
*do* want to play if the user hasn't typed "no more".
kind regards,
Jos
This doesn't make any difference from what I explained in my previous reply.
Basically it reads: while you haven't typed "no more" print a goodbye message
once because you'll break out of the loop immediately.
That while loop should be the outermost structure in your program because you
*do* want to play if the user hasn't typed "no more".
kind regards,
Jos
Thank you for your help, I understand what you are say and will apply it to the program!
This doesn't make any difference from what I explained in my previous reply.
Basically it reads: while you haven't typed "no more" print a goodbye message
once because you'll break out of the loop immediately.
That while loop should be the outermost structure in your program because you
*do* want to play if the user hasn't typed "no more".
kind regards,
Jos
I think I have it now, can you tell me what you think, it does what I need it to do but I was wondering if you could comment on the efficiency of the code? -
public class OldMcD
-
{
-
-
public static void main(String[] args)
-
{
-
Scanner stdin = new Scanner(System.in);
-
-
System.out.println("Let's sing \"Old MacDonald had a farm\": ");
-
System.out.println("Please enter an animal and noise, or no more to stop playing.");
-
String animal = stdin.next();
-
String noise = stdin.next();
-
-
while ((!"no".equals(animal)) & (!"more".equals(noise)))
-
{
-
System.out.println();
-
printFirstLast();
-
printMiddleVerse(animal, noise);
-
printFirstLast();
-
System.out.println();
-
System.out.println("Please enter an animal and noise, or no more to stop playing.");
-
animal = stdin.next();
-
noise = stdin.next();
-
}
-
-
}
-
-
public static void printFirstLast()
-
{
-
System.out.println("Old MacDonald had a farm, E-I-E-I-O.");
-
}
-
-
public static void printMiddleVerse(String animal, String noise)
-
{
-
System.out.println("And on that farm he had some " + animal + ", E-I-E-I-O ");
-
System.out.println("With a " + noise + "-" + noise + " here, and a " + noise + "-" + noise + " there");
-
System.out.println("Here a " + noise + ", there a " + noise);
-
System.out.println("Everywhere a " + noise + "-" + noise);
-
}
-
-
}
-
Hi, I have the same assignment and i am trying to go by with what you have started but when i compile the program it tells me that it cannot find the variable for animal? did this happen to you?
Hi, I have the same assignment and i am trying to go by with what you have started but when i compile the program it tells me that it cannot find the variable for animal? did this happen to you?
Are you using the code in this thread? Maybe you should be using it only for reference rather than copying the whole thing.
yeah i used it for reference but i actually figured it out i was including the right variable name in the method
Ok, used the pipes instead of & in the while loop and I have this now! Thanks for the advice!
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
15 posts
views
Thread by chahnaz.ourzikene |
last post: by
|
10 posts
views
Thread by Pavan |
last post: by
|
11 posts
views
Thread by steve smith |
last post: by
|
6 posts
views
Thread by J |
last post: by
|
7 posts
views
Thread by Buck Rogers |
last post: by
| | | | | | | | | | | | | | | |