gpt4 book ai didi

java - 我对这个 Java 构造函数程序有疑问

转载 作者:行者123 更新时间:2023-12-01 11:11:17 25 4
gpt4 key购买 nike

这是类(class):

public class Rectangle { 

private double lenght ;
private double width ;
public String name ;

public Rectangle (double len, double w, String n)

{
lenght = len;
width = w;
name = n;
}

public double getLenght()
{
return lenght ;
}

public double getWidth ()
{
return width ;
}

public String getName()
{
return name ;
}

public double getArea ()
{
return lenght * width ;
}

}

这是我的构造函数:

public class roomConstructor {

public static void main(String[] args) {


double roomLenght, roomWidht, totalArea;
String roomName;

Rectangle kitchen, bedroom, den;

Scanner keyboard = new Scanner (System.in);
System.out.println("What is the name of your kitchen? " );
roomName = keyboard.nextLine();
System.out.println("What is the lenght of the kitchen ? " );
roomLenght = keyboard.nextDouble();
System.out.println("What is the widht of the kitchen ? ");
roomWidht =keyboard.nextDouble();
kitchen = new Rectangle(roomLenght, roomWidht, roomName );

System.out.println("What is the name of your bedroom? " );
roomName = keyboard.nextLine();
keyboard.nextLine();
System.out.println("What is the lenght of the bedroom ? ");
roomLenght = keyboard.nextDouble();
System.out.println("What is your bedroom withd ? ");
roomWidht = keyboard.nextDouble();
bedroom = new Rectangle(roomLenght,roomWidht, roomName );

System.out.println("What is the name of your den? " );
roomName = keyboard.nextLine();
keyboard.nextLine();
System.out.println(" What is the lenght of your den ? ");
roomLenght =keyboard.nextDouble();
System.out.println("What is the widht of your den ? ");
roomWidht =keyboard.nextDouble();
den = new Rectangle ( roomLenght, roomWidht,roomName);

totalArea = kitchen.getArea() + bedroom.getArea() + den.getArea();

System.out.println(" The total area of your room is : " + totalArea);
System.out.println(" The name of your kitchen is : " + kitchen.getName());
System.out.println(" The name of your kitchen is : " + bedroom.getName());
System.out.println("The name of your bedroom is : " + den.getName());
System.out.println("The lenght of the kitchen is : " + bedroom.getLenght());


}

}

当我尝试获取卧室或书房名称时,缺少我得到的所有名称答案是厨房,然后有 2 个空白答案 5l 缺少卧室和书房名称)我该怎么办?感谢您的帮助

最佳答案

Keyboard.nextDouble() 不使用 newLine 字符。每次调用 Keyboard.nextDouble() 后调用 Keyboard.nextLine()

这会破坏构造函数中的数据存储,因为在不使用 \n 字符时输入不匹配

关于java - 我对这个 Java 构造函数程序有疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32337084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com