使用while循环时验证用户的输入

时间:2013-10-29 04:36:43

标签: validation loops input while-loop csc

我的问题是我正在尝试通过确保while循环只需要超过10的数字来验证我的输入。如果没有,他们必须再试一次。但我遇到的问题是,如果我关闭while循环,循环会不断重复。

import java.util.Scanner;
/**
* @(#)Hotel_Occupancy.java
*
* Hotel_Occupancy application
*
* @author 
* @version 1.00 2013/10/28
*/

public class Hotel_Occupancy {

    public static void main(String[] args) {

        //Declare Variables
        int floors;
        int rooms;
        int roomsOccupied;
        int roomsVacant;
        int occupancyRate;

        //Create Scanner Object
        Scanner keyboard = new Scanner(System.in);

        //Ask user for # of floors 
        System.out.print("Enter floors? Input number more than 1. ");
            floors = keyboard.nextInt();

        while (floors < 1 && floors != 10 )
        {   
            System.out.println("ENTER VALID NUMBER.");
            System.out.println("Enter floors? Input number more than 1. ");
            System.;

            //Create loop with floor iteration
            for (rooms = 1; rooms <= floors; rooms++)
            {   
                System.out.println("How many rooms? ");
                rooms = keyboard.nextInt();
            }

            for (roomsOccupied = 1; roomsOccupied <= rooms; roomsOccupied++)
            {   
                System.out.println("How many rooms occupied? ");
                roomsOccupied = keyboard.nextInt();
                roomsVacant = rooms - roomsOccupied;
                System.out.println("Rooms Vacant" + roomsVacant);
            }

            //Calculate Occupancy Rate
            occupancyRate = roomsOccupied + rooms;
            System.out.println("Occupancy rate is " + occupancyRate);
        }
    }
}       

1 个答案:

答案 0 :(得分:0)

while(floor >=10){ // Do whatever you want } 完成此基本while loop tutorial