Adblock breaks this site

Opinions

Discussion in 'Programming General' started by SuF, May 19, 2007.

  1. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    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
     
  2. aut0er

    aut0er Active Member

    Joined:
    Apr 28, 2005
    Posts:
    131
    Referrals:
    0
    Sythe Gold:
    0
    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);
    		}
    		}	
    					
    	             }
    
     
  3. dany_blackman

    dany_blackman Guest

    Referrals:
    0
    Opinions

    :) i like your style aut0er
     
< Gunz Armour Calculator(with premiums) | Help! >


 
 
Adblock breaks this site