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