简体   繁体   English

使用扫描仪用户输入功能创建对象

[英]Creating objects in function of the Scanner user input

So I have this code which creates 2 rectangles and returns the length, width and color . 所以我有这段代码可以创建2个矩形,并返回长度,宽度和颜色。
However, I would like to make it so that it creates the number of rectangles the user inputs in the "nbRect" variable. 但是,我想这样做,以便创建用户在“ nbRect”变量中输入的矩形数。 Here is my code : 这是我的代码:

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());

}

} class Rectangle { }类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;
}

I have modified a few things within your code. 我已经在您的代码中修改了一些内容。 I have tested it and it should work as you'd expect. 我已经对其进行了测试,它应该可以按预期工作。

package ca.qc.bdeb.info202.exercises;



import java.util.*;

   public class TestRectangle {
     public static void main(String[] args) {
    Scanner read = new Scanner(System.in);
    double longeur;
    double largeur;
    String couleur;
    int nbRect;
    List<Rectangle> rectangles = new ArrayList<Rectangle>();

    System.out.println("How many    rectangles do you want to create ?");
    nbRect = read.nextInt();

    for(int i = 1; i <= nbRect ; i++){
       Rectangle rect1 = new Rectangle(); 
       System.out.println("Enter the color of the rectangle : ");
       couleur = read.next();
       rect1.setCouleur(couleur);

       System.out.println("Enter the length of the rectangle : ");
       longeur = read.nextInt();
       rect1.setLong(longeur);

       System.out.println("Enter the width of the rectangle : ");
       largeur = read.nextInt();
       rect1.setLarg(largeur);

       rectangles.add(rect1);
    }

    int rectNumber = 1;
    for(Rectangle rect : rectangles){
      System.out.println("Rectangle: "+ rectNumber +" -- Long: " + rect.getLong() + ", Larg: " + rect.getLarg() + ", Couleur: " +   rect.getCouleur());
      rectNumber++;
    }
}

} }

class 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;
}

public void setCouleur(String couleur){
  this.couleur = couleur;
}

public void setLong(double longeur){
    this.longeur = longeur;
}

public void setLarg(double largeur){
    this.largeur = largeur;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM