hi everybody,
im writing a program to output 2 vales for X for a quadratic equation. im not the best with loops however and i wana put a loop in the code so that the menu will continue to reappear once it has calculated the previous values and until the user selects the end option. any ideas for code???
my code so far is as follows:
import java.io.*;
class QuadraticApp {
// ----------------FIELDS--------------------
// Create BufferedReader class instance
public static InputStreamReader input =
new InputStreamReader(System.in);
public static BufferedReader keyboardInput = new
BufferedReader(input);
// ---------------METHODS--------------------
/* Main Method*/
public static void main(
String[] args) throws IOException {
int selector;
// Output menu
selector = outputMenu();
// Process selection
processSelector(selector);
}
/* Menu */
public static int outputMenu() throws IOException {
System.out.println(" USER MENU ");
System.out.println("======================");
System.out.println("1. Calculate Quadratic");
System.out.println("2. End ");
System.out.println("Please select option: ");
return(Integer.parseInt(keyboardInput.readLine()));
}
/* Process selector. If unrecognised selection output error and repeat. */
public static void processSelector(int selector) throws IOException {
switch (selector) {
case 1:
System.out.println ("Input a value (no less than -100 or greater than 100) for a, b & c:");
double a = Double.parseDouble(keyboardInput.readLine());
double b = Double.parseDouble(keyboardInput.readLine());
double c = Double.parseDouble(keyboardInput.readLine());
// Create Instance
QuadraticClass newQuadratic = new QuadraticClass(a, b, c);
// a
if (a > 100 || a < -100){
System.out.println("Cannot complete calculation, please enter values between -100 and 100");
}
else
if (b > 100 || b < -100){
System.out.println("Cannot complete calculation, please enter values between -100 and 100");
}
else
if (c > 100 || c < -100){
System.out.println("Cannot complete calculation, please enter values between -100 and 100");
}
else{
// Calculation
double trial = a + b;
if (trial == 0){
System.out.println("Cannot complete calculation, incorrect values entered");
}
}
// Discrimination
double disc = (Math.pow((double)b,2)-(4*a*c));
if (disc < 0){
System.out.println("Cannot complete calculation, values are incompatible");
}
else{
// Calculate Root1
newQuadratic.calcRoot1(a, b, c);
// Calculate Root2
newQuadratic.calcRoot2(a, b, c);
}
break;
case 2:
System.out.println (" ");
break;
default:
System.out.println("ERROR: Unrecognised menu selection " +
selector + "!");
}
}
}