您好,我正在创建一个程序,要求用户在程序中输入多少个矩形。然后,我在for
循环中使用这个数字,循环时询问用户每个矩形的名称以及矩形上2个角的坐标。在这个for
循环中,在用户输入所有这些数据之后,我想比较这些点以找到哪个矩形具有最大/最小面积和周长而不使用数组。这是我的代码的一部分:如何在不使用数组的情况下比较for循环中生成的值
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
process(s);
}
public static void process(Scanner s) {
System.out.println("Please enter the number of rectangles as an integer");
int rectnum;
rectnum = s.nextInt();
for(int i = 0; i<rectnum; i++){
System.out.println("PLease enter the rectangle's name as a single letter");
String rectname;
rectname = s.next();
System.out.println("Please enter the x value of one coordinate
of the rectangle");
int xcoor1;
xcoor1 = s.nextInt();
System.out.println("Please enter the y value of the same coorinate
of the rectangle");
int ycoor1;
ycoor1 = s.nextInt();
System.out.println("Please enter the x value of annother
coordinate of the rectangle");
int xcoor2;
xcoor2 = s.nextInt();
System.out.println("Please enter the y value of the same
coordinate of the rectangle");
int ycoor2;
ycoor2 = s.nextInt();
int Perim;
Perim = 2 *(java.lang.Math.abs(ycoor1 - ycoor2)+
java.lang.Math.abs(xcoor1 - xcoor2));
int Area;
Area = java.lang.Math.abs((ycoor1 - ycoor2) * (xcoor1 - ycoor2));
/*
* ....
*/
}}
尝试为循环外的区域和边界创建最大/最小变量,并在发现新区域时更新它们。 – kirbyquerby
为什么你不想使用数组?你需要在处理方法之外的矩形,还是只需要最小/最大矩形? –
我会用什么样的代码来更新它?你能举个例子吗?谢谢 – user3440080