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 5 of 5

Thread: Need some help.

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Canada
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need some help.

    hello everyone,

    I just started to do java and I'm suppose to solve an quadratic equation ax^2+bx+c=0.

    So everything works good, and im trying to put some restrictions on the equation like if a=0 do this , if square root=0 do this if both do this.

    the restriction works, the only problem is that every time I run the program both message shows off. What should I do?

    System.out.println("Finding the solution to ax^2+bx+c=0");
          System.out.println("Write a value for a"); 
          double a = input.nextDouble();
     
          System.out.println("Write a value for b"); 
          double b = input.nextDouble();
          System.out.println("Write a value for c"); 
          double c = input.nextDouble();
          double square = (Math.sqrt(b*b-4*a*c));
          double x1 = (-b+square)/(2*a);
          double x2 = (-b-square)/(2*a);
          double xx1 = (-b+square);
          double xx2 = (-b-square);
     
     
     
     
          if (square > 0)
          {
             System.out.println("x1= " +x1+ "  and x2= "+x2);
          }
     
          else
          {
           System.out.println("Square root is 0 making x1 and x2 =" +-b/2*a);
          }
     
          if (a>0)
          {
             System.out.println("x1 = "+x1+ "   and x2 = "+x2);
          }
          else {
             System.out.println("x1 = "+xx1+ "   and x2 = "+xx2);
       }
     
       }
    }

    Thank you.

  2. #2
    Junior Member
    Join Date
    May 2013
    Location
    Charleston SC
    Posts
    21
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default Re: Need some help.

    The reason it is printing twice is because you do your check on the square and then on a instead of doing them together.

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    Canada
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So I just do them together in the same if or use else if ?

  4. #4
    Junior Member
    Join Date
    May 2013
    Location
    Charleston SC
    Posts
    21
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default Re: Need some help.

    I would. Basically start with your if statement with the square root. Within that block define what happens if a is above 0 and if a is 0 then do the same thing in the else part of your block for the squareroot

  5. #5
    Junior Member
    Join Date
    May 2013
    Location
    Canada
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you