gpt4 book ai didi

java - 循环?我需要弄清楚如何解决这个问题。

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

当我运行我的程序并尝试添加输入时。一直提示输入无效。再次输入。我知道为什么 b/c 我添加了一个 while 循环,但那是针对小于 1 的输入。我该如何解决这个问题?我需要显示入住率和空置房间数量。我是初学者,所以我认为我犯了一个愚蠢的错误。有人可以帮助我吗?

import java.util.Scanner;

public class hotelOccupancy
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

int floors, numFloors, rooms,totalRoomsOccupied,roomsOccupied, vacant;
double totalRooms;


System.out.println("Enter number of floors");
floors = input.nextInt();

while(floors < 1);
{
System.out.println("Invalid input. Enter again");
}
floors = input.nextInt();

numFloors = input.nextInt();
for(floors = 1; floors <= numFloors; floors++)

{
System.out.println("Enter number of rooms in floor");
rooms = input.nextInt();

while(rooms < 1);
{
System.out.println("Invalid entry. Enter again");
}

System.out.println("Enter number of rooms occupied for floor" + floors);
roomsOccupied = input.nextInt();

totalRoomsOccupied = input.nextInt();
totalRoomsOccupied = totalRoomsOccupied + roomsOccupied;

}

rooms = input.nextInt();
totalRoomsOccupied = input.nextInt();


vacant = rooms - totalRoomsOccupied;

totalRooms = input.nextDouble();
double occupancyRate = totalRoomsOccupied/totalRooms;

System.out.println("The number of rooms vacant are" + vacant);
System.out.println("Then occupancy rate is" + occupancyRate);

最佳答案

您必须移动 rooms = input.nextInt();进入while(rooms < 1)循环。

关于java - 循环?我需要弄清楚如何解决这个问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19740995/

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