编程的确是个循序渐进的过程,有了基础版,稍微改改就可以添加新功能了,不错,不错!
代码如下:
//JHTP Exercise 4.22: Find two Largest Number
//by pandenghuang@163.com
/* (Find the Two Largest Numbers) Using an approach similar to that for Exercise 4.21, find
the two largest values of the 10 values entered. [Note: You may input each number only once.*/
import java.util.Scanner;
public class twoLargestNumber {
public static void main(String[] args) {
int counter=1;
int largest=0;
int secondLargest=0;
Scanner scanner=new Scanner(System.in);
int number=0;
int salesNo=1;
int largestSalesNo=1;
int secondLargestSalesNo=1;
while (counter<=10){
System.out.printf("请输入销售员No.%d的销售额:",salesNo);
number=scanner.nextInt();
if(number>=largest)
{
largest=number;
largestSalesNo=salesNo;
}
if (number>=secondLargest && number<largest)
{
secondLargest=number;
secondLargestSalesNo=salesNo;
}
++counter;
++salesNo;
}
System.out.printf("恭喜销售员No.%d创造了最高销售额:%d RMB\n",largestSalesNo,largest);
System.out.printf("恭喜销售员No.%d创造了第二高销售额:%d RMB\n",secondLargestSalesNo,secondLargest);
}
}
运行结果:
请输入销售员No.1的销售额:12345
请输入销售员No.2的销售额:54321
请输入销售员No.3的销售额:44432
请输入销售员No.4的销售额:67890
请输入销售员No.5的销售额:98760
请输入销售员No.6的销售额:68997
请输入销售员No.7的销售额:43234
请输入销售员No.8的销售额:99987
请输入销售员No.9的销售额:99870
请输入销售员No.10的销售额:103
恭喜销售员No.8创造了最高销售额:99987 RMB
恭喜销售员No.9创造了第二高销售额:99870 RMB