I'm new to Java and this is my first program. I'm trying to verify that my thought processes are good.Let me know of any improvements infor the code, whether it's too few/too many comments, poorly structured code, etc.import javax.swing.*;import java.math.*;import java.util.*;class Root{public static void main(String[] args){    //Variable & Constant Declaration    double coeffA=0.0; //Variable for Coefficient A    double coeffB=0.0; //Variable for Coefficient B    double coeffC=0.0; //Variable for Coefficient C    double xroot1=0.0; //Variable for the first root    double xroot2=0.0; //Variable for the second root    double discriminant=0.0; //Varaible for the discriminant    double root1complex=0.0; //Variable for part a of the complex root     double root2complex=0.0; //Variable for part b of the complex root    boolean contloop=true; //Variable for continuing loop    String loop=y; //Variable for input choice of continuing loop    Scanner sc = new Scanner(System.in); //An object to read from the keyboard    try{        do{            if (loop.equals(y)){                //Input                System.out.println(Please enter the value for the first coefficient: ); //Asks the user to input the value for the A term                coeffA=sc.nextDouble(); //Captures the keyboard input and stores it to the variable coeffA                System.out.println(Please enter the value for the second coefficient: ); //Asks the user to input the value for the B term                coeffB=sc.nextDouble(); //Captures the keyboard input and stores it to the variable coeffB                System.out.println(Please enter the value for the third coefficient: ); //Asks the user to input the value for the C term                coeffC=sc.nextDouble(); //Captures the keyboard input and stores it to the variable coeffC                //Calculations                discriminant=coeffB*coeffB-(4*coeffA*coeffC); //Calculates the discriminant                xroot1=(-coeffB+(Math.sqrt(discriminant)))/(2*coeffA); //Calculates the first root of the polynomial                xroot2=(-coeffB-(Math.sqrt(discriminant)))/(2*coeffA); //Calculates the second root of the polynomial                //Output                System.out.println(The Equation is:); //Displays the value for Coefficient C                System.out.println(coeffA+x^2 + +coeffB+x + + coeffC); //Displays the equation to the user                System.out.println(The discriminant is:  + discriminant); //Displays the discriminant to the user                    if(discriminant<0){                        root1complex=-coeffB/(2*coeffA); //Calculates the first part of the complex root                        root2complex=Math.sqrt(-discriminant)/(2*coeffA); //Calculates the second part of the complex root                        System.out.println(The Equation has Imaginary roots.); //Displays to the user that there are imaginary roots                        System.out.println(The Roots are:  + root1complex + + + root2complex + i +  and  + root1complex + - + root2complex + i.);                        if (coeffA==0){                            System.out.println(*NaN stands for \Not a Number.\*);                        } //Ends If                    } //Ends If                    else if(discriminant==0){                        System.out.println(The Root is:  + xroot1 + .); //Displays the root                        if (coeffA==0){                            System.out.println(*NaN stands for \Not a Number.\*);                        } //Ends If                    } //Ends If                    else if (discriminant>0){                        System.out.println(The Roots are:  + xroot1 +  and  + xroot2 +.); //Displays the roots                        if (coeffA==0){                            System.out.println(*NaN stands for \Not a Number.\*);                        } //Ends If                    } //Ends If                    System.out.println(Do you wish to perform another calculation?); //Asks the user if they would like to perform another calculation                    loop=sc.nextLine(); //Captures the next string input and stores it to the variable loop                } //Ends If            else if (loop.equals(n)){                System.out.println(Quit.); //Prints Quit                contloop=false; // Sets variable contloop to false thereby allowing therby allowing the program to exit the loop and finish            } //Ends If            else if ((!loop.equals(y)) || (!loop.equals(n))){                System.out.println(Please enter \y\ or \n\.); //Requests the user to enter a valid input                loop=sc.nextLine(); //Captures the next string input and stores it to the variable loop            } //Ends If        }while(contloop==true); //Ends Loop        System.exit(0); //Exits Java    } //End Try    catch (InputMismatchException e){        System.out.println(Invalid input format\nOnly numbers (decimals or intergers) are valid input formats.\nPlease restart program.);    } //End Catch} //End Main} //End Class
How does this quadratic formula program look?
java;beginner;mathematics;swing
I agree to everything, Matthew Flaschen wrote, but like to add some more things: You don't declare and initialize your variables with dummy values upfront. Instead, you restrict the scope as much as possible, and delay the initialization, if possible, to first usage. In your example: public static void main (String [] args){       boolean contloop = true;     String loop = y;     Scanner sc = new Scanner (System.in);     try {    do {        if (loop.equals (y)) {            System.out.println (Please enter the value for the first coefficient: );             double coeffA = sc.nextDouble ();             System.out.println (Please enter the value for the second coefficient: );             double coeffB = sc.nextDouble ();             System.out.println (Please enter the value for the third coefficient: );             double coeffC = sc.nextDouble ();             double discriminant = coeffB * coeffB - (4 * coeffA * coeffC);             double xroot1 = (-coeffB + (Math.sqrt (discriminant))) / (2 * coeffA);             double xroot2 = (-coeffB - (Math.sqrt (discriminant))) / (2 * coeffA); This makes reasoning about what the variable is used for, who is influenced by modifying it and refactoring much more easy. I would suggest much more white space between the tokens. It's better readable for the human eye. The machine will not mention it. Compare yourself: xroot1 = (-coeffB + (Math.sqrt (discriminant))) / (2 * coeffA); xroot1=(-coeffB+(Math.sqrt(discriminant)))/(2*coeffA); If you use comments, put them infront of what is commented, so that javadoc maps them correctly. Avoid System.exit(0); //Exits Java! Your code can't be called from another Java program, if you use it, because, as written in your comment, it doesn't just stop your program, but the whole JVM. But you will often see it in tutorials. In your case, a return; is all you need.Trying to refactor your code, so that we would have a main loop with 3 steps:loop     read input    calculation     format outputwhile user okwith 3 method calls, and a class for the values, I mentioned, that the control is surprisingly complicated for the task, which is only whether there should be another loop or not. But we have a variable loop, another one contloop. Can they be out of sync? Why do we have two?But the main aspect is, to split the task into 3 smaller parts. Think of the possibility to reuse the calculation in a webinterface, Where you don't use System.out.