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