使用 Java 查找按字母顺序排列的第一个字符串
Find the alphabetically first String using Java
我的教授让我为这段代码创建一个解决方案,但我无法得到很好的输出。
他问了我几件事:
Ask the user to input a String value, the value "END", terminates the loop and the program and is not included in the calculation of Strings.
In the end, you should output which of the Strings that the user inputted is the String that comes alphabetically first. So for ex: if the user inputs "aac" and "aab", the "aab" comes first.
I have created the code, but at the end of the output it gives me the value "END" as the first alphabetically String for some reason, and I am kind of confused.
不知道教授问的对不对,还是只有我不懂!
到目前为止我的代码:
import java.util.Scanner;
public class Ush1_NOTDONE {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type a String value, the value \"END\" terminates the code");
String word = sc.nextLine();
final String SENTINEL = "END";
String firstString = null;
do {
if(!word.equals(SENTINEL)) {
System.out.println("Type a String value, the value \"END\" terminates the code");
word = sc.nextLine();
if(firstString == null || word.compareTo(firstString)<0 ){
firstString = word;
}
}}while(!(word.equals(SENTINEL)));
System.out.println("First alphabetically string is: " + firstString);
}
}
尽量避免这样做。相反,在 while 条件下进行赋值(assignment in java returns a value)。
public class HelloWorld{
public static void main(String []args){
String word;
Scanner sc = new Scanner(System.in);
String first = null;
while(!(word = sc.nextLine()).equals("END")) {
if(first == null || word.compareTo(first) < 0) {
first = word;
}
}
System.out.println(first);
}
}
我的教授让我为这段代码创建一个解决方案,但我无法得到很好的输出。 他问了我几件事:
Ask the user to input a String value, the value "END", terminates the loop and the program and is not included in the calculation of Strings.
In the end, you should output which of the Strings that the user inputted is the String that comes alphabetically first. So for ex: if the user inputs "aac" and "aab", the "aab" comes first.
I have created the code, but at the end of the output it gives me the value "END" as the first alphabetically String for some reason, and I am kind of confused.
不知道教授问的对不对,还是只有我不懂!
到目前为止我的代码:
import java.util.Scanner;
public class Ush1_NOTDONE {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type a String value, the value \"END\" terminates the code");
String word = sc.nextLine();
final String SENTINEL = "END";
String firstString = null;
do {
if(!word.equals(SENTINEL)) {
System.out.println("Type a String value, the value \"END\" terminates the code");
word = sc.nextLine();
if(firstString == null || word.compareTo(firstString)<0 ){
firstString = word;
}
}}while(!(word.equals(SENTINEL)));
System.out.println("First alphabetically string is: " + firstString);
}
}
尽量避免这样做。相反,在 while 条件下进行赋值(assignment in java returns a value)。
public class HelloWorld{
public static void main(String []args){
String word;
Scanner sc = new Scanner(System.in);
String first = null;
while(!(word = sc.nextLine()).equals("END")) {
if(first == null || word.compareTo(first) < 0) {
first = word;
}
}
System.out.println(first);
}
}