Skip to content

Commit 5413bf4

Browse files
Sorted Names
1 parent afd8d68 commit 5413bf4

File tree

3 files changed

+130
-29
lines changed

3 files changed

+130
-29
lines changed

Programming-Challenges/Decision-Structures/.idea/workspace.xml

Lines changed: 47 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Write a program that asks the user to enter three names, and then displays the names sorted in ascending order.
3+
For example,
4+
If the user entered “Charlie”, “Leslie”, and “Andy”, the program would display:
5+
6+
Andy
7+
Charlie
8+
Leslie
9+
*/
10+
11+
package com.challenges;
12+
13+
import java.util.Scanner;
14+
15+
public class SortedNames {
16+
public static void main(String [] args) {
17+
18+
// Declare Variables
19+
20+
String nameOne;
21+
String nameTwo;
22+
String nameThree;
23+
24+
Scanner scanner = new Scanner(System.in);
25+
26+
System.out.println("Please enter the first name: ");
27+
nameOne = scanner.nextLine();
28+
29+
System.out.println("Please enter the second name: ");
30+
nameTwo = scanner.nextLine();
31+
32+
System.out.println("Please enter the third name: ");
33+
nameThree = scanner.nextLine();
34+
35+
if(nameOne.compareToIgnoreCase(nameTwo) < 0 && nameOne.compareToIgnoreCase(nameThree) < 0) {
36+
37+
System.out.println(nameOne);
38+
39+
if(nameTwo.compareToIgnoreCase(nameThree) < 0) {
40+
41+
System.out.println(nameTwo);
42+
System.out.println(nameThree);
43+
}
44+
else {
45+
46+
System.out.println(nameThree);
47+
System.out.println(nameTwo);
48+
}
49+
}
50+
51+
else if(nameTwo.compareToIgnoreCase(nameOne) < 0 && nameTwo.compareToIgnoreCase(nameThree) < 0) {
52+
53+
System.out.println(nameTwo);
54+
55+
if(nameOne.compareToIgnoreCase(nameThree) < 0) {
56+
57+
System.out.println(nameOne);
58+
System.out.println(nameThree);
59+
}
60+
else {
61+
62+
System.out.println(nameThree);
63+
System.out.println(nameOne);
64+
}
65+
}
66+
67+
else {
68+
69+
System.out.println(nameThree);
70+
71+
if(nameOne.compareToIgnoreCase(nameTwo) < 0) {
72+
73+
System.out.println(nameOne);
74+
System.out.println(nameTwo);
75+
}
76+
else {
77+
78+
System.out.println(nameTwo);
79+
System.out.println(nameOne);
80+
}
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)