A couple of issues with my quadratic equation solver.
Code :
import java.util.Scanner;
public class Andregradsformel {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Double a;
Double b;
Double c;
System.out.println("This program solves quadratic equations.");
System.out.println("Use integers.");
System.out.println("Enter a: ");
a = in.nextDouble();
System.out.println("Enter b: ");
b = in.nextDouble();
System.out.println("Enter c: ");
c = in.nextDouble();
System.out.println("First solution:");
System.out.println((-b+Math.sqrt(b*b-4*a*c))/2*a);
System.out.println("Second solution:");
System.out.println((-b-Math.sqrt(b*b-4*a*c))/2*a);
}
}
While it works, I have two issues with it:
I have come to understand that one can use decimal numbers while working with doubles. How come my program does not accept that as the user input?
Furthermore, once the program is finished, I want the program to run again. I have looked up some tutorials on loops, but I do not understand how I would go about implementing it. Thanks.
EDIT: Tried changing from Double to Float. Still works with integers, but decimalnumbers are still defect.
EDIT2: For some reason, user-input has to entered as "3,4", not "3.4". Solved.
Re: A couple of issues with my quadratic equation solver.
Quote:
Originally Posted by
Zaphod_b
It's a Locale thing.
What do you get if you put the following statement at the beginning of main()?
Code :
System.out.println("Locale: " + Locale.getDefault().getDisplayName());
If you want all of your input and output floating point numbers to use '.' decimal points instead of commas, you can try the following at the beginning of the main() function:
Code :
public static void main(String [] args) {
Locale.setDefault(Locale.US);
.
.
.
Note that, depending on your Locale, this can affect lots of other things, not just the "decimal point."
If you want the input to use the '.' instead of ',' for the "decimal point" but have the output print "normally" for your Locale, you can try the following:
Code :
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
in.useLocale(Locale.US);
References:
Locale Class for Locale.getDefault(), getDisplayName(), setDefault()
and
Scanner Class for useLocale()
Cheers!
Z
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Locale cannot be resolved to a variable
Re: A couple of issues with my quadratic equation solver.
Quote:
Originally Posted by
EatMyBible
...
EDIT2: For some reason, user-input has to entered as "3,4", not "3.4". Solved.
It's a Locale thing.
What do you get if you put the following statement at the beginning of main()?
Code java:
System.out.println("Locale: " + Locale.getDefault().getDisplayName());
If you want all of your input and output floating point numbers to use '.' decimal points instead of commas, you can try the following at the beginning of the main() function:
Note that, depending on your Locale, this can affect lots of other things, not just the "decimal point."
If you want the input to use the '.' instead of ',' for the "decimal point" but have the output print "normally" for your Locale, you can try the following:
Code java:
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
in.useLocale(Locale.US);
References:
Locale Class for Locale.getDefault(), getDisplayName(), setDefault()
and
Scanner Class for useLocale()
Quote:
Originally Posted by
EatMyBible
...once the program is finished, I want the program to run again....
One way is to make an "infinite loop." Something like this:
Code java:
System.out.println("This program solves quadratic equations.");
while(true) {
System.out.print("Enter a: ");
Double a = in.nextDouble();
.
. // Read other inputs and calculate and print outputs
.
.
System.out.println("\nNext problem:");
}
The code will run until you enter something other than a numerical quantity for a coefficient, in which case it will unceremoniously crash with an InputMismatchException.
You can catch the exception and use that as a condition for breaking out of the loop, or you could make a special case of user inputs (all zeros, for example) that would break out of the loop so that the program can terminate more gracefully.
Maybe something like:
Code java:
public static void main(String [] args) {
.
.
.
System.out.println("This program solves quadratic equations.");
boolean fini = false;
while(!fini) {
.
. // Code to get values of a, b, and c
.
System.out.printf("\nYou entered %f %f %f\n\n", a, b, c); // For debugging: Show what the program is seeing
if (/* Code to test value of a and b and c to see if they are all equal to zero )*/ {
fini = true;
}
else {
// Code to calculate and print output values.
} // End if if..else.. statement for all zeros
} // End of if(!fini) loop
System.out.println("Goodbye for now.");
Cheers!
Z
Re: A couple of issues with my quadratic equation solver.
Quote:
Originally Posted by
EatMyBible
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Locale cannot be resolved to a variable
Did you read the Locale Class reference that I linked?
You have to import java.util.Locale to use it.
In fact, instead of importing java.util.Scanner and java.util.Locale separately, the way this program uses things, you can just do this:
Code java:
import java.util.*;
public class Whatever {
Cheers!
Z