斯坦福大学开放课程——编程方法 作业2 - 5
2010-12-14 16:12
447 查看
5.
Write a ConsoleProgram that reads in a list of integers, one per line, until a sentinel value of 0 (which you should be able to change easily to some other value). When the sentinel is read, your program should display the smallest and largest values in the list, as illustrated in this sample run:
Your program should handle the following special cases:
If the user enters only one value before the sentinel, the program should report that value as both the largest and smallest.
If the user enters the sentinel on the very first input line, then no values have been entered, and your program should display a message to that effect.
程序源代码:
Write a ConsoleProgram that reads in a list of integers, one per line, until a sentinel value of 0 (which you should be able to change easily to some other value). When the sentinel is read, your program should display the smallest and largest values in the list, as illustrated in this sample run:
Your program should handle the following special cases:
If the user enters only one value before the sentinel, the program should report that value as both the largest and smallest.
If the user enters the sentinel on the very first input line, then no values have been entered, and your program should display a message to that effect.
程序源代码:
/* * File: FindRange.java * Name: * Section Leader: * -------------------- * This file is the starter file for the FindRange problem. */ import acm.program.*; public class FindRange extends ConsoleProgram { public void run() { /* You fill this in */ println("This program finds the largest and smallest numbers."); int max, //最大值 min, //最小值 tmp; //临时存储变量 tmp = readInt("? "); max = min = tmp; //初始赋值 while(0 != tmp){ tmp = readInt("? "); max = tmp>max?tmp:max; min = tmp<min?tmp:min; } println("smallest: " + min); println("largest: " + max); } }
相关文章推荐
- 斯坦福大学开放课程——编程方法 作业1-4 (完美解决任何地图都能找到中点)【在中心点放置Beeper】
- 斯坦福大学开放课程——编程方法 作业2 - 1
- 斯坦福大学开放课程——编程方法 作业1-4 (只解决奇数宽度的算法)【在中心点放置Beeper】
- 斯坦福大学开放课程——编程方法 作业1-3 (附作业中文翻译及源码)【放置黑白棋盘】
- 斯坦福大学开放课程——编程方法 作业1-1
- 斯坦福大学开放课程——编程方法 作业2 - 2
- 斯坦福大学开放课程——编程方法 作业1-2,个人实现方法。
- 斯坦福大学开放课程——编程方法 作业1-3,个人实现方法。
- 斯坦福大学开放课程——编程方法 作业1-3
- 斯坦福大学开放课程——编程方法 作业2 - 3
- 斯坦福大学开放课程——编程方法 作业4 完整代码
- 斯坦福大学开放课程——编程方法 作业7
- 斯坦福大学开放课程——编程方法 作业3
- 斯坦福大学开放课程——编程方法 作业2 - 4
- 斯坦福大学开放课程——编程方法 作业1-4
- 斯坦福大学开放课程——编程方法 作业1-4 (附作业中文翻译及源码)【在中心点放置Beeper】
- 斯坦福大学开放课程——编程方法 作业2 - 6
- 斯坦福大学开放课程——编程方法 作业6
- 斯坦福大学开放课程——编程方法 作业1-2
- 斯坦福大学开放课程——编程方法 作业1-2(附作业中文翻译及源码)【修复支撑拱门】