import java.util.Scanner;
public class Demo {
public static void main( String args[ ] ) {
Scanner myInput = new Scanner( System.in ) 。
double a、b、c、d、e。
a = myInput.nextDouble();
b = myInput.nextDouble();
c = myInput.nextDouble();
d = myInput.nextDouble();
e = myInput.nextDouble()。
//檢查a是否為最大或最小。
if (a > b && a > c && a > d & a > e) {
System.out.println("a is max")。
} else if (a < b && a < c & a < d & a < e) {
System.out.println("a is min")。
}
//checks if b is max or min.
if (b > a && b > c && b > d & b > e) {
System.out.println("b is max")。
} else if (b < a && b < c & b < d & b < e) {
System.out.println("b is min")。
}
//檢查c是否為最大或最小。
if (c > a && c > b && c > d & c > e) {
System.out.println("c is max")。
} else if (c < a && c < b & c < d & c < e) {
System.out.println("c is min")。
}
//checks if d is max or min.
if (d > a && d > b && d > c & d > e) {
System.out.println("d is max")。
} else if (d < a & & d < b & & d < c & d < e) {
System.out.println("d is min"/span>)。
}
//checks if e is max or min.
if (e > a && e > b && e > c & e > d) {
System.out.println("e is max")。
} else if (e < a && e < b && e < c & e < d) {
System.out.println("e is min")。
}
//checks if the integers are different.
if (a == b || a == c || a == d || a == e || b == c || b == d || b == e || c == d || d == e)
System.out.println("檢測到相同的整數"/span>)。
}
}
我試圖找到5個輸入的最大和最小值。這段代碼作業得很好,但能否簡化?它看起來很過分,但我才剛剛開始用Java編程,所以我還沒有學到很多東西。我也只被允許使用if陳述句。沒有回圈。
uj5u.com熱心網友回復:
試試這個。
static void maxMin( String name, double f, double g, double h, double i, double j) {
if (f > g && f > h && f > i & f > j)
System.out.println(name " is max"/span>)。
else if (f < g & & f < h & & f < i & & f < j)
System.out.println(name " is min"/span>)。
}
public static void main(String[] args){
Scanner myInput = new Scanner(System.in)。
double a、b、c、d、e。
a = myInput.nextDouble();
b = myInput.nextDouble();
c = myInput.nextDouble();
d = myInput.nextDouble();
e = myInput.nextDouble();
maxMin("a"/span>, a, b, c, d, e)。
maxMin("b"/span>, b, c, d, e, a);
maxMin("c"/span>, c, d, e, a, b);
maxMin("d"/span>, d, e, a, b, c);
maxMin("e"/span>, e, a, b, c, d);
if (a == b || a == c || a == d || a == e || b == c || b == d || b == e || c == d || d == e)
System.out.println("檢測到相同的整數"/span>)。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/329416.html
標籤:
