当程序启动时,它将要求用户提供四条信息:
该计划将对其中每一项设置限制。限制应该是问题的一部分,就像how wide do you want the windows? (1-5).一样,如果用户超过了一个限制,无论是在上面还是下面,都应该输出一条消息,并且应该再次查询用户。这应该发生的次数,这是必要的,以获得一个数字在一定范围内。(您可以看到下面的代码,我已经完成了这个部分)
(这部分我有问题)我的窗户的数目和大小应该用循环来控制。但是,我不允许使用大量的"if“语句。每个问题的上限应作为常数列入。例如,如果常量被更改,我的程序应该能够处理新的限制,而不需要进行任何其他的更改。我应该使用哪些循环来控制窗口的数量和大小?
import java.util.Scanner;
public class BuildingAssign {
public static void main (String [] args){
int input;
Scanner in = new Scanner(System.in);
do {
System.out.print("How many stories high the building will be? (1-5): ");
input = in.nextInt();
}while (input < 1 || input > 5);
do {
System.out.print("How many windows on each story? (1-10): ");
input = in.nextInt();
}while (input < 1 || input > 10);
do {
System.out.print("How wide each window will be? (3-5): ");
input = in.nextInt();
}while (input < 3 || input > 5);
do {
System.out.print("How tall each window will be? (4-6): ");
input = in.nextInt();
}while (input < 4 || input > 6);
}
}发布于 2014-02-23 23:29:02
看来你已经知道该怎么做了。基本上,您会为其他人做同样的事情,但是不是在参数中进行硬编码,而是将变量放入其中。例如,
final int MAXSTORIESHIGH= 12;
final int MINSTORIESHIGH = 1;
do {
System.out.print("How many stories high the building will be? (1-5): ");
input = in.nextInt();
}while (input < MINSTORIESHIGH || input > MAXSTORIESHIGH);如果要更改最大值和最小值,只需更改变量的值即可。
https://stackoverflow.com/questions/21975127
复制相似问题