【发布时间】:2015-06-18 07:02:57
【问题描述】:
问题 2 在文件 TwoSmall.java 中编写程序。
输入一组正整数,以-1结尾作为标记。按顺序打印这些数字中最小的和次小的。
您可以假设在 -1 之前总是至少有两个数字;您不必检查这是否属实。
public class TwoSmall{
public static void main(String [] args){
int min;
int secondMin;
int sentinel = -1;
System.out.println("Enter atleast two numbers");
int input = IO.readInt();
min = input;
secondMin = input;
do{
if(input < min){
secondMin = min;
min = input;
}
else if (input < secondMin) {
secondMin = input;
}
input = IO.readInt();
} while(input!=sentinel);
System.out.println(min);
System.out.println(secondMin);
}
}
这不起作用?谁能告诉我这个程序有什么问题?
【问题讨论】:
-
您是否尝试过一步一步地完成它,或者通过头脑中的思考,在整个程序流程中使用打印语句来跟踪它,或者使用实际的调试器?
-
IO.readInt 一次只能读取 1 个 int,但现在您正在设置 min 和 secondMin 以在读取后输入。您不应该为每个变量调用两次 IO.readInt 吗?
-
你能详细说说它是如何失败的吗?
-
“这不起作用”请描述你输入了什么,输出是什么
标签: java