有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用字符串按字母顺序对用户输入的字符串名称进行排序。等于和字符串。比较函数

该程序输出应为:

Enter first : Alexis
Enter Second : Zach
Enter Third : Cassie

Here are sorted the sorted name:

Alexis

Cassie

Zach

但如果名称相同,则必须输出一条错误消息,说明名称相同

范例 输入名字1亚历克西斯 输入名字2亚历克西斯 输入名字3凯蒂

名字一和名字二是一样的

import java.util.Scanner;
import java.util.Arrays;


public class AlexisPriceAssignment6
{
        public static String input = " ";
        public static String input2 = " ";
        public static String input3 = "";


    public static void Greet()
    {
        System.out.println("Welcome to Alexis Price's Name Sorter.");
        System.out.println("All names must be unique.");

    }
    public static void Uinput()
    {
          Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter the first name: ");
        input = keyboard.nextLine();
        char one = input.charAt(0);

        System.out.print("Enter the second name: ");
        input2 = keyboard.nextLine();
        char two = input2.charAt(0);

        System.out.print("Enter the third name: " );
        input3 = keyboard.nextLine();
        char three = input3.charAt(0);

        System.out.println();
        if (input.equals(input2) )
        {
            System.out.println(input + " is the same as "+ input2);
        }
        if ( input.equals(input3))
        {
           System.out.println(input + " is the same as "+ input3);
        }
        if (input2.equals(input3))
        {
            System.out.println(input2 + " is the same as "+ input3);
        }
        if (input!=(input2))
        {
            sort();
        }


    }

    public static void sort()
    {
        System.out.println("Here are the sorted names.");

        char charArray [] = input.toCharArray();
        Arrays.sort(charArray);
        String sortedString = new String(charArray);
        System.out.println(sortedString);


    }


    public static void main(String[] args)
    {
        Greet();
        Uinput();


    }

}

共 (3) 个答案

  1. # 1 楼答案

    Here is the answer I was looking for. 
    
    
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Assignment6
    {
        public static void main(String[] args)
        {
            //variables
            String user_name[] = new String[3];
            String user_number[] = {"one","two","three"};
            Scanner s = new Scanner(System.in);
    
            //Program title and info
            System.out.println("Welcome to Price Name Sorter.");
            System.out.println("All names must be unique.");
    
            //Prompt user for input and get input
            System.out.print("Enter First Name: ");
            user_name[0] = s.nextLine();
    
            System.out.print("Enter Second Name: ");
            user_name[1] = s.nextLine();
    
            System.out.print("Enter Third Name: ");
            user_name[2] = s.nextLine();
    
            //checks if there are any equal names
            for(int i = 0; i < (user_name.length-1); i++)
            {
                for(int j = i+1; j < user_name.length; j++)
                {
                    if(user_name[i].equals(user_name[j]))
                    {
                        System.out.println("\nNames " + user_number[i] + " " + user_number[j] + " are equal.");
                        System.exit(0);
                    }
                }
            }
    
            System.out.println();
    
            //sort names
            Arrays.sort(user_name);
    
            System.out.println("Here are the sorted names");
            for(int i = 0; i < user_name.length; i++)
                System.out.println(user_name[i]);
        }
    
    }
    
  2. # 2 楼答案

    支票:

        if (input!=(input2))
        {
            sort();
        }
    

    两者都是错误的,因为String使用。equals()方法进行比较,但没有用,因为您已经通过直接比较每一对来检查所有三种组合。只需在3if语句之后调用sort()方法

  3. # 3 楼答案

    使用TreeSet使解决方案具有可扩展性

    • TreeSet中的Be默认元素按字母顺序排序。订单可以更改
    • 检验 唯一性使用方法tSet.contains()