Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By subzero1012
  • 1 Post By pbrockway2

Thread: Multiple variables

  1. #1
    spek is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Blog Entries
    1
    Rep Power
    0

    Thumbs up 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 12:08 AM. Reason: code tags added

  2. #2
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default 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.
    Fubarable likes this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Multiple variables

    Quote Originally Posted by subzero1012 View Post
    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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Multiple variables

    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

    Java 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.)

    Java Code:
        // replaces speed2
    boolean speedIsPositive = speed > 0;
    -----

    [Edit] too slow! and didn't spot that main() was inside the constructor... but the rest stands
    Last edited by pbrockway2; 03-10-2012 at 12:33 AM.
    Fubarable likes this.

  6. #6
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default 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

  1. Calling Variables in Multiple Methods
    By PrimalScientist in forum New To Java
    Replies: 10
    Last Post: 02-07-2012, 10:26 AM
  2. using variables in multiple methods
    By cagipple in forum New To Java
    Replies: 2
    Last Post: 09-14-2011, 03:44 AM
  3. if statements for multiple variables
    By dookie1293 in forum New To Java
    Replies: 9
    Last Post: 06-17-2011, 09:30 AM
  4. Replies: 5
    Last Post: 03-17-2011, 04:19 PM
  5. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •