比较字符串(用户键入他想要的任意多个),然后使用 for 循环判断哪个是第一个(基于哪个第一个字母先出现)
Comparing Strings ( the user types as many as he wants) and then telling which one is first (based on which first letter comes first) using for loop
首先我必须要求用户输入一个数字。该数字将决定他必须输入多少个句子(应该 >2 ),然后我必须比较这些句子。我必须告诉哪个先来(基于字母顺序)。这是我到目前为止所做的一切。我不知道如何比较字符串,因为我不知道用户将键入多少字符串并且没有它们的名称。
import java.util.*;
public class Sentences {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = 0;
while (number < 2) {
System.out.println("Type a number > 2");
number = scan.nextInt();
}
scan.nextLine();
String sentence;
int y = 0;
for (int i = 0; i < number; i++) {
System.out.println("Type a sentence");
sentence = scan.nextLine();
}
}
}
你很接近。您需要一个数组 (sentences[]
) 而不是单个变量 (sentence
),如下所示:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = 0;
while (number < 2) {
System.out.print("Type a number > 2: ");
number = Integer.parseInt(scan.nextLine());
}
String[] sentences = new String[number];
int y = 0;
for (int i = 0; i < number; i++) {
System.out.print("Type a sentence: ");
sentences[i] = scan.nextLine();
}
Arrays.sort(sentences);
for (String sentence : sentences) {
System.out.println(sentence);
}
}
}
要对String sentences[]
进行排序,您需要使用Arrays.sort(sentences)
,如上所示。
样本运行:
Type a number > 2: 0
Type a number > 2: 3
Type a sentence: Hello world
Type a sentence: You are awesome
Type a sentence: My name is Jane
Hello world
My name is Jane
You are awesome
[更新]
根据您的说明,您只想打印按字母顺序排列最低的一个句子。这就像跟踪数字列表中的最小数字。
算法如下:
- 将第一个输入存储到变量,例如
lowestAlphabetical
。
- 在循环中,将
lowestAlphabetical
的值与下一个输入进行比较,如果下一个输入的字母顺序较低,则将下一个输入放入 lowestAlphabetical
。
演示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = 0;
while (number < 2) {
System.out.print("Type a number > 2: ");
number = Integer.parseInt(scan.nextLine());
}
// Scan the first sentence
System.out.print("Type a sentence: ");
String sentence = scan.nextLine();
// Since we have only one sentence till now, it is also the lowest in
// alphabetical order
String lowestAlphabetical = sentence;
// Loop for next inputs
for (int i = 1; i < number; i++) {
System.out.print("Type the next sentence: ");
sentence = scan.nextLine();
if (sentence.compareTo(lowestAlphabetical) < 0) {
lowestAlphabetical = sentence;
}
}
System.out.println(lowestAlphabetical);
}
}
样本运行:
Type a number > 2: 3
Type a sentence: Hello world
Type the next sentence: Good morning
Type the next sentence: My name is Jane
Good morning
首先我必须要求用户输入一个数字。该数字将决定他必须输入多少个句子(应该 >2 ),然后我必须比较这些句子。我必须告诉哪个先来(基于字母顺序)。这是我到目前为止所做的一切。我不知道如何比较字符串,因为我不知道用户将键入多少字符串并且没有它们的名称。
import java.util.*;
public class Sentences {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = 0;
while (number < 2) {
System.out.println("Type a number > 2");
number = scan.nextInt();
}
scan.nextLine();
String sentence;
int y = 0;
for (int i = 0; i < number; i++) {
System.out.println("Type a sentence");
sentence = scan.nextLine();
}
}
}
你很接近。您需要一个数组 (sentences[]
) 而不是单个变量 (sentence
),如下所示:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = 0;
while (number < 2) {
System.out.print("Type a number > 2: ");
number = Integer.parseInt(scan.nextLine());
}
String[] sentences = new String[number];
int y = 0;
for (int i = 0; i < number; i++) {
System.out.print("Type a sentence: ");
sentences[i] = scan.nextLine();
}
Arrays.sort(sentences);
for (String sentence : sentences) {
System.out.println(sentence);
}
}
}
要对String sentences[]
进行排序,您需要使用Arrays.sort(sentences)
,如上所示。
样本运行:
Type a number > 2: 0
Type a number > 2: 3
Type a sentence: Hello world
Type a sentence: You are awesome
Type a sentence: My name is Jane
Hello world
My name is Jane
You are awesome
[更新]
根据您的说明,您只想打印按字母顺序排列最低的一个句子。这就像跟踪数字列表中的最小数字。
算法如下:
- 将第一个输入存储到变量,例如
lowestAlphabetical
。 - 在循环中,将
lowestAlphabetical
的值与下一个输入进行比较,如果下一个输入的字母顺序较低,则将下一个输入放入lowestAlphabetical
。
演示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = 0;
while (number < 2) {
System.out.print("Type a number > 2: ");
number = Integer.parseInt(scan.nextLine());
}
// Scan the first sentence
System.out.print("Type a sentence: ");
String sentence = scan.nextLine();
// Since we have only one sentence till now, it is also the lowest in
// alphabetical order
String lowestAlphabetical = sentence;
// Loop for next inputs
for (int i = 1; i < number; i++) {
System.out.print("Type the next sentence: ");
sentence = scan.nextLine();
if (sentence.compareTo(lowestAlphabetical) < 0) {
lowestAlphabetical = sentence;
}
}
System.out.println(lowestAlphabetical);
}
}
样本运行:
Type a number > 2: 3
Type a sentence: Hello world
Type the next sentence: Good morning
Type the next sentence: My name is Jane
Good morning