'How to keep results on the same line in console in java?
I've been working on this assignment for my programming class where I have to write a program to solve a quadratic equation in the format of a^2+bx+c=0. I worked with a classmate to figure out how to solve the actual problem, but when I try to run it through our testing software it tells me I need the results in the console to all be on the same line.
I'll attach my code at the bottom, I'm a beginner and was pretty much guided through this by my classmate so I'm still trying to figure things out. Thanks so much in advance!
import java.util.Scanner;
// TODO: document this class
public class PA2a {
// TODO: document this function
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a b c: ");
double a,b,c, root_1, root_2;
a = input.nextDouble();
b = input.nextDouble();
c = input.nextDouble();
double discriminant = b * b - 4 * a * c;
if(discriminant > 0) {
root_1 = (-b + Math.sqrt(discriminant)) / (2 * a);
root_2 = (-b - Math.sqrt(discriminant)) / (2 * a);
if(root_1 < root_2)
System.out.printf("Roots: %.2f, %.2f", root_1, root_2);
else
System.out.printf("Roots: %.2f, %.2f", root_2, root_1);
}
else if (discriminant == 0) {
root_1 = root_2 = -b / (2*a);
System.out.printf("Root: %.2f", root_1);
}
else {
System.out.print("Roots: imaginiary");
}
input.close();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

