数组 Java 上的最后一个 word/sentence
Last word/sentence on an Array Java
我有一个作业,看起来很简单,但我不知道如何解决它。
它说:
- a) 问用户:你想写多少words/sentences(at
至少 5) ? (使用while循环)
- b) 使用 for 循环让用户编写 words/sentences
- c) 用户写完words/sentences后,输出
word/sentence 按字母顺序排在最后(使用 .compareTo() 方法)
这是我想出的:
import java.util.Scanner;
import java.lang.String;
import java.util.Arrays;
public class LastString {
public static void main (String [] args){
Scanner input = new Scanner (System.in);
final short MIN_NUM = 2;
int num = 0;
int count = 0;
String [] sentence = new String [0];
String last = "";
while (num < MIN_NUM){
System.out.println("How many words/sentences do you want to put? " + "\t\t\t\t\t\t\t\t --- at least " + MIN_NUM);
num = input.nextInt();
sentence = new String [num];
}
for (int i = 0; i < num ; i++ ) {
System.out.println("\nWrite a word/sentence" + "\t\t\t\t\t\t\t\t\t --- (Time: " + (i+1) + " )");
sentence [i] = input.nextLine();
System.out.println("The word/sentence is: " + sentence[i]);
}
int i = 0;
int max;
for (i=0;i<num-1 ;i++ ) {
if(sentence[i].compareTo(sentence[i+1]) > 0){
last = sentence[i];
count ++;
}else if (sentence[i].compareTo(sentence[i+1]) < 0) {
last = sentence[i+1];
count++;
}
}
System.out.println("\n\n------------" +
"\nLast word/sentence is: " + last);
System.out.println(Arrays.toString(sentence));
}
}
我编译运行。我有两个问题:
nextLine >>> 跳过第一句
我不知道如何让算法计算哪个 word/sentence 具有最大值,或者使用 compareTo() 方法 word/sentence 具有值> 0 与数组中的每个其他值相比。
谢谢。
Q1 的答案:num = input.nextInt();
将一个数字作为输入,但不消耗 new-line,因此 nextLine
消耗空的新行......你也可以使用 input.nextLine
来获取第一个数字而不是 num = input.nextInt();
通过读取一行,然后将 int 值解析为 num = Integer.parseInt(input.nextLine());
问题 2 的答案:
你每次 re-set last
的值,但你不会将下一个最大候选值与上次 re-assigning 之前的 last
的值进行比较...
例如,请看以下内容:
for (int i = 0; i < num - 1; i++) {
String thisLast = "";
if (sentence[i].compareTo(sentence[i + 1]) > 0) {
thisLast = sentence[i];
count++;
} else if (sentence[i].compareTo(sentence[i + 1]) < 0) {
thisLast = sentence[i + 1];
count++;
}
if (thisLast.compareTo(last) > 0)
last = thisLast;
}
它将解决您的问题....
int count = 0;
String [] sentence = new String[6];
String last = "";
for (int i = 0; i < num ; i++ ) {
System.out.println("\nWrite a word/sentence" + "\t\t\t\t\t\t\t\t\t --- (Time: " + (i+1) + " )");
sentence [i] = input.nextLine();
count++;
if(count >= 2){
if(sentence[i].compareTo(last) > 0){
last = sentence [i] ;
}
}else{
last = sentence [i];
}
System.out.println("The word/sentence is: " + sentence[i]);
}
我有一个作业,看起来很简单,但我不知道如何解决它。
它说:
- a) 问用户:你想写多少words/sentences(at 至少 5) ? (使用while循环)
- b) 使用 for 循环让用户编写 words/sentences
- c) 用户写完words/sentences后,输出 word/sentence 按字母顺序排在最后(使用 .compareTo() 方法)
这是我想出的:
import java.util.Scanner;
import java.lang.String;
import java.util.Arrays;
public class LastString {
public static void main (String [] args){
Scanner input = new Scanner (System.in);
final short MIN_NUM = 2;
int num = 0;
int count = 0;
String [] sentence = new String [0];
String last = "";
while (num < MIN_NUM){
System.out.println("How many words/sentences do you want to put? " + "\t\t\t\t\t\t\t\t --- at least " + MIN_NUM);
num = input.nextInt();
sentence = new String [num];
}
for (int i = 0; i < num ; i++ ) {
System.out.println("\nWrite a word/sentence" + "\t\t\t\t\t\t\t\t\t --- (Time: " + (i+1) + " )");
sentence [i] = input.nextLine();
System.out.println("The word/sentence is: " + sentence[i]);
}
int i = 0;
int max;
for (i=0;i<num-1 ;i++ ) {
if(sentence[i].compareTo(sentence[i+1]) > 0){
last = sentence[i];
count ++;
}else if (sentence[i].compareTo(sentence[i+1]) < 0) {
last = sentence[i+1];
count++;
}
}
System.out.println("\n\n------------" +
"\nLast word/sentence is: " + last);
System.out.println(Arrays.toString(sentence));
}
}
我编译运行。我有两个问题:
nextLine >>> 跳过第一句
我不知道如何让算法计算哪个 word/sentence 具有最大值,或者使用 compareTo() 方法 word/sentence 具有值> 0 与数组中的每个其他值相比。
谢谢。
Q1 的答案:num = input.nextInt();
将一个数字作为输入,但不消耗 new-line,因此 nextLine
消耗空的新行......你也可以使用 input.nextLine
来获取第一个数字而不是 num = input.nextInt();
通过读取一行,然后将 int 值解析为 num = Integer.parseInt(input.nextLine());
问题 2 的答案:
你每次 re-set last
的值,但你不会将下一个最大候选值与上次 re-assigning 之前的 last
的值进行比较...
例如,请看以下内容:
for (int i = 0; i < num - 1; i++) {
String thisLast = "";
if (sentence[i].compareTo(sentence[i + 1]) > 0) {
thisLast = sentence[i];
count++;
} else if (sentence[i].compareTo(sentence[i + 1]) < 0) {
thisLast = sentence[i + 1];
count++;
}
if (thisLast.compareTo(last) > 0)
last = thisLast;
}
它将解决您的问题....
int count = 0;
String [] sentence = new String[6];
String last = "";
for (int i = 0; i < num ; i++ ) {
System.out.println("\nWrite a word/sentence" + "\t\t\t\t\t\t\t\t\t --- (Time: " + (i+1) + " )");
sentence [i] = input.nextLine();
count++;
if(count >= 2){
if(sentence[i].compareTo(last) > 0){
last = sentence [i] ;
}
}else{
last = sentence [i];
}
System.out.println("The word/sentence is: " + sentence[i]);
}