[英]Sorting 3 Numbers from Least to Greatest
As part of a problem set I have to sort 3 numbers in ascending order.作为问题集的一部分,我必须按升序对 3 个数字进行排序。 A simple enough task, but for some reason I'm not getting the expected result.
一个足够简单的任务,但由于某种原因,我没有得到预期的结果。 Using arrays is not allowed.
不允许使用数组。 Below is my code;
下面是我的代码; I've linked to my flowchart here .
我已在此处链接到我的流程图。 I cannot get the program to sort 3 numbers such as 5, 5, and -4.
我无法让程序对 5、5 和 -4 等 3 个数字进行排序。 When I attempt that case, here is the output:
当我尝试这种情况时,这是输出:
Enter three numbers.
In order -0.04 5.0 5.0In order 5.0 -0.04 5.0按顺序 -0.04 5.0 5.0 按顺序 5.0 -0.04 5.0
If I get that one to work, I cannot get the case of 23, 0, 39 to sort.如果我让那个工作,我无法对 23、0、39 的情况进行排序。 Not sure if I have over-complicated the attempt with so many cases;
不确定我是否在这么多情况下使尝试过于复杂; I feel that my flowchart covers all possibilities.
我觉得我的流程图涵盖了所有的可能性。 Thanks in advance!
提前致谢!
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter three numbers.");
double x = reader.nextDouble();
double y = reader.nextDouble();
double z = reader.nextDouble();
if (x >= y){
if (y >= z)
System.out.print("In order " + z + " "+ y + " " + x);
if (z >= x)
System.out.print("In order " + y + " "+ x + " " + z);
if (x > z)
System.out.print("In order " + y + " " + z + " " + x);
}
if (y > x)
{
if (z >= y)
System.out.print("In order " + x + " " + y + " "+ z);
if (z >= x)
System.out.print("In order " + y + " " + x + " " + z);
if (x > z)
System.out.print("In order " + y + " " + z + " " + x);
}
}
}
You can solve this with no if
(s) using Math.max(double, double)
and Math.min(double, double)
and basic addition and subtraction.您可以使用
Math.max(double, double)
和Math.min(double, double)
以及基本的加法和减法,不用if
(s) 来解决这个问题。 Like,喜欢,
double max = Math.max(x, Math.max(y, z));
double min = Math.min(x, Math.min(y, z));
double mid = x + y + z - max - min;
System.out.printf("In order %f %f %f%n", min, mid, max);
Using if
and else
comparisons instead of Math.max
and Math.min
is a little more complicated.使用
if
和else
比较而不是Math.max
和Math.min
会稍微复杂一些。 Pick a default value and compare with the other two.选择一个默认值并与其他两个进行比较。 Like,
喜欢,
double max = z;
if (x > max || y > max) {
if (x > y) {
max = x;
} else {
max = y;
}
}
double min = z;
if (x < min || y < min) {
if (x < y) {
min = x;
} else {
min = y;
}
}
double mid = x + y + z - max - min;
System.out.printf("In order %f %f %f%n", min, mid, max);
If you want to stick with the if/else logic, here's a slight modification to your original solution.如果您想坚持 if/else 逻辑,这里对您的原始解决方案稍作修改。 Note the use of an else if.
注意 else if 的使用。 I have commented out your earlier lines of code for comparison.
我已经注释掉了您之前的代码行以进行比较。
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Enter three numbers."); //Use println instead of print, that way the input begins on the next line
double x = reader.nextDouble();
double y = reader.nextDouble();
double z = reader.nextDouble();
if (x >= y){ //In the three responses below, y is always before x.
if (y >= z)
System.out.print("In order " + z + " "+ y + " " + x);
else if (z >= x)
System.out.print("In order " + y + " "+ x + " " + z);
else if (x > z)
System.out.print("In order " + y + " " + z + " " + x);
}
if (y > x){// In the three responses below, x is always before y
if (z >= y)
System.out.print("In order " + x + " " + y + " "+ z);
else if (z >= x)
//System.out.print("In order " + y + " " + x + " " + z); //In this case, z has to be smaller than y. The order was off
System.out.print("In order " + x + " " + z + " " + y);
else if (x > z)
//System.out.print("In order " + y + " " + z + " " + x);
System.out.print("In order " + z + " " + x + " " + y); //Y is the biggest. The order here was off.
}
}
}
There is some issues with your if/else
statements:您的
if/else
语句存在一些问题:
(x < y)
for sure but you are printing x
before y
(now edited).(x < y)
是肯定的,但是您在y
之前打印了x
(现在已编辑)。 Here is the correct code:这是正确的代码:
if (x >= y) {
if (y >= z)
System.out.print("In order " + z + " " + y + " " + x);
else if (z >= x)
System.out.print("In order " + y + " " + x + " " + z);
else if (x >= z)
System.out.print("In order " + y + " " + z + " " + x);
} else {
if (z >= y)
System.out.print("In order " + x + " " + y + " " + z);
else if (z >= x)
System.out.print("In order " + x + " " + z + " " + y);
else if (x >= z)
System.out.print("In order " + z + " " + x + " " + y);
}
import java.util.Scanner;
public class a9 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Input your numbers !!");
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
var ok = Math.min(a , Math.min(b , c));
if ( ok == a ) {
if (c > b ) {
System.out.println("Your order is : " + "," + c + "," + b + "," + a);
}
else {
System.out.println("Your order is : " + b + "," + c + "," + a);
}
}
else if (ok == b) {
if( a > c) {
System.out.println("Your order is : " + a + "," + c + "," + a);
}
else {
System.out.println("Your order is : " + c + "," + a + "," + b);
}
}
else {
if ( b > a) {
System.out.println("Your order is : " + a + "," + b + "," + c);
}
else {
System.out.println("Your order is : " + b + "," + a + "," + c);
}
}
scan.close();
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.