尝试让java按字母顺序排列城市。不会工作,我';我是java新手

尝试让java按字母顺序排列城市。不会工作,我';我是java新手,java,Java,我试图让扫描仪扫描每个城市,分析第一个字符并按字母顺序排列。我不知道如何在这个任务中使用for语句,我在谷歌上搜索了很多,但现在我认为这是正确的选择。除非If-Else即使在正确的条件下也能工作,否则它仍然运行Else。如果我可以使用For(),我将如何使用它,如果不能,我将如何使它工作 对于样本城市,我使用了“奥斯汀[输入]芝加哥[输入]丹佛[输入]” 编辑:让我澄清一下,我添加了else的“Error”部分来测试If参数 package homework2; import java

我试图让扫描仪扫描每个城市,分析第一个字符并按字母顺序排列。我不知道如何在这个任务中使用for语句,我在谷歌上搜索了很多,但现在我认为这是正确的选择。除非If-Else即使在正确的条件下也能工作,否则它仍然运行Else。如果我可以使用For(),我将如何使用它,如果不能,我将如何使它工作

对于样本城市,我使用了“奥斯汀[输入]芝加哥[输入]丹佛[输入]”

编辑:让我澄清一下,我添加了else的“Error”部分来测试If参数

package homework2;

    import java.util.Scanner;

    public class OrderCities {

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

    // Prompt user to input cities
    System.out.println("Please enter each city");
    String c1 = scanner.nextLine();
    String c2 = scanner.nextLine();
    String c3 = scanner.nextLine();

    int first = 0;
    int i1 = c1.charAt(first);
    int i2 = c2.charAt(first);
    int i3 = c3.charAt(first);

    if ((i1 > i2 && i1 > i3) && i2 > i3){
        System.out.println(c1 + " " + c2 + " " + c3);
    } else
        System.out.println("Error");



        }

    }
因此,我的教授告诉我它是正确的,并且发现符号[大于,小于]应该交换,因为程序用Unicode读取它们。他添加了
System.out.println(i1+“”+i2+“”+i3)
inti2=c2.charAt(第一);
int i3=c3.字符(第一);
System.out.println(i1+“”+i2+“”+i3)

if(i1
让它读出Unicode。感谢大家的帮助!以下是我的原始代码(这只是为了文档,以防像我这样的初学者有类似的问题):

`一揽子家庭作业2

        import java.util.Scanner;

        public class OrderCities {

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

        // Prompt user to input cities
        System.out.println("Please enter each city");
        String c1 = scanner.nextLine();
        String c2 = scanner.nextLine();
        String c3 = scanner.nextLine();

        int first = 0;
        int i1 = c1.charAt(first);
        int i2 = c2.charAt(first);
        int i3 = c3.charAt(first);
        System.out.println(i1 + " " + i2 + " " + i3);

        if (i1 < i2 && i1 < i3){
            if (i2 < i3){
                System.out.println(c1 + ", " + c2 + ", " + c3);
            }else
                System.out.println(c1 + ", " + c3 + ", " + c2);
        } else if (i2 < i1 && i2 < i3){
            if (i1 < i3){
                System.out.println(c2 + ", " + c1 + ", " + c3);
            }else
                System.out.println(c2 + ", " + c3 + ", " + c1);
       } else if (i3 < i1 && i3 < i2){
            if (i2 < i1){
                System.out.println(c3 + ", " + c2 + ", " + c1);
            }else
                System.out.println(c3 + ", " + c1 + ", " + c2);
        } else
            System.out.println("ROFLOL, there seems to be an 3RR0R, which means you somehow found a way to break my code!! TR0LL0L0L0L0L");
            }





}
import java.util.Scanner;
公共秩序城市{
公共静态void main(字符串[]args){
//创建扫描仪
扫描仪=新的扫描仪(System.in);
//提示用户输入城市
System.out.println(“请输入每个城市”);
字符串c1=scanner.nextLine();
字符串c2=scanner.nextLine();
字符串c3=scanner.nextLine();
int first=0;
int i1=c1.字符(第一个);
int i2=c2.字符(第一);
int i3=c3.字符(第一);
System.out.println(i1+“”+i2+“”+i3);
如果(i1
这应该有你需要的一切:

不需要提取第一个字符进行比较,Java的集合可以完成这项工作


(将字符串放入
新的ArrayList();
或任何其他集合。)

我喜欢你的第一种方法,我看到的唯一一件事让这件事变得更难的是你是如何检查扫描仪获取的所有输入的第一个字母的(如果两个城市有相同的第一个字母,嗯,哦)

第二种方法更好,因为您使用了一个列表(这是一个很好的选择)来收集输入,并且似乎您正试图根据列表的内容对其进行排序。 知道Java有可以对某些类型的列表进行操作的内置类是件好事(这是一个漂亮的工具)。看看你的作业所需的技能水平,我怀疑你需要一个同步的任何东西,更不用说列表了


看看这个类,让它在你的城市列表上执行必要的操作。答案在javadoc中,我不想透露太多;)

使用
Array.sort(yourArray)
,它将按字母顺序返回
yourArray

但如何让它将用户的输入插入到数组中?我想说的是,扫描仪/charAt/printing的使用演示了超出基础的内容。耸耸肩。快速简单:List stringList=new ArrayList();stringList.put(STRING)@mattklamp,我想我在谷歌上找到了这个字符。这个代码会自动允许输入吗?有人能给我举个例子说明我如何使用它吗?我无法让它工作。我甚至检查了Oracle的网站。这是我目前所拥有的,因为其他人没有工作,并且抛出了一些错误。
        import java.util.Scanner;

        public class OrderCities {

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

        // Prompt user to input cities
        System.out.println("Please enter each city");
        String c1 = scanner.nextLine();
        String c2 = scanner.nextLine();
        String c3 = scanner.nextLine();

        int first = 0;
        int i1 = c1.charAt(first);
        int i2 = c2.charAt(first);
        int i3 = c3.charAt(first);
        System.out.println(i1 + " " + i2 + " " + i3);

        if (i1 < i2 && i1 < i3){
            if (i2 < i3){
                System.out.println(c1 + ", " + c2 + ", " + c3);
            }else
                System.out.println(c1 + ", " + c3 + ", " + c2);
        } else if (i2 < i1 && i2 < i3){
            if (i1 < i3){
                System.out.println(c2 + ", " + c1 + ", " + c3);
            }else
                System.out.println(c2 + ", " + c3 + ", " + c1);
       } else if (i3 < i1 && i3 < i2){
            if (i2 < i1){
                System.out.println(c3 + ", " + c2 + ", " + c1);
            }else
                System.out.println(c3 + ", " + c1 + ", " + c2);
        } else
            System.out.println("ROFLOL, there seems to be an 3RR0R, which means you somehow found a way to break my code!! TR0LL0L0L0L0L");
            }





}