Famcraft

General Category => General Discussion => Topic started by: eGI on April 27, 2014, 04:20:06 PM

Title: More coding issues for eGI
Post by: eGI on April 27, 2014, 04:20:06 PM
So this is just the part of the code I've gotten done so far, but there are some big problems with it.  The biggest thing is after I input the number of floors, it then asks me for rooms on the 1st floor and then goes to an endless loop of me being able to input numbers but never asking for anything out of it.  I need the program to ask me for the number of rooms on each floor after inputting the number of floors.  Any help would be greatly appreciated.

import java.util.Scanner;

public class HotelOccupancy
{
    public static void main(String[] args)
    {
        final int MIN_FLOORS = 2;
        final int MIN_ROOMS = 11;
        int floors;
        int rooms;
        double totalrooms;
        int occupied;
        int vacant;
       
        Scanner keyboard = new Scanner(System.in);
       
        System.out.print("Enter the number of floors the hotel has: ");
        floors = keyboard.nextInt();
        while (floors > MIN_FLOORS);
        {
            System.out.println("You should have at least " + MIN_FLOORS + " for the hotel.");
            System.out.print("Enter the number of floors the hotel has: ");
            floors = keyboard.nextInt();
        }
        for (int i = 1; i <= floors; i++)
        {
            System.out.print("Enter the number of rooms on floor " + i + ": ");
            rooms = keyboard.nextInt();
            while (rooms > MIN_ROOMS);
            {
                System.out.println("You should have at least " + MIN_ROOMS + " for each floor");
                System.out.print("Enter the number of rooms on floor " + i + ": ");
                rooms = keyboard.nextInt();
            }
        }
    }
}
Title: Re: More coding issues for eGI
Post by: Stitch 🔷 Jordyn on April 27, 2014, 07:18:58 PM
IF
   0 > 1
      {Console.system.print.("Logic Bro")}
ELSE
   {Console.system.print.("My brames are too fried for mafs today.")}
Title: Re: More coding issues for eGI
Post by: Domsters931 on April 27, 2014, 08:05:05 PM
this is so cool!! i love reading codong that i don't understand
Title: Re: More coding issues for eGI
Post by: Kealper on April 28, 2014, 12:11:46 AM
There were a few things that were iffy about this, although I'll highlight the big ones.

The while loops were terminated with a ; when they shouldn't have been, which took a bit for me to spot honestly, and it was making everything else not make any sense at all, causing strange issues all over the place.
The rooms variable had the wrong scope, so once you set it the first time, it was not acting correct any other time after the first time.

Sticking with DRY (Don't Repeat Yourself), I've cleaned up the code a bit so it will always go in to the while loop at least once, and only make the correction message if the data entered is not correct.

Fixed code, tested and working:
Code: [Select]
import java.util.Scanner;

public class HotelOccupancy
{
    public static void main(String[] args)
    {
        final int MIN_FLOORS = 2;
        final int MIN_ROOMS = 11;
        int floors = 0;
        double totalrooms;
        int occupied;
        int vacant;
       
        Scanner keyboard = new Scanner(System.in);
       
        while (floors < MIN_FLOORS)
        {
            System.out.print("Enter the number of floors the hotel has: ");
            floors = keyboard.nextInt();
            if (floors < MIN_FLOORS)
            {
                System.out.println("You should have at least " + MIN_FLOORS + " for the hotel.");
            }
        }
        int i;
        for (i = 1; i <= floors; i++)
        {
            int rooms = 0;
            while (rooms < MIN_ROOMS)
            {
                System.out.print("Enter the number of rooms on floor " + i + ": ");
                rooms = keyboard.nextInt();
                if (rooms < MIN_ROOMS)
                {
                    System.out.println("You should have at least " + MIN_ROOMS + " for each floor");
                }
            }
        }
    }
}
Title: Re: More coding issues for eGI
Post by: eGI on April 28, 2014, 12:50:08 AM
One final problem, the output for the occupancyrate is always 0.00 and I cannot figure out how to fix that problem.



import java.util.Scanner;
import java.text.DecimalFormat;

public class HotelOccupancy
{
    public static void main(String[] args)
    {
        final int MIN_FLOORS = 2;
        final int MIN_ROOMS = 11;
        int floors = 0;
        int totalrooms;
        int occupied;
        int totaloccupied;
        int vacant;
        int occupancyrate;
       
        Scanner keyboard = new Scanner(System.in);
       
        DecimalFormat percent = new DecimalFormat("#0.00");
       
        while (floors < MIN_FLOORS)
        {
            System.out.print("Enter the number of floors the hotel has: ");
            floors = keyboard.nextInt();
            if (floors < MIN_FLOORS)
            {
                System.out.println("You should have at least " + MIN_FLOORS + " for the hotel.");
            }
        }
        totalrooms = 0;
        totaloccupied = 0;
        int i;
        for (i = 1; i <= floors; i++)
        {
            int rooms = 0;
            while (rooms < MIN_ROOMS)
            {
                System.out.print("Enter the number of rooms on floor " + i + ": ");
                rooms = keyboard.nextInt();
                if (rooms < MIN_ROOMS)
                {
                    System.out.println("You should have at least " + MIN_ROOMS + " for each floor");
                }
            }
            totalrooms += rooms;
            System.out.print("How many rooms are occupied on floor " + i + ": ");
            occupied = keyboard.nextInt();
            if (occupied > rooms)
            {
                System.out.println("You should have no more than " + rooms + " occupied.");
                System.out.print("How many rooms are occupied on floor " + i + ": ");
                occupied = keyboard.nextInt();
            }
            totaloccupied += occupied;
        }
        vacant = (totalrooms - totaloccupied);
        occupancyrate = (totaloccupied / totalrooms);
        System.out.println("The hotel has " + totalrooms + " rooms.");
        System.out.println(totaloccupied + " are occupied.");
        System.out.println(vacant + " are vacant.");
        System.out.println("The occupancy rate of the hotel is " + percent.format(occupancyrate) + "%.");
    }
}
Title: Re: More coding issues for eGI
Post by: eGI on April 28, 2014, 12:57:08 AM
And 6 minutes later I figured out the problem and fixed it and completed the program...
Title: Re: More coding issues for eGI
Post by: mathfreak2 on April 28, 2014, 05:28:20 AM
Yay! Sorry I wasn't much help :/
Title: Re: More coding issues for eGI
Post by: MrChris13 on April 28, 2014, 07:04:41 AM
this is so cool!! i love reading codong that i don't understand


That made me smile so much LOL
Title: Re: More coding issues for eGI
Post by: Domsters931 on April 28, 2014, 09:53:58 AM
I meant coding