Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,634 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,066 people online right now. Registration is fast and FREE... Join Now!




While-if structure

 
Reply to this topicStart new topic

While-if structure

nrp88
post 11 Oct, 2008 - 08:03 AM
Post #1


New D.I.C Head

*
Joined: 19 Sep, 2008
Posts: 30

I finally got JCreator to work (@ my previous post).

But now I'm having troble adding to the code for my assignment.

The task at hand is to made fido say "rrrrruff" on the second iteration, but just "ruff" any where else.

here is the original code from my prof.
CODE
class MainApplication {

    public static void main(String args[]) {
        
        Dog fido = new Dog();    
        
        fido.bark(5);
        
    }
}

// Class Dog
class Dog {

       public void bark(int iterations){
           
           int i = 0;
           
           while (i < iterations) {
           
            System.out.println("ruff");
            
            i++;

          }
    }

}


Here is my attempt; this makes him say "rrrrruff", but it's not on the second line, it's on the fourth. My question is, how do I make him say it on the 2nd only?

CODE
class MainApplication {

    public static void main(String args[]) {
        
        Dog fido = new Dog();    
        
        fido.bark(3);
        
    }
}

// Class Dog
class Dog {

       public void bark(int iterations){
           
           int i = 0;
           
           while (i < iterations) {
           
            System.out.println("ruff");
            
            i++;
        }
        
        if (i <= 4) {
            
            System.out.println("rrrrruff");
            
            i++;
        }
    }

}


Thanks!
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 11 Oct, 2008 - 08:21 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


Change this:
CODE
       public void bark(int iterations){
          
           int i = 0;
          
           while (i < iterations) {
          
            System.out.println("ruff");
            
            i++;
        }
        
        if (i <= 4) {
            
            System.out.println("rrrrruff");
            
            i++;
        }
    }


To something more like so:
CODE
       public void bark(int iterations){
           for(int i=0; i<iterationsl; i++){ //for loops reduce the number of lines of code needed.
            if(i == 1){ // checks if the loop has been completed 1 time, if so it is on iteration 2, and the rrrrruff is printed
              System.out.println("rrrrruff");
              continue; // Skips the rest of the loop and starts the next iteration
            }
            System.out.println("ruff"); // if i != 1 ruff is printed
        }
    }


Basically, you had the if statement outside of your loop which won't work because it is only checked after the loop if complete, but you wanted it to be checked each time through the loop, so we put the if statemetn in the loop instead.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:08AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month