Announcement

Collapse
No announcement yet.

Need help with a loop

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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


  • #2
    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() + "";
        }
    }
    Fusion Brain Version 6 Released!
    1.9in x 2.9in -- 47mm x 73mm
    30 Digital Outputs -- Directly drive a relay
    15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
    Buy now in the MP3Car.com Store

    Comment


    • #3
      gotta love the badger
      MY NEWEST INSTALL:modded infiniti fx with big screen

      first windows carpc install........my liquid cooled LVDS screen :D

      Comment

      Working...
      X