Results 1 to 6 of 6
Thread: Multiple variables
- 03-10-2012, 12:58 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
Multiple variables
I'm just getting started with programming and i want it so that if the variable = 0 it doesn't show up in the equation. I made an alternate boolean variable but i need help with the rest or do you have any other suggestions to get what I want.
Java:
Java Code:public class App { public App() { public static void main(String[] args) { System.out.println("Skating"); float speed = 2; boolean speed2; if (speed >= 0){speed2 = true; }else if (speed <= 0){speed2 = false;} float technique = 3; boolean technique2; if (technique >= 0){technique2 = true;} else if (technique <= 0){technique2 = false;} float acceleration = 4; int acceleration2; if (acceleration >= 0){acceleration2 = 1;} else if (acceleration <= 0){acceleration2 = 2;} float backwards = 1; int backwards2; if (backwards >= 0){backwards2 = 1;} else if (backwards <= 0){backwards2 =2;} float average_skating = (backwards + acceleration + technique + speed)/4; System.out.println("the average is " +average_skating); }}
Last edited by Fubarable; 03-10-2012 at 01:08 AM. Reason: code tags added
- 03-10-2012, 01:22 AM #2
Member
- Join Date
- Feb 2012
- Location
- Phoenix, AZ
- Posts
- 26
- Rep Power
- 0
Re: Multiple variables
I'm also new to Java programming. It appears that you've placed the main method inside of what looks like the App class's constructor. I haven't seen it done this way in the examples that I've seen so far. Just trying to understand.
-
Re: Multiple variables
- 03-10-2012, 01:27 AM #4
Re: Multiple variables
Learn to indent code correctly and you won't make that kind of a silly mistake so easily: Code Conventions for the Java Programming Language: Contents
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-10-2012, 01:31 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Multiple variables
i want it so that if the variable = 0 it doesn't show up in the equation
I assume that the value assigned to average_skating should depend in some way on the values already assigned to speed, technique, acceleration and backwards. But what exactly is the relationship? Try to describe it, or give examples for different values of the independent variables.
-----
Apart from that, it's a good idea to set out code so that it's readable. The code in if blocks should be on its own line and indented (here in the forum put [code] at the start of the code and [/code] at the end). Variables should be descriptive of what they represent. Eg
Java Code:int backwardsValue; // replaces backwards2 if(backwards > 0) { backwardsValue = 1; } else { backwardsValue = 2; }
Java Code:// replaces speed2 boolean speedIsPositive = speed > 0;
[Edit] too slow! and didn't spot that main() was inside the constructor... but the rest standsLast edited by pbrockway2; 03-10-2012 at 01:33 AM.
- 03-10-2012, 01:55 AM #6
Member
- Join Date
- Feb 2012
- Location
- Phoenix, AZ
- Posts
- 26
- Rep Power
- 0
Re: Multiple variables
There may be a more efficient way of writing the code for your problem, but you could achieve your result through the following code. Just replace float average_skating = (backwards + acceleration + technique + speed)/4; with the following.
Java Code:int skating = 0; int count = 0; if (bakcwards2 == 1){ skating = skating + bakcwards; count = count + 1;} if (acceleration2 == 1){ skating = skating + acceleration; count = count + 1;} if (technique2 == true){ skating = skating + technique; count = count + 1;} if (speed2 == true){ skating = skating + speed; count = count + 1;} if (count <> 0) float average_skating = skating/count;
Similar Threads
-
Calling Variables in Multiple Methods
By PrimalScientist in forum New To JavaReplies: 10Last Post: 02-07-2012, 11:26 AM -
using variables in multiple methods
By cagipple in forum New To JavaReplies: 2Last Post: 09-14-2011, 04:44 AM -
if statements for multiple variables
By dookie1293 in forum New To JavaReplies: 9Last Post: 06-17-2011, 10:30 AM -
Array with multiple variables - Problem with input
By Xinc in forum New To JavaReplies: 5Last Post: 03-17-2011, 05:19 PM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 06:48 PM
Bookmarks