Ale*_*der 0 java algorithm conditional-statements
在阅读一本面向初学者的Java书籍时,我偶然发现了一个练习:
编写一个程序,该程序
int从命令行获取三个值并按升序打印它们。使用Math.min()和Math.max()。
问题是if尚未考虑条件条件,因此从逻辑上讲我不能使用它们。
我试图对C中的类似问题使用答案,但遇到not a statement错误。
public class three_sort
{
public static void main(String[] args)
{
int a = Integer.parseInt( args[0] );
int b = Integer.parseInt( args[1] );
int c = Integer.parseInt( args[2] );
int min = a;
(min > b) && (min = b); //finding minimum
(min > c) && (min = c);
System.out.println(min);
int i = a;
(b < max) && (b > min) && (i = b); // finding intermediate
(c < max) && (c > min) && (i = c);
System.out.println(i);
int max = a;
(max < b) && (max = b); //finding maximum
(max < c) && (max = c);
System.out.println(max);
}
}
Run Code Online (Sandbox Code Playgroud)
是的,我还没有使用Math.min和Math.max,因为在它们的帮助下,我可以找到min和max,但是找不到中间的。有人知道如何解决这个问题吗?
在找到最小值和最大值之后,找到中间数字:
int a = Integer.parseInt( args[0] );
int b = Integer.parseInt( args[1] );
int c = Integer.parseInt( args[2] );
int x = Math.min(a, b);
int min = Math.min(x,c);
int z = Math.max(a, b);
int max = Math.max(z, c);
int mid = a+ b+ c- min - max;
System.out.print(min);
System.out.print(mid);
System.out.print(max);
Run Code Online (Sandbox Code Playgroud)
关于这个,我已经回答了另一个问题,在这里您可以看到它。
| 归档时间: |
|
| 查看次数: |
3277 次 |
| 最近记录: |