[英]Multiple conditions in ternary operators
首先,問題是“編寫一個 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.");
}
}
嘗試
int min = x < y ? (x < z ? x : z) : (y < z ? y : z);
您還可以刪除括號:
int min = x < y ? x < z ? x : z : y < z ? y : z;
由於這是家庭作業,我不只是要給你答案,而是一個算法,這樣你就可以自己解決了。
首先弄清楚如何使用單個三元運算符編寫 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);
}
}
當您確實不需要時,您正在測試 z。 你的三元運算符必須是 cond 形式? 如果真:如果假;
所以如果你有多個條件,你有這個:
條件 1? ifTrue1:cond2? 如果 True2 : ifFalse2;
如果你明白這一點,請不要往下看。 如果您仍然需要幫助,請往下看。
我還包含了一個更清晰的不嵌套它們的版本(假設您不需要嵌套它們。我當然希望您的作業不需要您嵌套它們,因為那太丑了!)
.
.
這是我想出的:
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.");
}
}
我知道已經晚了。 僅供參考,這也適用:
int smallestNum = (x<y ? x : (x=y)) < z ? x : z
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 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.");
}
我的貢獻 ...
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 ) );
}
最后一部分: (z<y && z<x) ? z
(z<y && z<x) ? z
缺少一個 ':' :
(z<y && z<x) ? z : some_value;
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);
這個答案晚了七年,所以我只給你代碼:
int smallestNumber = (x > y) ?
((y > z) ? z : y) :
((x > z) ? z : x);
縮進應該解釋代碼,它只是一個三元,它在初始條件x > y
上評估其他三元; /* 如果條件為真,則評估第一個三元,否則評估第二個。 */
您錯過了 z var minimumNum = (x<y && x<z) 之后的值? x : (y<x && y<z) ? y : (z<y && z<x) ? z:0; 試試這個,它有效
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);
}
}
最好的方法是使用 if 和 else 語句做一個例子,然后對其應用三元運算符(q
int min = (x<y)?((x<z)?x:z):((y<z)?y:z);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.