# Multiple variables

• 03-09-2012, 11:58 PM
spek
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:
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); }}```
• 03-10-2012, 12:22 AM
subzero1012
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.
• 03-10-2012, 12:26 AM
Fubarable
Re: Multiple variables
Quote:

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.

I think that you're right on the money.

Also to the original poster: please try to post well-formatted code only. If your code is not well formatted, we will have difficulty understanding it and helping you.
• 03-10-2012, 12:27 AM
DarrylBurke
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

db
• 03-10-2012, 12:31 AM
pbrockway2
Re: Multiple variables
Quote:

i want it so that if the variable = 0 it doesn't show up in the equation
I'm not sure I understand what this means.

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

Code:

```int backwardsValue; // replaces backwards2 if(backwards > 0) {     backwardsValue = 1; } else {     backwardsValue = 2; }```
Remember that you can assign boolean expressions to boolean variables. (This one takes a little getting used to because it is unlike anything in mathematics.)

Code:

```    // replaces speed2 boolean speedIsPositive = speed > 0;```
-----

 too slow! and didn't spot that main() was inside the constructor... but the rest stands
• 03-10-2012, 12:55 AM
subzero1012
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.

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;```