问题
I have a project for class were I need to get 4 different strings inputted and then output them in alphabetical order.
So far I have this:
String wd1, wd2, wd3, wd4;
Scanner scan1 = new Scanner(System.in);
System.out.println ("Type Word One: ");
wd1 = scan1.next();
System.out.println ("Type Word Two: ");
wd2 = scan1.next();
System.out.println ("Type Word Three: ");
wd3 = scan1.next();
System.out.println ("Type Word Four: ");
wd4 = scan1.next();
I know that I can get the alphabetical order of 2 strings by using:
int compare = wd1.compareTo(wd2);
if (compare < 0)
{System.out.println(wd1 + " " + wd2);}
else {
if (compare > 0)
{System.out.println(wd2+ " " + wd1);}
I need help getting all 4 of the strings in the proper order. I am supposed to be using if else statements and not arrays to do this.
Any help would be great!
Thanks
回答1:
boolean swapped = false;
do {
swapped = false;
if (w2.compareTo(w1) < 0) {
String tmp = w2;
w2 = w1;
w1 = tmp;
swapped = true;
}
if (w3.compareTo(w2) < 0) {
String tmp = w3;
w3 = w2;
w2 = tmp;
swapped = true;
}
if (w4.compareTo(w3) < 0) {
String tmp = w4;
w4 = w3;
w3 = tmp;
swapped = true;
}
} while (swapped)
System.out.println(w1);
System.out.println(w2);
System.out.println(w3);
System.out.println(w4);
回答2:
The easiest way to do what you are looking for is to put them in a list, then use list.sort() to put them in order.
回答3:
How about putting the Strings in a List
and call the sort()
method?
The Java String class implements the 'Comparable' interface and thus already has the compareTo()
method, which should compare the strings in order.
回答4:
You need to compare first string with the rest three to find which is alphabetically first. after that you need to compare among the rest 3 and so on.
回答5:
Since you are unable to use arrays or any other data structure and a sorting algorithm, you will need to do this manually with several if
statements. Before you try to write any code, I suggest you try doing this by hand. If I gave you four words, how would you determine which is the first one in alphabetical order? The second? Keep going; if you can describe how you do this in words, translating into Java should be straight-forward.
Edit:
Don't worry about ASCII code or anything computer related at the moment. I guess you do need to consider the detail that compareTo()
only lets you compare two words at a time, though. So let's say you pick wd1
and wd2
to compare. When you compare these two "words", what are the possible outcomes? In each case, what will you do next?
Another Edit:
After discussing in comments, you can see that the algorithm here is something like this (in pseudocode):
if wd1 comes before wd2
if wd1 comes before wd3
if wd1 comes before wd4
print out wd1 // wd1 is the first word in alphabetical order
else
print out wd4 // wd4 is the first word in alphabetical order
else
// Details left as an exercise to the reader
else
// Details left as an exercise to the reader
Finish filling in the else statements first in English following the same pattern illustrated here. When you have this finished, writing the code in Java should be pretty simple.
As you can see "understanding the concept" is often not enough to get you started writubg the code. You need to take the time figuring out all the excruciating details. Often I find it helps writing out the steps in English using pen and paper (or maybe a word processor) before I even start writing code. (Of course, you can use your own native language if that's easier. The point is to NOT jump on the computer and start writing Java code from the begginning, especially when you are stuck.)
When you can describe the steps in your native language, then translating into Java becomes easier. Sometimes it is trivial. Other times, you run into a detail that wasn't considered in your natural-language description. Then you back up away from the Java and fix the description before continuing with the Java.
This is basically the process I use when I'm trying to write a computer program. I hope you can adapt some of these ideas to your own coding.
回答6:
Typical divide and concur strategy could be applied. Think about merge sort, sort first 2 and last 2 strings, after that merge results.
if(s2<s1) swap(s1,s2)
if(s4<s3) swap(s3,s4)
if(s1<s3) {
print(s1)
if(s2<s3){
print(s2)
print(s3)
print(s4)
} else {
print(s3)
if(s2<s4){
print(s2)
print(s4)
} else {
print(s4)
print(s2)
}
}
} else {
print(s3)
if(s4<s1){
print(s4)
print(s1)
print(s2)
} else {
print(s1)
if(s4<s2){
print(s4)
print(s2)
} else {
print(s2)
print(s4)
}
}
}
回答7:
For comparing more than 2 strings you should put the strings in an array and then run them through a sorting method
public class SortLetters2 {
public String[] sort(String[] asd) {
String[] sorted = asd.clone();
for (int i = 0; i < sorted.length; i++) {
for (int j = i + 1; j < sorted.length; j++) {
int compare = sorted[i].compareTo(sorted[j]);
if ((compare > 0) && (i != j)) {
//compare two strings
String temp = sorted[j];
sorted[j] = sorted[i];
sorted[i] = temp;
}
}
}
return sorted;
}
public static void main(String[] args) {
SortLetters2 list1 = new SortLetters2();
//SortLetters2 is the class name
Scanner scan1 = new Scanner(System.in);
String wd1, wd2, wd3, wd4;
System.out.println("Type Word One: ");
wd1 = scan1.next();
System.out.println("Type Word Two: ");
wd2 = scan1.next();
System.out.println("Type Word Three: ");
wd3 = scan1.next();
System.out.println("Type Word Four: ");
wd4 = scan1.next();
String array[] = {wd1, wd2, wd3, wd4};
//set array equal to the inputs
String[] sortedArray = list1.sort(array);
for (int i = 0; i < sortedArray.length; i++) {
if (i == sortedArray.length - 1) {
System.out.println(sortedArray[i]);
} else {
System.out.print(sortedArray[i] + ",");
}
}
//run sorting program
}
}
来源:https://stackoverflow.com/questions/12681103/java-alphabetizing-strings