简体   繁体   English

数组Java上的最后一句话

[英]Last word/sentence on an Array Java

I have an assignment, it looks pretty easy however I cannot figure it out how to solve it. 我有一个作业,看起来很简单,但是我无法弄清楚如何解决它。

It says: 它说:

  • a) Ask the user: How many words/sentences do you want to write (at least 5) ? a)问用户:您要写多少个单词/句子(至少5个)? (Use while loop) (使用while循环)
  • b) Use for loop to make the user write the words/sentences b)使用for循环使用户写出单词/句子
  • c) After the user's written the words/sentences, output which word/sentence comes last alphabetically (using .compareTo() method ) c)用户写完单词/句子后,输出按字母顺序最后出现的单词/句子(使用.compareTo()方法)

This is what I came up with: 这是我想出的:

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));

}

}

I compiles and runs. 我编译并运行。 I have two problems: 我有两个问题:

  1. nextLine >>> it is skiping the first Sentence nextLine >>>它正在跳过第一个句子

  2. I don't know how to make the algorithm to calculate which word/sentence has the biggest value or, using the compareTo() method which word/sentence has the value > 0 compared to each and every other value on the array. 我不知道如何使算法来计算哪个单词/句子的值最大,或者使用compareTo()方法将哪个单词/句子的值与数组中的每个其他值相比都> 0。

Thank you. 谢谢。

Answer to Q1 : num = input.nextInt(); 对问题1的回答: num = input.nextInt(); takes a number as the input but doesn't also consume the new-line, and hence the nextLine consumes the empty new line ... you could use input.nextLine also to get the first number instead of num = input.nextInt(); 接受一个数字作为输入但不占用换行符,因此nextLine占用空的换行符...您也可以使用input.nextLine来获取第一个数字,而不是num = input.nextInt(); by reading a line, then parsing the int value as num = Integer.parseInt(input.nextLine()); 通过读取一行,然后将int值解析为num = Integer.parseInt(input.nextLine());

Answer to Q2 : 回答第二季度:

You re-set the value of last everytime but you don't compare the value of the next biggest candidate with the last before re-assigning last ... 您重新设置的值, last每次但你没有未来最大的候选值与比较last重新分配前年...

for example, look at the following : 例如,查看以下内容:

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;

}

it will solve your problem.... 它将解决您的问题。

    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]);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM