如何打印“Unoccupied”?

时间:2014-01-19 01:49:01

标签: java arrays

我应该写一个程序,提示用户进入被占用的酒店房间。完成后,用户输入-1并提示输入随机酒店编号。如果酒店房间被占用,则会打印出来。如果房间空置,打印机无人占用。我似乎无法弄清楚为什么无人无法打印。建议?

import java.util.Arrays;
import java.util.Scanner;

public class GoughAndreaChapter9
{
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int [] arr=new int[100];
        int counter=0;
        int currval=0;

        System.out.println("Please enter an occupied hotel room number, -1 to quit ");

        do
        {
            currval = sc.nextInt();
            if(currval==-1)
                break;
            if(currval>0)
                arr[counter++]=currval;
        }
        while(currval !=-1);

        // sort using java API
        int [] temparr=new int[counter];
        for(int i = 0; i<counter; i++)
        {
            temparr[i] = arr[i];
        }
        arr = temparr;
        Arrays.sort(arr);

        //binary search.
        int low=0;
        int high = counter-1;
        System.out.println("Please enter a room to search for:  ");
        currval = sc.nextInt();
        int status=0;
        int mid;

        while(low<high)
        {
            if(arr[low]==currval)
            {
                System.out.println("Occupied");
                status=1;
                break;
            }

            else if(arr[high]==currval)
            {
                System.out.println("Occupied");
                status=1;
                break;
            }

            mid = low+high/2;

            if(arr[mid]==currval)
            {
                System.out.println("Occupied");
                status=1;
                break;
            }
            else if(arr[mid]<currval)
            {
                low=mid;
            }
            else if(arr[mid]<currval)
            {
                high = mid;
            }
        }

        if(status==0)
            System.out.println("Unoccupied");
    }
}

2 个答案:

答案 0 :(得分:0)

请勿晕眩!这样做:

Room.java

public class Room {
    private boolean isOccupied;

    public Room() {
        this.isOccupied = false;
    }

    public boolean obtainTheRoom() {
        if(!isOccupied) this.isOccupied = true;
        return !isOccupied;
    }
}

Main.java

import java.util.Scanner;

public final class Main {
    private static final int ROOM_AMOUNT = 50;
    private static int actualRoom;
    private static Scanner cmdin = new Scanner(System.in);

    public static void main(String[] args) {
        Room[] rooms = new Room[ROOM_AMOUNT];

        // Select some random, but static rooms to be occupied
        for(int i = 1; i <= ROOM_AMOUNT; i++) {
            if(i % 3 - 1 == 0 || i * 2 % i + 10 - 2 == 2) {
                rooms[i - 1].obtainTheRoom();
            }
        }

        for(;;) {
             System.out.print("Enter a room number:\t");
             try {
                 actualRoom = Integer.parseInt(cmdin.next());
             } catch(NumberFormatException nfe) {
                 loopRoomNumber();
             }

             if(rooms[actualRoom - 1].obtainTheRoom()) {
                 System.out.println("Got the room " + actualRoom + "! Now it's occupied -_-");
             } else {
                 System.out.println("Room Occupied!");
             }
        }
    }

    private static void loopRoomNumber() {
        System.out.print("That's not a valid room number!\n\n");

        try {
            actualRoom = Integer.parseInteger(cmdin.next());
        } catch(NumberFormatException nfe) {
            loopRoomNumber();
        }
    }
}

这应该有用。祝你好运!

答案 1 :(得分:0)

我会更改你的最后一个if语句,如下所示。这会奏效。因为否则你会无限循环;

else if (arr[mid] < currval)

        {

            low = mid;
            --high;
        }

        else if (arr[mid] > currval)

        {

            high = mid;
            ++low;
        }

请注意,当中间值小于当前值时,我已经递减了高,当中间值大于当前值时,我递增了低值