正在加载图片...
Java语言实现: 2. import java. io. 3. public class Max i 4. public static void main(String[] args) throws IOException t 5 /初始化 6 Buffered Reader input new Buffered Reader (new InputStream Reader (system. in); 8 int largest=0; 9 int counter=0 10 int theInteger=0 11 ∥/循环比较 2, while( counter 10)t 13 //输入被比较的数 14 counter++;//计数 System. out printIn("请输入第"+ counter+"个被比较的数:") 16 String inputstring input readline i theInteger Integer. parseInt(inputstring; 18 /大值比较 19 if (theInteger> largest) largest=theInteger; 20 3// while 21 System. out. printin("求出最大数是:"+ argest); 22.} 23. 24.}Java程序设计大学教程 5.1.2 常用算法 ◼ 基本算法大 都比较简单, 是其他算法 的基础。这 类算法在程 序中应用非 常普遍,如: 累加求和、 累乘求积、 求最大和最 小值等。 从10个数中求最大值算法 的例子 开始 初始化,将largest和counter设为0 计数器判断 counter <10 ? 否 、 是 while-do 循环 largest←theInteger 大值比较 theInteger>larges? 否 、 是 返回largest 结束 输入被比较的数theInteger 计数:counter←counter+1 伪代码描述算法: FindLargest Input: 10 positive integers 1. largest←0 2. counter←0 3. while(counter < 10) 3.1 Input theInteger 3.2 if (theInteger > largest) then 3.2.1 largest←theInteger end if 3.3 counter←counter+1 end while 4. Return largest End 1. Java语言实现: 2. import java.io.*; 3. public class Max { 4. public static void main(String[] args) throws IOException { 5. //初始化 6. BufferedReader input = new BufferedReader 7. (new InputStreamReader(System.in)); 8. int largest=0; 9. int counter=0; 10. int theInteger=0; 11. //循环比较 12. while(counter < 10) { 13. //输入被比较的数 14. counter++;//计数 15. System.out.println("请输入第"+counter+"个被比较的数:"); 16. String inputString = input.readLine(); 17. theInteger = Integer.parseInt(inputString); 18. //大值比较 19. if (theInteger > largest) largest=theInteger; 20. }// while 21. System.out.println("求出最大数是:"+largest); 22. } 23. 24.}
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有