Onoinato
Onoinato

Reputation: 1

Quadratic Formula With User Input (Java)

I'm a complete newbie at coding and I'm not really sure how to word this question 100%. I'm trying to figure out why my program is only storing the first input for value 'a' even if that number is a negative.

For example: a= -1 (Value must be greater than 0. Enter value 'a' again:)a= 1 b= 3 c= -4

So when the program compiles it only has the first value for 'a'(-1) stored instead of the second value (1) and terminates the program when it hits the second if code block. I'm completely stumped on how to fix this :/

Here's my code:

package quadratic;

import java.util.*;

//Program that does quadratic equations
public class Quadratic{
    public static void main(String[] args){

        boolean run = true;
        while(run){ //if program completes true, will start program again
            Scanner sc = new Scanner(System.in); // set scanner to allow user input

            System.out.println("Please enter value for 'a':");
            double a = (sc.nextDouble()); //looking for user input
            if (a <= 0){
                System.out.print("Value must be greater than 0. Enter value 'a' again:\n");
                sc.nextDouble(); //prompt user again if value less than or equal to 0
            }
            System.out.println("Please enter value for 'b':");
            double b = (sc.nextDouble());
            System.out.println("Please enter value for 'c':");
            double c = (sc.nextDouble());
            System.out.printf("Values entered: a:%s b:%s c:%s \n",a,b,c);

            if (Math.pow(b,2)- (4 * a * c) <= 0){
                System.out.println("Impossible. Program Terminating");
                System.exit(0); //Terminate program
            }

            double qf1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
            double qf2 = (-b - Math.sqrt(Math.pow(b, 2) -(4 * a * c)))/ (2 * a);
            // qf stands for Quadratic Formula
            System.out.printf("Anwser One: %s \n", qf1); //%s whatever qf1 returned
            System.out.printf("Anwser Two: %s \n", qf2); //%s whatever qf2 returned
        }
    }
}

Upvotes: 0

Views: 2155

Answers (2)

Wololo
Wololo

Reputation: 861

 if (a <= 0){
    System.out.print("Value must be greater than 0. Enter value 'a' again:\n");
    sc.nextDouble(); //prompt user again if value less than or equal to 0
    }

Replace the above code with:

 if (a <= 0){
    System.out.print("Value must be greater than 0. Enter value 'a' again:\n");
    a = sc.nextDouble(); //prompt user again if value less than or equal to 0
    }

Plus, you should use a loop instead of if statement, like:

 while (a <= 0){
    System.out.print("Value must be greater than 0. Enter value 'a' again:\n");
    a = sc.nextDouble(); //prompt user again if value less than or equal to 0
    }

Upvotes: 0

tim_yates
tim_yates

Reputation: 171074

You just call

sc.nextDouble(); //prompt user again if value less than or equal to 0

not

a = sc.nextDouble(); //prompt user again if value less than or equal to 0

So, you're just throwing away the second number

Also, you could just enter a negative number the second time, as you don't check for that being less than zero (hint: Maybe a loop, until a valid value for a is entered?)

Upvotes: 2

Related Questions