Sponsored links

Go Back   MP3Car.com > General > Off Topic


Reply
 
Share Thread Tools Display Modes
Old 05-10-2007, 05:23 PM   #1
FLAC
 
Join Date: Apr 2005
Location: Queens, New York
Posts: 1,385
Cheekz185 will become famous soon enough
Need help with a loop

Ok so i am writing a little program for school. The program is 99% done all i want to do left is add a loop so it will keep going back to the "selection menu" It is coded in java. I was wondering because i cant get a loop to seem to work. WOuld anyone beable to help put a loop so it keeps going back the the menu after the selection? ALso i need to add an exit button well here is the program:

Code:
import java.io.*; public class PetTest { public static void main(String[] args) { try { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("------------------------------------------------"); System.out.println("Please Choose From the Following:\n 1. Dogs " + "2. Cats 3. Fish 4. Birds 5. VIEW ALL "); System.out.println("------------------------------------------------"); String line = br.readLine().trim(); int selection = Integer.parseInt(line); String s = ""; switch(selection) { case 1: s = "dog"; break; case 2: s = "cat"; break; case 3: s = "bird"; break; case 4: s = "fish"; break; case 5: s = "View All"; break; default: s = "unexected selection"; } System.out.println("selection = " + s); Dog dog = null ; Cat cat = null; Fish fish = null; Bird bird = null; if(s.equalsIgnoreCase("dog") || s.equalsIgnoreCase("View All")) { dog = new Dog(4, 12, 50.00, "Terrier", Dog.DRY); System.out.println("\ndog = " + dog); } if(s.equalsIgnoreCase("cat") || s.equalsIgnoreCase("View All")){ cat = new Cat(5, 5, 25.00, "Persian", Cat.CLAWED); System.out.println("\ncat = " + cat);} if(s.equalsIgnoreCase("bird")|| s.equalsIgnoreCase("View All")){ bird = new Bird(3, 2, 5.00, "Canary", "USA"); System.out.println("\nbird = " + bird);} if(s.equalsIgnoreCase("fish")|| s.equalsIgnoreCase("View All")){ fish = new Fish(1, 1, 10.00, "Angel", Fish.SALT); System.out.println("\nfish = " + fish); } br.close(); } catch(IOException e) { System.out.println("Read error: " + e.getMessage()); } } } class Pet { int age; double weight; double price; protected Pet(int age, double weight, double price) { this.age = age; this.weight = weight; this.price = price; } public String toString() { //String name = getClass().getName(); return ", \nage:" + age + ", \nweight:" + weight + ", \nprice:" + price; } } class Dog extends Pet { String breed; String foodType; final static String CANNED = "canned food"; final static String DRY = "dry food"; public Dog(int age, double weight, double price, String breed, String foodType) { super(age, weight, price); this.breed = breed; this.foodType = foodType; } public String toString() { return "\n\nDog \nbreed:" + breed + ", \nfoodType:" + foodType + super.toString() + ""; } } class Cat extends Pet { String breed; String clawStatus; final static String CLAWED = "has claws"; final static String DECLAWED = "de-clawed"; public Cat(int age, double weight, double price, String breed, String clawStatus) { super(age, weight, price); this.breed = breed; this.clawStatus = clawStatus; } public String toString() { return "\n\nCat \nbreed:" + breed + ", \nclawStatus:" + clawStatus + super.toString() + ""; } } class Bird extends Pet { String type; String nationalOrigin; public Bird(int age, double weight, double price, String type, String origin) { super(age, weight, price); this.type = type; nationalOrigin = origin; } public String toString() { return "\n\nBird \ntype:" + type + ", \nnationalOrigin:" + nationalOrigin + super.toString() + ""; } } class Fish extends Pet { String type; String waterType; final static String FRESH = "freah water"; final static String SALT = "salt water"; public Fish(int age, double weight, double price, String type, String habitat) { super(age, weight, price); this.type = type; waterType = habitat; } public String toString() { return "\n\nFish \ntype:" + type + ", \nwaterType:" + waterType + super.toString() + ""; } }

__________________
2002 Mitsubishi Galant
Progress: 90% [-▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓- ->

Carputer Specs:
Via M10K
512mb Ram
60GB HDD
Souund Blaster Audigy2 NX
OPUS ITX PC Case

Cheekz185 is offline   Reply With Quote
Advertisement
 
Advertisement
Sponsored links

Old 05-10-2007, 05:32 PM   #2
Fusion Brain Creator
 
2k1Toaster's Avatar
 
Join Date: Mar 2006
Location: Colorado, but Canadian!
Posts: 8,862
2k1Toaster has a brilliant future2k1Toaster has a brilliant future2k1Toaster has a brilliant future2k1Toaster has a brilliant future2k1Toaster has a brilliant future2k1Toaster has a brilliant future2k1Toaster has a brilliant future2k1Toaster has a brilliant future2k1Toaster has a brilliant future2k1Toaster has a brilliant future2k1Toaster has a brilliant future
edit: nevermind, at first glance I thought polymorphic, but I see now it isnt... I hate polymorphic substitution.

Ok well just put it in a while loop, with the break statement being a switch.


Code:
import java.io.*; public class PetTest { public static void main(String[] args) { while(true) { try { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("------------------------------------------------"); System.out.println("Please Choose From the Following:\n 1. Dogs " + "2. Cats 3. Fish 4. Birds 5. VIEW ALL "); System.out.println("------------------------------------------------"); String line = br.readLine().trim(); int selection = Integer.parseInt(line); String s = ""; switch(selection) { case 1: s = "dog"; break; case 2: s = "cat"; break; case 3: s = "bird"; break; case 4: s = "fish"; break; case 5: s = "View All"; break; case 6: s = "EXIT"; break; default: s = "unexected selection"; } System.out.println("selection = " + s); Dog dog = null ; Cat cat = null; Fish fish = null; Bird bird = null; if(s.equalsIgnoreCase("exit")) { break; } if(s.equalsIgnoreCase("dog") || s.equalsIgnoreCase("View All")) { dog = new Dog(4, 12, 50.00, "Terrier", Dog.DRY); System.out.println("\ndog = " + dog); } if(s.equalsIgnoreCase("cat") || s.equalsIgnoreCase("View All")){ cat = new Cat(5, 5, 25.00, "Persian", Cat.CLAWED); System.out.println("\ncat = " + cat);} if(s.equalsIgnoreCase("bird")|| s.equalsIgnoreCase("View All")){ bird = new Bird(3, 2, 5.00, "Canary", "USA"); System.out.println("\nbird = " + bird);} if(s.equalsIgnoreCase("fish")|| s.equalsIgnoreCase("View All")){ fish = new Fish(1, 1, 10.00, "Angel", Fish.SALT); System.out.println("\nfish = " + fish); } } br.close(); } catch(IOException e) { System.out.println("Read error: " + e.getMessage()); } } } class Pet { int age; double weight; double price; protected Pet(int age, double weight, double price) { this.age = age; this.weight = weight; this.price = price; } public String toString() { //String name = getClass().getName(); return ", \nage:" + age + ", \nweight:" + weight + ", \nprice:" + price; } } class Dog extends Pet { String breed; String foodType; final static String CANNED = "canned food"; final static String DRY = "dry food"; public Dog(int age, double weight, double price, String breed, String foodType) { super(age, weight, price); this.breed = breed; this.foodType = foodType; } public String toString() { return "\n\nDog \nbreed:" + breed + ", \nfoodType:" + foodType + super.toString() + ""; } } class Cat extends Pet { String breed; String clawStatus; final static String CLAWED = "has claws"; final static String DECLAWED = "de-clawed"; public Cat(int age, double weight, double price, String breed, String clawStatus) { super(age, weight, price); this.breed = breed; this.clawStatus = clawStatus; } public String toString() { return "\n\nCat \nbreed:" + breed + ", \nclawStatus:" + clawStatus + super.toString() + ""; } } class Bird extends Pet { String type; String nationalOrigin; public Bird(int age, double weight, double price, String type, String origin) { super(age, weight, price); this.type = type; nationalOrigin = origin; } public String toString() { return "\n\nBird \ntype:" + type + ", \nnationalOrigin:" + nationalOrigin + super.toString() + ""; } } class Fish extends Pet { String type; String waterType; final static String FRESH = "freah water"; final static String SALT = "salt water"; public Fish(int age, double weight, double price, String type, String habitat) { super(age, weight, price); this.type = type; waterType = habitat; } public String toString() { return "\n\nFish \ntype:" + type + ", \nwaterType:" + waterType + super.toString() + ""; } }


Last edited by 2k1Toaster; 05-10-2007 at 05:35 PM.
2k1Toaster is offline   Reply With Quote
Old 05-10-2007, 05:43 PM   #3
Super Moderator. If my typing sucks it's probably because I'm driving....
 
turbocad6's Avatar
 
Join Date: Oct 2004
Location: NY
Posts: 6,102
turbocad6 is a splendid one to beholdturbocad6 is a splendid one to beholdturbocad6 is a splendid one to beholdturbocad6 is a splendid one to beholdturbocad6 is a splendid one to beholdturbocad6 is a splendid one to beholdturbocad6 is a splendid one to beholdturbocad6 is a splendid one to behold
gotta love the badger
turbocad6 is offline   Reply With Quote
Sponsored links
Advertisement
 
Advertisement
Reply

Bookmarks

Thread Tools
Display Modes

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
Strange ground loop problem. norbie Car Audio 5 06-18-2006 03:25 PM
Re-Ground HU or Ground Loop Isolator? Incinorator Car Audio 5 04-20-2006 08:52 PM
I tried everything, can't get rid of this ground loop intense General Hardware Discussion 3 07-10-2005 09:58 AM
VIDEO groud loop isolator lookinco LCD/Display 8 10-07-2004 07:17 AM
Turtle Beach Santa Cruz Sound Card GIVES me a ground loop? rmjjensen General Hardware Discussion 2 11-15-2003 08:26 AM



All times are GMT -5. The time now is 04:56 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.2
Copyright © 1999 - 2008 Mp3Car.com Inc.Ad Management by RedTyger
Message Board Statistics