Welcome to Dream.In.Code
Become a Java Expert!

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




Incrementing by a variable.

 
Reply to this topicStart new topic

Incrementing by a variable.

poleman
24 Mar, 2007 - 03:12 AM
Post #1

New D.I.C Head
*

Joined: 24 Mar, 2007
Posts: 6


My Contributions
I want to adapt the code below so that when int i increments by 1, c1.xlocation increments by its speed variable. ie in this case 30. rather than by 1.

Probably really simple but I can't see it!




CODE

public class Main
{

public String name; public int startX; public int endX; public int startY; public int endY;  //road vars
public String vname; public double speed; public double xlocation; public double ylocation;  //vehicle vars

    public static void main(String[] args){
        Road h1 = new Road ("H1", 0, 600, 50, 100);
        Road h2 = new Road ("H2", 0, 600, 150, 200);
        Road h3 = new Road ("H3", 0, 600, 250, 300);
        Road v1 = new Road ("V1", 50, 100, 0, 400);
        Road v2 = new Road ("V2", 150, 200, 0, 400);
        Road v3 = new Road ("V3", 250, 300, 0, 400);
        Vehicle c1 = new Car ("Car 1", 30, 20, 52);
        Vehicle b1 = new Bike ("Bike 1", 15, 20, 51);
        
        for (int i = 0; i<10; i++, c1.xlocation++)            
        
        System.out.println("Car 1 is at x position " + c1.xlocation);
        
}
}

User is offlineProfile CardPM
+Quote Post

horace
RE: Incrementing By A Variable.
24 Mar, 2007 - 05:23 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
try
CODE

        for (int i = 0; i<10; i++, c1.xlocation+=30)            


User is offlineProfile CardPM
+Quote Post

poleman
RE: Incrementing By A Variable.
24 Mar, 2007 - 05:30 AM
Post #3

New D.I.C Head
*

Joined: 24 Mar, 2007
Posts: 6


My Contributions
QUOTE(poleman @ 24 Mar, 2007 - 04:12 AM) *

I want to adapt the code below so that when int i increments by 1, c1.xlocation increments by its speed variable. ie in this case 30. rather than by 1.

Probably really simple but I can't see it!




CODE

public class Main
{

public String name; public int startX; public int endX; public int startY; public int endY;  //road vars
public String vname; public double speed; public double xlocation; public double ylocation;  //vehicle vars

    public static void main(String[] args){
        Road h1 = new Road ("H1", 0, 600, 50, 100);
        Road h2 = new Road ("H2", 0, 600, 150, 200);
        Road h3 = new Road ("H3", 0, 600, 250, 300);
        Road v1 = new Road ("V1", 50, 100, 0, 400);
        Road v2 = new Road ("V2", 150, 200, 0, 400);
        Road v3 = new Road ("V3", 250, 300, 0, 400);
        Vehicle c1 = new Car ("Car 1", 30, 20, 52);
        Vehicle b1 = new Bike ("Bike 1", 15, 20, 51);
        
        for (int i = 0; i<10; i++, c1.xlocation++)            
        
        System.out.println("Car 1 is at x position " + c1.xlocation);
        
}
}



Thanks, icon_up.gif

That did the trick.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:40PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month