1
因此,我的編程任務是編寫一個程序,讀取10個用戶輸入,然後通知用戶哪個是最高值。如下圖所示,它完成得很好,我在任務中獲得了100%。顯示答案
但是,我想拋光編碼結構,以便每次用戶在提示後輸入更大的值時(例如0,1,2,3,4,5,6,7, 8,9)輸出不會顯示1,2,3,4,5,6,7,8和9; 9是最終產出。
如何擺脫9之前的所有值,以便輸出結果爲9?
import java.util.Scanner;
class Army{
public static void main(String[] args){
// declares an array of doubles
double[] inputArray = new double[10];
// allocates memory for 10 doubles
System.out.println("Please enter ten numbers.");
try {
Scanner in = new Scanner(System.in);
for (int j = 0; j < inputArray.length ; j++) {
inputArray[(int) j] = in.nextDouble();
}
}
catch (Exception e) {
e.printStackTrace();
}
double maxValue = inputArray[0];
for (int i=0; i < inputArray.length; i++) {
if (inputArray[i] > maxValue){
maxValue = inputArray[i];
System.out.println("The largest number is "+maxValue+".");
}else{
System.out.println("The largest number is "+inputArray[i]+".");
// optional: display only one answer.
}
}
}
}
@PeterZēng歡迎您。 – 2014-09-29 05:55:05