• Log in or sign up for an account here: https://www.sythe.org/login

Opinions

SuF

Legend
Joined
Jan 21, 2007
Sythe Gold
1,234
Messages
14,211
Opinions
I have made a concel based program that does the quadratic formula for you and its works great (jiust look that up in an algabra book) i just want to know if there is anything i can do to make this program better i would like to here from some others :)

Code:
import java.util.*;


public class math 
{
static boolean useAgain = true;	
static Scanner sc = new Scanner(System.in);
static boolean validInput = true;
		
public static void main(String[] args) 
{
System.out.println("Welcome to The Quadtratic Formula Solver!");	
System.out.println("Remeber Your Negivtive Signs!");	
			
		
while(useAgain)
{
quadraticFormula();
System.out.print("Would you like to use this program again? ('Y' or 'N'):");
	do
	{
	String answer;	
	answer = sc.next();
	validInput = true;
	if (answer.equalsIgnoreCase("N"))
	useAgain = false;
	else if (answer.equalsIgnoreCase("Y"))
		;
		else 
	{	validInput = false;
	System.out.println("Please Enter 'Y' or 'N'");
	}
				
	}while(!validInput);
				
	}
	System.out.println("");
System.out.println("Thank You For Using The Quadtratic Formula Solver!");
}
		
		
	public static void quadraticFormula()
	{
	double a;
	double b;
	double c;	
	System.out.print("Enter A:");
	a = GetA();
	System.out.print("Enter B:");
	b = GetB();
	System.out.print("Enter C:");
	c = GetC();
	double d = 4 * a * c;
	System.out.println("4 * " + a + " * " + c + " is " + d);
double e = (b * b);
	double ee = e - d;
	System.out.println(b + " * " + b + " is " + e);
	System.out.println(e + " - " + d + " is " + ee);
	double f = 2 * a;
	System.out.println("2 * " + a + " is " + f);
	double g = Math.sqrt(ee);
	System.out.println(ee + "s Squareroot is " + g);
	double h = -b + g;
	System.out.println(-b + " + " + g + " is " + h);
	double i = h / f;
	double j = -b - g;
	System.out.println(-b + " - " + g + " is " + j);
	double k = j / f;
	System.out.println("The + Number is " + i);
	System.out.println("The - Number is " + k);
	}
		

	public static double GetA()
	{
		while (true)
	{
		try
		{
			return sc.nextDouble();
		}	
	catch (InputMismatchException e)
		{
		sc.next();
		System.out.print("Enter A:");
		}	}
				
			}
					
		public static double GetB()
	{
		while (true)
	{
		try
		{
		return sc.nextDouble();
		}	
		catch (InputMismatchException e)
		{
		sc.next();
		System.out.print("Enter B:");
		}
		}	
					
	             }
		public static double GetC()
		{
while (true)
		{
		try
		{
	return sc.nextDouble();
	}
             catch (InputMismatchException e)
		{
	sc.next();
	System.out.print("Enter C:");
	}
	
	}
					
}
	
	}

there it is i would love some others ideas thanks



SuF
 
Opinions
look up JOptionPane to make your input look more professional ... also , your getA , getB and getC repeat alot of code ... do something like this ... it would be much more efficient:
Code:
//in quadratic()
             double a;
	double b;
	double c;	
	a = getLetter("Enter A:");
	b = getLetter("Enter B:");
	c = getLetter("Enter C:");

// new method called getLetter
public double getLetter(String a)
{
		while (true)
	{
		try
		{
		return sc.nextDouble();
		}	
		catch (InputMismatchException e)
		{
		sc.next();
		System.out.print(a);
		}
		}	
					
	             }
 
Runescape Gamblingrunescape gambling
Back
Top