Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: If-Else Statement and Math

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If-Else Statement and Math

    So, I am trying to make a program that does a math problem. Can someone please look at problem and see what i am doing wrong? The problem is "double Ea = EEa + KEa + GEa;" and it cant find EEa, KEa, or GEa. Here is the full code:
    import java.util.*;
    import java.util.Scanner;
    import java.*;
    /**
     * Write a description of class EnergyWork here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class EnergyWork
    {
        // instance variables - replace the example below with your own
        private int x;
     
        /**
         * Constructor for objects of class EnergyWork
         */
        public EnergyWork()
        {
            // initialise instance variables
            x = 0;
        }
     
        public static void main(String[] args)
        {
            Scanner kb = new Scanner(System.in);
     
            System.out.println("Answer the next 3 questions about point A (Yes or No)");
     
            System.out.println("Is the object moving?");
     
            String Aa = kb.nextLine();
            if( Aa.equals("Yes")||( Aa.equals("yes")))
            {
                System.out.println("What is m?");
                double Aam = kb.nextInt();
                System.out.println("What is Va?");
                double Aav = kb.nextInt();
     
                double KEa = .5 * Aam * (Aav * Aav);
     
                System.out.print("Kinetic Energy = " + KEa);
            }
     
            System.out.println("Is the object elevated?");
     
            String Ab = kb.nextLine();
            if( Ab.equals("Yes") ||( Ab.equals("yes")))
            {
                System.out.println("What is m?");
                double Abm = kb.nextInt();
                System.out.println("What is Ha?");
                double Ha = kb.nextInt();
                double GEa = Abm * 9.8 * Ha;
     
                System.out.print("Gravitational Energy = " + GEa);
            }
     
            System.out.println("Is the object on a spring?");
     
            String Ac = kb.nextLine();
            if( Ac.equals("Yes")||( Ac.equals("yes")))
            {
                System.out.println("What is k?");
                double Ak = kb.nextInt();
                System.out.println("What is Xa?");
                double Xa = kb.nextInt();
                double EEa = .5 * Ak * (Xa * Xa);
     
                System.out.print("Elastic Energy = " + EEa);
            }
     
            double Ea = EEa + KEa + GEa;
            System.out.print("The energy of the system at point A =" + Ea);
        }
    }

    Thanks!


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: If-Else Statement and Math

    You seriously need to know about scope and lifetime of variables.

Similar Threads

  1. Math.Random()
    By xionyus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2011, 10:22 PM
  2. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  3. Math Drill
    By Java Neil in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 5th, 2011, 01:12 AM
  4. Math Problem
    By kidsforsale in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 14th, 2010, 04:09 PM
  5. Math in Java?
    By [Kyle] in forum Java Theory & Questions
    Replies: 3
    Last Post: September 23rd, 2009, 12:21 PM