三元运算符中的多个条件

发布于 2024-10-17 08:39:50 字数 503 浏览 15 评论 0 原文

首先,问题是“编写一个 Java 程序,使用三元运算符查找三个数字中的最小者”。

这是我的代码:

class questionNine
{
    public static void main(String args[])
    {
        int x = 1, y = 2, z = 3;
        int smallestNum;

        smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y : (z<y && z<x) ? z;
        System.out.println(smallestNum + " is the smallest of the three numbers.");
    }
}

我尝试在三元运算符中使用多个条件,但这不起作用。我缺席了几天,所以我真的不知道该怎么办,而且我老师的电话也关机了。有什么帮助吗?

First off, the question is "Write a Java program to find the smallest of three numbers using ternary operators."

Here's my code:

class questionNine
{
    public static void main(String args[])
    {
        int x = 1, y = 2, z = 3;
        int smallestNum;

        smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y : (z<y && z<x) ? z;
        System.out.println(smallestNum + " is the smallest of the three numbers.");
    }
}

I tried to use multiple conditions in the ternary operator but that doesn't work. I was absent a few days so I'm not really sure what to do and my teacher's phone is off. Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

何必那么矫情 2024-10-24 08:39:50

尝试

int min = x < y ? (x < z ? x : z) : (y < z ? y : z);

也可以删除括号:

int min = x < y ? x < z ? x : z : y < z ? y : z;

Try

int min = x < y ? (x < z ? x : z) : (y < z ? y : z);

You can also remove the parenthesis:

int min = x < y ? x < z ? x : z : y < z ? y : z;
时光清浅 2024-10-24 08:39:50

由于这是家庭作业,我不仅会给您答案,还会提供一个算法,以便您可以自己解决。

首先弄清楚如何使用单个三元运算符编写 min(x, y)。

完成后,将 min(x, y, z) 的以下代码更改为使用三元运算符,然后替换您在上一步中计算出的 min(x, y) 的代码。

int min(x, y, z) {
    if (x <= y) {
        return min(x, z);
    } else {
        return min(y, z);
    }
}

Since this is homework I'm not just going to give you the answer, but instead an algorithm so that you can work it out yourself.

First work out how to write min(x, y) using a single ternary operator.

Once you have that, change the following code for min(x, y, z) to use a ternary operator then substitute in the code for min(x, y) that you worked out from the previous step.

int min(x, y, z) {
    if (x <= y) {
        return min(x, z);
    } else {
        return min(y, z);
    }
}
情归归情 2024-10-24 08:39:50

当您确实不需要时,您正在测试 z。您的三元运算符必须采用 cond 形式?如果真:如果假;

所以如果你有多个条件,你有这个:

cond1? ifTrue1:条件2?如果为真2:如果为假2;

如果你明白这一点,就不要看下面了。如果您仍然需要帮助,请查看下文。

我还提供了一个更清晰的不嵌套它们的版本(假设您不需要嵌套它们。我当然希望您的作业不需要您嵌套它们,因为那非常丑陋!)

这是我的想法:

class QuestionNine
{
    public static void main(String args[])
    {
        smallest(1,2,3);
        smallest(4,3,2);
        smallest(1,1,1);
        smallest(5,4,5);
        smallest(0,0,1);
    }

    public static void smallest(int x, int y, int z) {
        // bugfix, thanks Mark! 
        //int smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y :  z;
        int smallestNum = (x<=y && x<=z) ? x : (y<=x && y<=z) ? y :  z;
        System.out.println(smallestNum + " is the smallest of the three numbers.");
    }

    public static void smallest2(int x, int y, int z) {
       int smallest = x < y ? x : y; // if they are equal, it doesn't matter which
       smallest = z < smallest ? z : smallest;
       System.out.println(smallest + " is the smallest of the three numbers.");
    }
}

You're testing for z, when you really don't need to. Your ternary operator must be of form cond ? ifTrue : ifFalse;

so if you have multiple conditions, you have this:

cond1? ifTrue1 : cond2? if True2 : ifFalse2;

If you understand this, don't look below. If you still need help, look below.

I also included a version that doesn't nest them that is clearer (assuming you don't need to have them nested. I sure would hope your assignment doesn't require you to nest them because that's pretty ugly!)

.

.

Here's what I come up with:

class QuestionNine
{
    public static void main(String args[])
    {
        smallest(1,2,3);
        smallest(4,3,2);
        smallest(1,1,1);
        smallest(5,4,5);
        smallest(0,0,1);
    }

    public static void smallest(int x, int y, int z) {
        // bugfix, thanks Mark! 
        //int smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y :  z;
        int smallestNum = (x<=y && x<=z) ? x : (y<=x && y<=z) ? y :  z;
        System.out.println(smallestNum + " is the smallest of the three numbers.");
    }

    public static void smallest2(int x, int y, int z) {
       int smallest = x < y ? x : y; // if they are equal, it doesn't matter which
       smallest = z < smallest ? z : smallest;
       System.out.println(smallest + " is the smallest of the three numbers.");
    }
}
拥有 2024-10-24 08:39:50
public static int min(int x, int y, int z) {
    return x<y?Math.min(x, z):Math.min(y, z);           
}

public static void main(String[] args) {
    int smallestNum = min(10, 12, 15);
    System.out.println(smallestNum);
}
public static int min(int x, int y, int z) {
    return x<y?Math.min(x, z):Math.min(y, z);           
}

public static void main(String[] args) {
    int smallestNum = min(10, 12, 15);
    System.out.println(smallestNum);
}
不顾 2024-10-24 08:39:50

我知道已经晚了。仅供参考,这也有效:

int smallestNum = (x<y ? x : (x=y)) < z ? x : z

I know it's late. Just for information this also works:

int smallestNum = (x<y ? x : (x=y)) < z ? x : z
扛起拖把扫天下 2024-10-24 08:39:50

最后一部分: (z 缺少 ':' :

(z<y && z<x) ? z : some_value;

The last part: (z<y && z<x) ? z is missing a ':' :

(z<y && z<x) ? z : some_value;
羁拥 2024-10-24 08:39:50

我的解决方案:

public static void main(String args[])
{
    int x = 1, y = 2, z = 3;
    int smallestNum = (x < y && x < z) ? x :
        (y < x && y < z) ? y :
        (z < y && z < x) ? z:y;
    System.out.println(smallestNum + " is the smallest of the three numbers.");
}

My solution:

public static void main(String args[])
{
    int x = 1, y = 2, z = 3;
    int smallestNum = (x < y && x < z) ? x :
        (y < x && y < z) ? y :
        (z < y && z < x) ? z:y;
    System.out.println(smallestNum + " is the smallest of the three numbers.");
}
寄离 2024-10-24 08:39:50

我的贡献...

public static int getSmallestNumber( int x, int y, int z) {

     return x < y && x < z ? x : y < x && y < z ? y : z;
}

public static void main ( String ... args ) {

    System.out.println( getSmallestNumber( 123, 234, 345 ) );
}

My contribution ...

public static int getSmallestNumber( int x, int y, int z) {

     return x < y && x < z ? x : y < x && y < z ? y : z;
}

public static void main ( String ... args ) {

    System.out.println( getSmallestNumber( 123, 234, 345 ) );
}
想念有你 2024-10-24 08:39:50
public static void main(String args[]) {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the number and check heigest number of three number:-");
    int a=sc.nextInt();
    int b=sc.nextInt();
    int c=sc.nextInt();
    int max;
    max=(a>b&&a>c)?a:(b>a&&b>c)?b:(c>a&&c>b)?c:(a==b&&b==c)?a:0;
    
    System.out.println("the heigest number is:"+max);
    
public static void main(String args[]) {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the number and check heigest number of three number:-");
    int a=sc.nextInt();
    int b=sc.nextInt();
    int c=sc.nextInt();
    int max;
    max=(a>b&&a>c)?a:(b>a&&b>c)?b:(c>a&&c>b)?c:(a==b&&b==c)?a:0;
    
    System.out.println("the heigest number is:"+max);
    
心头的小情儿 2024-10-24 08:39:50

这个答案迟到了七年,所以我只给你代码:

int smallestNumber = (x > y) ? 
            ((y > z) ? z : y) : 
            ((x > z) ? z : x);

缩进应该解释代码,它只是一个三元数,它在初始条件 x > > 上评估其他三元数。 y; /* 如果条件为真,则评估第一个三元数,否则评估第二个三元数。 */

This answer is coming in seven years late, so I'll just give you the code:

int smallestNumber = (x > y) ? 
            ((y > z) ? z : y) : 
            ((x > z) ? z : x);

The indentation should explain the code, which is simply a ternary that evaluates other ternaries on the initial condition x > y; /* the first ternary gets evaluated if that condition is true, else the second one gets evaluated. */

你的往事 2024-10-24 08:39:50

你错过了 z var 之后的值
最小数 = (x

you missed the value after z var
smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y : (z<y && z<x) ? z:0;
Try this, it works

奢华的一滴泪 2024-10-24 08:39:50
public class questionNine{
  public static void main(String args[]){
    int x = 1, y = 2, z = 3;
    int smallestNum =  (x<y && x<z) ? x : ((y < x && y<z) ? y : z);
    System.out.println(smallestNum);
  }
}
public class questionNine{
  public static void main(String args[]){
    int x = 1, y = 2, z = 3;
    int smallestNum =  (x<y && x<z) ? x : ((y < x && y<z) ? y : z);
    System.out.println(smallestNum);
  }
}
转身以后 2024-10-24 08:39:50
int min = (x<y)?((x<z)?x:z):((y<z)?y:z);
int min = (x<y)?((x<z)?x:z):((y<z)?y:z);
失与倦" 2024-10-24 08:39:50

最好的方法是使用 if 和 else 语句创建一个示例,然后对其应用三元运算符
(q

best way to do this is to make an example using if and else statement and then apply the ternary operators on it
(q

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文