Famcraft

Author Topic: More coding issues for eGI  (Read 4293 times)

Offline eGI

  • Retired
  • Hero Member
  • *
  • Posts: 538
    • View Profile
More coding issues for eGI
« Opened 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();
            }
        }
    }
}
Head Coach Middle School Cross Country and Track
Assistant Coach High School Cross Country and Track

I know running... and a few other things...

Offline Stitch 🔷 Jordyn

  • Retired
  • Sr. Member
  • *
  • Posts: 432
  • Professional Blue Koala 🐨
    • View Profile
  • Minecraft Name: StitchAKA626
Re: More coding issues for eGI
« Reply #1 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.")}
 
"Ohana means family, family means nobody gets left behind or forgotten."

Hello! I'm Stitch! A blue alien koala who sometimes plays as a gray African Wild Dog named Layden!

Offline Domsters931

  • Donator
  • Hero Member
  • *
  • Posts: 579
  • I love building
    • View Profile
Re: More coding issues for eGI
« Reply #2 on April 27, 2014, 08:05:05 PM »
this is so cool!! i love reading codong that i don't understand

Offline Kealper

  • Administrator
  • Hero Member
  • *
  • Posts: 1349
    • View Profile
  • Minecraft Name: Kealper
Re: More coding issues for eGI
« Reply #3 on April 28, 2014, 12:11:46 AM (Edited April 28, 2014, 12:13:17 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");
                }
            }
        }
    }
}

Offline eGI

  • Retired
  • Hero Member
  • *
  • Posts: 538
    • View Profile
Re: More coding issues for eGI
« Reply #4 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) + "%.");
    }
}
Head Coach Middle School Cross Country and Track
Assistant Coach High School Cross Country and Track

I know running... and a few other things...

Offline eGI

  • Retired
  • Hero Member
  • *
  • Posts: 538
    • View Profile
Re: More coding issues for eGI
« Reply #5 on April 28, 2014, 12:57:08 AM »
And 6 minutes later I figured out the problem and fixed it and completed the program...
Head Coach Middle School Cross Country and Track
Assistant Coach High School Cross Country and Track

I know running... and a few other things...

Offline mathfreak2

  • Retired
  • Hero Member
  • *
  • Posts: 941
  • integral from 0 to 2sqrt(21) of x wrt x = 42
    • View Profile
Re: More coding issues for eGI
« Reply #6 on April 28, 2014, 05:28:20 AM »
Yay! Sorry I wasn't much help :/
<Thoatt> I was very much into fluid dynamics
(SrMod: ~Rowan) but your opinion on that was... fluid?
<Thoatt> up to ferrofluid in magnetic field dynamics
(SrMod: ~Rowan) that sounds like an attractive field
(SrMod: ~Rowan) I'm going hard on the puns right now XD

Offline MrChris13

  • Retired
  • Hero Member
  • *
  • Posts: 985
    • View Profile
  • Minecraft Name: MrChris13
Re: More coding issues for eGI
« Reply #7 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

Offline Domsters931

  • Donator
  • Hero Member
  • *
  • Posts: 579
  • I love building
    • View Profile
Re: More coding issues for eGI
« Reply #8 on April 28, 2014, 09:53:58 AM »
I meant coding