0
所以我有这个代码创建2个矩形并返回长度,宽度和颜色。
但是,我想使它创建用户在“nbRect”变量中输入的矩形数。 这里是我的代码:创建扫描仪用户输入功能的对象
package ca.qc.bdeb.info202.exercises;
import java.util.Scanner;
public class TestRectangle {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
double longeur;
double largeur;
String couleur;
int nbRect;
System.out.println("How many rectangles do you want to create ?");
nbRect = read.nextInt();
Rectangle rect1 = new Rectangle(); //Here is the problem i think, i want it to make as many rectangles as the "nbRect" variable value.
System.out.println("Enter the color, the length and the width of the rectangle : ");
couleur = read.nextLine();
rect1.setCouleur(couleur);
longeur = read.nextInt();
rect1.setLong(longeur);
largeur = read.nextInt();
rect1.setLarg(largeur);
System.out.println("Rectangle 1 -- Long: " + rect1.getLong() + ", Larg: " + rect1.getLarg() + ", Couleur: " + rect1.getCouleur());
}
} 类Rectangle {
private double largeur = 1;
private double longeur = 1;
private String couleur = "white";
public Rectangle() {
}
public Rectangle(double largeur, double longeur, String couleur) {
this.couleur = couleur;
this.largeur = largeur;
this.longeur = longeur;
}
public double getLarg() {
return largeur;
}
public double getLong() {
return longeur;
}
public String getCouleur() {
return couleur;
}
使用集合类型,以及某些种类的循环。 – OldProgrammer