【发布时间】:2013-11-26 20:29:28
【问题描述】:
我有一些作业需要帮助。所以我不得不编写代码,其中给了我三个数字,并且通过使用 if-else 语句,我必须以正确的顺序显示它们。所以我认为我在正确的数字道路上,但不知道如何使用字符串来显示它。这是我到目前为止所做的:
public class CS1702_Lab32
{
static public void main(String args[])
{
int a = 9, b = -10, c = -3;
/* These are the six possible orders
* abc
* acb
* bac
* bca
* cab
* cba
*/
if ( ( a <= b ) && ( b <= c ) )
{
System.out.println("The correct order is 'a'<='b'<='c'");
}
else if ( ( a <= c ) && ( c <= b ) )
{
System.out.println("The correct order is 'a'<='c'<='b'");
}
else if ( ( b <= a ) && ( a <= c ) )
{
System.out.println("The correct order is 'b'<='a'<='c'");
}
else if ( ( b <= c ) && ( c <= a ))
{
System.out.println("The correct order is 'b'<='c'<='a'");
}
else if ( ( c <= a) && ( a <= b ))
{
System.out.println("The correct order is 'c'<='a'<='b'");
}
else
{
System.out.println("The correct order is 'c'<='b'<='a'");
}
}
}
现在我将如何按字母顺序对三个字符串做同样的事情?我认为我现在不应该使用数组,因为工作表只是在条件语句上。以下是工作表中的问题(如果有帮助):
Write the Java code that will solve the following problems:
1. Given three numbers, displays them in the correct order (ascending)
2. Given three names (e.g. name1, name2 and name3 as string variables), display them in the correct alphabetical order
【问题讨论】:
-
您是否尝试过使用
yourStringA.compareTo(yourStringB)?见:stackoverflow.com/a/6203441/362298
标签: java if-statement