Здравейте,
Имам проблем с решаването на задачата по- долу:
Sort Three Numbers
Description
Write a program that enters 3 real numbers and prints them sorted in descending order.
- Use nested
if
statements. - Don’t use arrays and the built-in sorting functionality.
Input
- On the first three lines, you will receive the three numbers, each on a separate line.
Output
- Output a single line on the console - the sorted numbers, separated by a white space
Това е моето решение, но ми дава някаква грешк и не мога да я разбера къде е проблема.
Благодаря предварително.
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input1 = scanner.nextLine();
int a = Integer.parseInt(input1);
String input2 = scanner.nextLine();
int b = Integer.parseInt(input2);
String input3 = scanner.nextLine();
int c = Integer.parseInt(input3);
if (a > b && a > c) {
System.out.print(a + " ");
if (b > c)
System.out.print(b + " " + c);
else
System.out.print(c + " " + b);
} else if (b > a && b > c) {
System.out.print(b + " ");
if (a > c)
System.out.print(a + " " + c);
else
System.out.print(c + " " + a);
} else if (c > b && c > a) {
System.out.print(c + " ");
if (a > c)
System.out.print(a + " " + c);
else
System.out.print(c + " " + a);
}
}
}