【问题标题】:Is it possible to do 2 actions with ternary operator In java是否可以在 java 中使用三元运算符执行 2 个操作
【发布时间】:2021-09-01 14:32:03
【问题描述】:

我也在尝试将每个数字 7 变成一个字母。例如:57 75 157 现在它给了我除以 7 的词

public class Boomtest {

public static int boom () {
    int boom2 = 0;
    String boom7 = "Boombala";{
    for (boom2 = 0; boom2 <= 200; ++boom2) {
    String print = boom2 % 7 == 0 ? boom7 : Integer.toString(boom2);
    
        }
    }
    return boom2;
    
}

}

  1. 可以用三进制实现吗?
  2. 如何在没有 String 打印的情况下运行它,感觉好像我错过了什么。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请通过tour 了解 Stack Overflow 的工作原理,并阅读How to Ask 以了解如何提高问题的质量。然后查看help center,看看你可以问什么问题。目前尚不清楚您要问什么或要做什么。请edit您的问题更详细地描述您遇到的问题。

标签: java conditional-operator


【解决方案1】:

我知道还有其他答案说不,比如这个:Multiple Statements in the Ternary Operator,但你可以发挥你的想象力来做到这一点,例如,你可以创建执行操作的方法,像这样

public static int boom(){
    ...
    int numberToDelete = (/*conditional here*/)?actions01():actions02();
    ...
}
public static int actions01(){
    /*Multiple actions here*/
    return 0;
}
public static int actions02(){
    /*Multiple actions here*/
    return 0;
}

无论如何我不建议你这样做。

PS:尝试纠正你的身份

【讨论】:

    猜你喜欢
    • 2019-10-08
    • 2020-06-13
    • 2016-10-04
    • 2021-10-06
    • 2012-04-06
    • 2020-02-20
    • 1970-01-01
    • 2016-05-15
    • 2012-03-25
    相关资源
    最近更新 更多