0

Quote for Quote from text:“编写一个程序,要求用户输入三个名字,然后按升序显示这些名字。例如,如果用户输入“Charlie”、“Leslie”和“Andy”,则程序会显示

Andy 
Charlie 
Leslie 

我的教授特别说我们不允许使用循环或数组,因为我们没有在课堂上讨论过。我一直在尝试使用该compareTo方法,但似乎无法让它与两个以上的字符串变量一起运行。

public class SortedNames {

    public static void main(String[] args) {

        //Declare Variables
        String name1;
        String name2;
        String name3;

        //Accept User Imput
        Scanner keyboard=new Scanner(System.in);
        System.out.print("Please Enter First Name ");
        name1=keyboard.nextLine();
        System.out.print("Please Enter Second Name ");
        name2=keyboard.nextLine();
        System.out.print("Please Enter Third Name ");
        name3=keyboard.nextLine();


        //Compare

        if((name2.compareToIgnoreCase(name1)<0)&&(name2.compareToIgnoreCase(name3)<0))
        {
            System.out.println(name2);
        }

        //Compare 

        if((name1.compareToIgnoreCase(name2)<0)&&(name1.compareToIgnoreCase(name3)<0))
        {
            System.out.println(name1);
        }

        //Compare

        if((name3.compareToIgnoreCase(name1)<0)&&(name3.compareToIgnoreCase(name2)<0))
        {
            System.out.println(name3);
        }
  }     

}
4

4 回答 4

6

您有三个字符串,您需要比较它们并相应地对它们进行排序。让我们使用您的示例值:

String name1, name2, name3;
name1 = "Charlie";
name2 = "Leslie";
name3 = "Andy";

为简单起见,有一条规则:您不能简单地“交换”这些值。您需要一个临时变量来保存要交换的值:

String temp;

现在,比较。我解决这个问题的方法是将名字与第二个和第三个名字进行比较,并进行必要的交换:

if(name1.compareTo(name2) > 0) { // "Charlie" > "Leslie" (this won't happen)
    temp = name1;    // temp = "Charlie"
    name1 = name2;   // name1 = "Leslie"
    name2 = temp;    // name2 = "Charlie"
}
if(name1.compareTo(name3) > 0) { // "Charlie" > "Andy" (this will happen)
    temp = name1;    // temp = "Charlie"
    name1 = name3;   // name1 = "Andy"
    name3 = temp;    // name3 = "Charlie"
}
/*
 * Up to this point:
 * name1 = "Andy"; name2 = "Leslie"; name3 = "Charlie"
 */

这样,您将确保三个值中的最小值保持在name1. 现在,比较name2name3,并在必要时交换它们:

if(name2.compareTo(name3) > 0) { // "Leslie" > "Charlie" (this will happen)
    temp = name2;    // temp = "Leslie"
    name2 = name3;   // name2 = "Charlie"
    name3 = temp;    // name3 = "Leslie"
}
/*
 * Up to this point:
 * name1 = "Andy"; name2 = "Charlie"; name3 = "Leslie"
 */

你完成了。

于 2013-10-04T23:39:15.060 回答
3

伪代码

if s1 > s2 swap them
if s2 > s3 swap them
if s1 > s2 swap them
print s1, s2, s3
于 2013-10-04T20:47:10.143 回答
2

这是一个解决方案:

import java.io.Console;

public final class SortNames
{
    private static final String PROMPT_FMT = "Enter %s name: ";
    private static final String NAME_FMT = "%s\n";

    private static final String FIRST = "1st";
    private static final String SECONDS = "2nd";
    private static final String THIRD = "3rd";

    public static void main(final String[] args)
    {
        final Console console = System.console();
        if (console != null)
        {
            final SortNames sortNames = new SortNames(console);
            sortNames.run();
        } // if
        else
        {
            System.err.println("Sorry, no console.");
            System.exit(1);
        } // else
    } // main(String[])

    private final Console mConsole;

    private String mName1 = null;
    private String mName2 = null;
    private String mName3 = null;

    public SortNames(final Console console)
    {
        super();
        mConsole = console;
    } // constructor()

    private void run()
    {
        requestNames();
        sortNames();
        printNames();
    } // run()

    private void requestNames()
    {
        mName1 = requestName(FIRST);
        mName2 = requestName(SECONDS);
        mName3 = requestName(THIRD);
    } // requestNames()

    private String requestName(final String ordinal)
    {
        return mConsole.readLine(PROMPT_FMT, ordinal);
    } // requestName(String)

    private void sortNames()
    {
        sortNames1and2();
        if (sortNames2and3())
        {
            sortNames1and2();
        } // if
    } // sortNames()

    private void sortNames1and2()
    {
        if (greaterThan(mName1, mName2))
        {
            final String greater = mName1;
            mName1 = mName2;
            mName2 = greater;
        } // if
    } // sortNames1and2()

    private boolean sortNames2and3()
    {
        if (greaterThan(mName2, mName3))
        {
            final String greater = mName2;
            mName2 = mName3;
            mName3 = greater;
            return true;
        } // if
        return false;
    } // sortNames2and3()

    private void printNames()
    {
        printName(mName1);
        printName(mName2);
        printName(mName3);
    } // printNames()

    private void printName(final String name)
    {
        mConsole.format(NAME_FMT, name);
    } // printName(String)

    private static boolean greaterThan(final String s1, final String s2)
    {
        return s1.compareTo(s2) > 0;
    } // greaterThan(String, String)


} // class SortNames
于 2013-10-05T00:02:49.303 回答
1
import java.util.Scanner;  // Needed for User Input

public static void main(String[] args) 
{

    String name1;
    String name2;
    String name3;

    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please Enter First Name ");
    name1=keyboard.nextLine();
    System.out.print("Please Enter Second Name ");
    name2=keyboard.nextLine();
    System.out.print("Please Enter Third Name ");
    name3=keyboard.nextLine();
    
    if((name1.compareTo(name2) < 0) && (name1.compareTo(name3) < 0))
        {
            System.out.println(name1);
            if (name2.compareTo(name3) < 0)
            {
                System.out.println(name2);
                System.out.println(name3);
            }
            else
            {
                System.out.println(name3);
                System.out.println(name2);
            }
        }       
    else if((name1.compareTo(name2) > 0) && (name2.compareTo(name3) < 0))
        {
            System.out.println(name2);
            if (name1.compareTo(name3) < 0)
            {
                System.out.println(name1);
                System.out.println(name3);
            }
            else
            {
                System.out.println(name3);
                System.out.println(name1);
            }
        }
    else 
        {
        System.out.println(name3);
            if (name1.compareTo(name2) < 0)
            {
                System.out.println(name1);
                System.out.println(name2);
            }
            else
            {
                System.out.println(name2);
                System.out.println(name1);
            }
        }
    
}
于 2015-09-05T21:21:11.993 回答