gpt4 book ai didi

java - 提示用户输入然后按字母顺序排序?

转载 作者:太空宇宙 更新时间:2023-11-04 07:04:11 25 4
gpt4 key购买 nike

我目前正在上第一学期。我有一个项目要求我构建一个程序,让用户输入 3 个单词,按字母顺序对它们进行排序并输出中间的单词。我做了一些搜索,似乎只返回了对 2 个单词进行排序的结果。到目前为止,我有代码来获取用户输入,但我完全不知道如何按字母顺序对它们进行排序以及如何提示用户输入三个字符串。请耐心等待我,因为我对编程很陌生。如果有人能给我提供任何建议或最好或最简单的方法来对这些进行排序,我将不胜感激

import java.util.Scanner; //The Scanner is in the java.util package.

public class MiddleString {
public static void main(String [] args){
Scanner input = new Scanner(System.in); //Create a Scanner object.

String str1, str2, str3;

System.out.println("Please enter one word words : "); //Prompt user to enter one word
str1=input.next(); //Sets "str1" = to first word.
str2=input.next(); //Sets "str2" = to second word.
str3=input.next(); //Sets "str3" = to third word.



System.out.println("The middle word is " ); // Outputs the middle word in alphabetical order.

}
}

请帮忙!

最佳答案

尝试这样的事情:

String [] strings;    
int i = 0;
System.out.println("Please enter one word words : "); //Prompt user to enter one word
strings[i++] = input.next(); //Sets "str1" = to first word.
strings[i++] = input.next(); //Sets "str2" = to second word.
strings[i++] = input.next(); //Sets "str3" = to third word.
Arrays.sort(strings);
System.out.println("The middle word is " + strings[strings.length / 2]);

关于java - 提示用户输入然后按字母顺序排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21651547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com