简体   繁体   English

使用compareTo(); 方法似乎不起作用

[英]Using the compareTo(); method doesn't seem to be working

I'm new to Java and am working through some problems. 我是Java新手,正在解决一些问题。 I'm stuck on a question that asks me to "Write a program to input ten words and then display the words that are first and last in alphabetical order". 我遇到了一个问题,要求我“编写一个程序以输入十个单词,然后按字母顺序显示第一个和最后一个单词”。 The question is ambiguous. 这个问题是模棱两可的。 It could mean put all the input words into alphabetical order and display the first and last of these (harder) or display the first and last inputted word in alphabetical order (easier). 这可能意味着将所有输入的单词按字母顺序排列并显示这些单词的第一个和最后一个(较难显示),或者按字母顺序显示第一个和最后输入的单词(较容易)。 I wrote the following code: 我写了以下代码:

import java.util.Scanner;

public class Alphabetical {

public static void main(String[] args) {

    String[] s = new String[10];
    for (int i = 0; i < 10; i++) {
        System.out.println("Enter word");
        Scanner ins = new Scanner(System.in);

        s[i] = ins.nextLine().toLowerCase();
    }

    int result = s[0].compareTo(s[10]);

    if (result < 0) {
        System.out.println(s[0]);
        System.out.println(s[10]);
    }
    else if(result>0){
        System.out.println(s[10]);
        System.out.println(s[0]);
    }
    else{
        System.out.println("Words are identical so cannot be placed in alphabetical order");
        }
    }

}

But I'm getting an out of bounds exception where the compareTo method is placed and I'm not sure why. 但是我在放置compareTo方法时遇到了一个异常,我不确定为什么。 If anybody could help that would be great. 如果有人可以帮助,那就太好了。 If anyone could help with the harder version of the question too, that would be even better. 如果有人也可以为这个难题提供帮助,那就更好了。

new String[10] creates an array of 10 elements. new String[10]创建一个包含10个元素的数组。

s[10] is the 11th element of the array since elements begin with 0. So you have to treat s[9] as your last element. s[10]是数组的第11个元素,因为元素以0开头。因此,您必须将s[9]当作最后一个元素。 compareTo is not your problem compareTo不是你的问题

Because your "s" array have 10 elements. 因为您的“ s”数组有10个元素。 You cahange your code 您更改代码

import java.util.Scanner; 导入java.util.Scanner;

public class Alphabetical { 公共课程按字母顺序{

public static void main(String[] args) { 公共静态void main(String [] args){

String[] s = new String[10];
for (int i = 0; i < 10; i++) {
    System.out.println("Enter word");
    Scanner ins = new Scanner(System.in);

    s[i] = ins.nextLine().toLowerCase();
}

int result = s[0].compareTo(s[9]);

if (result < 0) {
    System.out.println(s[0]);
    System.out.println(s[9]);
}
else if(result>0){
    System.out.println(s[9]);
    System.out.println(s[0]);
}
else{
    System.out.println("Words are identical so cannot be placed in alphabetical order");
    }
}

} }

The reason you're getting the error about the array is because arrays start with index 0 and go up to one less than the size of the array. 您收到有关数组的错误的原因是因为数组以索引0开头,并且比数组的大小小1。 So on an array with a size of 10, the highest index is 9. Did you try searching the exception online before posting this question? 因此,在大小为10的数组上,最高索引为9。在发布此问题之前,是否尝试过在线搜索异常?

What the question means, you'll have to clarify with the person who gave you the assignment/problem. 这个问题是什么意思,您必须与给您任务/问题的人澄清。

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

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