Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在理解类和对象java编程的数组时感到困惑?_Java_Arrays_Input - Fatal编程技术网

在理解类和对象java编程的数组时感到困惑?

在理解类和对象java编程的数组时感到困惑?,java,arrays,input,Java,Arrays,Input,我想编写一个测试程序,要求用户输入房间的数量、房间的尺寸以及所需地毯的每平方英尺价格。然后显示地毯的总成本。 我不确定如何将数组中的数据输入到这两个类中,然后计算成本 任何帮助都将不胜感激!谢谢 这是我的第一节课 public class RoomCarpet { private RoomDimension size; private double CarpetCost; public RoomCarpet(RoomDimension dim, doubl

我想编写一个测试程序,要求用户输入房间的数量、房间的尺寸以及所需地毯的每平方英尺价格。然后显示地毯的总成本。 我不确定如何将数组中的数据输入到这两个类中,然后计算成本 任何帮助都将不胜感激!谢谢

这是我的第一节课

  public class RoomCarpet
  {
     private RoomDimension size;
     private double CarpetCost;

     public RoomCarpet(RoomDimension dim, double cost)
     {
        size = dim;
        CarpetCost = cost;
     }

    public double getCarpetCost()
    {
       return CarpetCost;
    }

    public RoomDimension getSize()
    {
       return size;
    }

    public void setCost(double CarpetCost)
    {
      this.CarpetCost = CarpetCost;
    }
    public void setSize(RoomDimension size)
    {
     this.size = size;
    }

   //calculate total cost
   public double getTotalCost()
   {
     return size.getArea() * CarpetCost;
   }

  //display the total cost
   public String toString()
   {
     String str = "Total Cost: " + getTotalCost();

     return str;
   }


   }
二等舱

 public class RoomDimension
 {
   private double length;
   private double width;

   public RoomDimension(double len, double wid)
   {
      length = len;
      width = wid;
   }

   public double getLength()
   {
      return length;
   }

   public double getWidth()
   { 
     return width;
   }

   public void setLength(double length)
   {
      this.length = length;
   }

   public void setWidth(double width)
   {
      this.width = width;
   }

   //calculate the area of dimensions
   public double getArea()
   {
     return length * width;
   }

   //print out length, width, and area
   public String toString()
   {
      String str = "Length: " + length +
               "\nWidth: " + width + 
               "\nArea: " + getArea();

     return str;
   } 
测试类(帮助!!:()

import java.util.Scanner;
导入java.io.*;
公共类测试程序
{
公共静态void main(字符串[]args)
{
房间号;
双倍长度;
双倍宽度;
双重成本;
扫描仪键盘=新扫描仪(System.in);
System.out.println(“房子里有多少个房间?”);
roomNumber=键盘.nextInt();
RoomDimension[]房间=新的RoomDimension[roomNumber];
对于(int index=0;index
那么您的意思是使用main方法中的args参数?。 您可以定义如下输入:

房间宽度X长度成本

然后可以使用拆分参数:

for(String arg:args){
    StringTokenizer st = new StringTokenizer("-");
    int rooms = Integer.parseInt(st.nextToken());
    String dimmensions = st.nextToken();
    double width = Integer.parseInt(dimmensions.substring(0,dimmensions.indexOf("X")-1));
    double length = Integer.parseInt(dimmensions.substring(dimmensions.indexOf("X")+1,dimmensions.length));
    double cost = Double.parseDouble(st.nextToken());

    //pass parameters to the classes and then execute your method
    RoomDimension dim = new RoomDimension (length, width );
    RoomCarpet carpet = new RoomCarpet(dim, cost);
    System.out.println("Total cost: "+ (rooms * carpet.getTotalCost()));

}    

我知道有更好的方法可以做到这一点,比如使用regexp,但我对它非常熟悉。而且regexp可以让您验证输入。

我会在您的主方法中完成这项工作,首先提取常量-也就是

// Constant questions.
private static final String HOW_MANY_ROOMS = "How many rooms are there in the house? ";
private static final String HOW_LONG = "Enter the length of room #%d: ";
private static final String HOW_WIDE = "Enter the width of room #%d: ";
private static final String HOW_MUCH = "Enter the cost per square foot for room #%d: ";

public static void main(String[] args) {
  Scanner keyboard = new Scanner(System.in);
  try {
    System.out.println(HOW_MANY_ROOMS);
    int roomNumber = keyboard.nextInt(); // got the room count.

    RoomCarpet[] carpets = new RoomCarpet[roomNumber]; // carpet array.

    for (int index = 0; index < roomNumber; index++) {
      System.out.printf(HOW_LONG, 1 + index);
      double len = keyboard.nextDouble();

      System.out.printf(HOW_WIDE, 1 + index);
      double wid = keyboard.nextDouble();
      RoomDimension dim = new RoomDimension(len, wid);

      System.out.printf(HOW_MUCH, 1 + index);
      carpets[index] = new RoomCarpet(dim,
          keyboard.nextDouble());
      System.out.println(carpets[index]);
    }
  } finally {
    keyboard.close(); // Close your scanner.
  }
}
//不断的问题。
private static final String HOW_MANY_ROOMS=“房子里有多少个房间?”;
private static final String HOW_LONG=“输入房间长度#%d:”;
private static final String HOW_WIDE=“输入房间的宽度#%d:”;
private static final String HOW_moment=“输入房间的每平方英尺成本#%d:”;
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
试一试{
System.out.println(有多少个房间);
int roomNumber=keyboard.nextInt();//获取房间计数。
Room地毯[]地毯=新的Room地毯[roomNumber];//地毯阵列。
对于(int index=0;index
您想根据用户输入创建一个
Room地毯
对象的数组。因此,您已经获得了
长度
宽度
成本
,您能想出如何将其转换为填充的
Room地毯
对象吗?一旦有了它,请将其添加到数组中。任何示例都会非常有用,谢谢!
// Constant questions.
private static final String HOW_MANY_ROOMS = "How many rooms are there in the house? ";
private static final String HOW_LONG = "Enter the length of room #%d: ";
private static final String HOW_WIDE = "Enter the width of room #%d: ";
private static final String HOW_MUCH = "Enter the cost per square foot for room #%d: ";

public static void main(String[] args) {
  Scanner keyboard = new Scanner(System.in);
  try {
    System.out.println(HOW_MANY_ROOMS);
    int roomNumber = keyboard.nextInt(); // got the room count.

    RoomCarpet[] carpets = new RoomCarpet[roomNumber]; // carpet array.

    for (int index = 0; index < roomNumber; index++) {
      System.out.printf(HOW_LONG, 1 + index);
      double len = keyboard.nextDouble();

      System.out.printf(HOW_WIDE, 1 + index);
      double wid = keyboard.nextDouble();
      RoomDimension dim = new RoomDimension(len, wid);

      System.out.printf(HOW_MUCH, 1 + index);
      carpets[index] = new RoomCarpet(dim,
          keyboard.nextDouble());
      System.out.println(carpets[index]);
    }
  } finally {
    keyboard.close(); // Close your scanner.
  }
}