Java 询问用户三个名字并按字母顺序打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19190297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ask the user for three names and print them in alphabetical order
提问by Harvey
Quote for Quote from the text: "Write a program that asks the user to enter three names, and then displays the names sorted in ascending order. For example, if the user entered "Charlie","Leslie, and "Andy", the program would display
Quote for Quote from the text:“编写一个程序,要求用户输入三个名字,然后按升序显示名字。例如,如果用户输入了“查理”、“莱斯利”和“安迪”,则程序会显示
Andy
Charlie
Leslie
My professor specifically said we are not allowed to use loops or arrays since we have not covered that in class. I have been trying to use the compareTo
method but cant seem to get it to run with more than two string variables.
我的教授特别说我们不允许使用循环或数组,因为我们在课堂上没有涉及到。我一直在尝试使用该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);
}
}
}
回答by Tarik
Pseudocode
伪代码
if s1 > s2 swap them
if s2 > s3 swap them
if s1 > s2 swap them
print s1, s2, s3
回答by Barranka
Now that you have posted your code, I will give you some ideas to help you.
现在你已经发布了你的代码,我会给你一些想法来帮助你。
First of all, try to make some examples in paper, and work out the logic of what you are trying to do. Make an effort to think what the computer must do.
首先,试着在纸上做一些例子,并找出你想要做的事情的逻辑。努力思考计算机必须做什么。
- How would your program know if two names need to be swapped?
- How would your program swap them?
- 您的程序如何知道是否需要交换两个名称?
- 你的程序将如何交换它们?
Important.Do the examples on paper before reading what I've wrote below the lines. Two or three good and honest examples will help you to understand how the thing needs to be done.
重要的。在阅读我在下面写的内容之前,先在纸上做例子。两三个好的和诚实的例子将帮助你理解这件事需要如何完成。
Ok, so now you know how to do it, at least in paper. Now, let's code it.
好的,现在你知道怎么做了,至少在纸上是这样。现在,让我们对其进行编码。
You have three strings, and you need to compare them and sort them accordingly. Let's use your example values:
您有三个字符串,您需要比较它们并相应地对它们进行排序。让我们使用您的示例值:
String name1, name2, name3;
name1 = "Charlie";
name2 = "Leslie";
name3 = "Andy";
To keep things simple, there's one rule: You cannot simply "swap" the values. You'll need a temporary variable to hold the values you are swapping:
为简单起见,有一条规则:您不能简单地“交换”值。您需要一个临时变量来保存要交换的值:
String temp;
Now, the comparisons. The way I would tackle this is by comparing the first name with the second and third name, and make the necessary swaps:
现在,比较。我解决这个问题的方法是比较第一个名称与第二个和第三个名称,并进行必要的交换:
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"
*/
This way, you will ensure that the smallest of the three values is held at name1
. Now, compare name2
and name3
, and swap them if necessary:
这样,您将确保三个值中的最小值保持在name1
。现在,比较name2
和name3
,并在必要时交换它们:
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"
*/
And you are done.
你已经完成了。
Important.Before writing this code. understand it. Quoting from this ten commandments:
重要的。在编写此代码之前。明白了吧。引用这十诫:
Thou shalt not copy and paste other people's code without at least attempting to understand what it does.
在至少尝试了解它的作用之前,您不得复制和粘贴其他人的代码。
Some personal tips:
一些个人建议:
If you want to ask a question, be sure to be honest. You need to put some effort, and show that effort. Quoting from http://whathaveyoutried.com:
如果你想问一个问题,一定要诚实。你需要付出一些努力,并表现出这种努力。引自http://whathaveyoutryed.com:
That's the key realisation. When you're asked “what have you tried?”, it doesn't mean “show me the code you've written, or piss off”. What you have to do is at least try to help yourself – and the trying is the important thing.
So next time you're considering asking a question, you'd better be ready with a convincing answer when you're asked “What have you tried?”
If your answer amounts to “not a lot”, take my word for it: the next question you get back will be “then why should I help you?”
这是关键的认识。当你被问到“你尝试过什么?”时,这并不意味着“给我看看你写的代码,或者生气”。你必须做的至少是尝试帮助自己——而尝试才是最重要的。
因此,下次您考虑提出问题时,当您被问到“您尝试过什么?”时,最好准备好令人信服的答案。
如果你的回答是“不多”,相信我的话:你得到的下一个问题将是“那我为什么要帮助你?”
回答by Peter Sutton
Here is a solution:
这是一个解决方案:
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
回答by user5304649
import java.util.Scanner; // Needed for User Input
导入 java.util.Scanner; // 需要用户输入
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);
}
}
}
}
}