Search code examples
javawhile-loopnested-loops

Trying to get the summation of my int inputs for a while loop


I'm trying ask the user to input the floors of a hotel and have them input the number of rooms on each floor and then the rooms of those that are occupied. If i inputted in that I had a total of two floors in my hotel and for the second floor I inputted that it had 20 rooms in total, then my system output would return the 20 rooms. I'm trying to see if there's anyway of a summation for all of my inputs or something that would do a similar job so I could get the actual answer. Also, I'm a very new beginner so I'll try to understand any any comments, but I might not. Thanks!

  Scanner keyboard= new Scanner(System.in);

  int numfloors;
  int numrooms=0;
  int totalrooms=0;
  int occupiedrooms=0;
  int number=1;
  int secnumber=10;

  System.out.println("How many floors are in your hotel?");
  numfloors=keyboard.nextInt();

  while(number<=numfloors && numfloors>1)

  {

     System.out.println("How many rooms are on floor"+number+"?");
     numrooms=keyboard.nextInt();
     number++;

  while(secnumber<=numrooms)   
  {
     System.out.println("How many of those rooms are occupied?");
     occupiedrooms=keyboard.nextInt();

    break; 
  } 

  }

     System.out.println("The hotel has "+totalrooms+" total rooms");
     System.out.println("The hotel has "+occupiedrooms+" total occupied rooms");
     System.out.println("The hotel has "+(numrooms-occupiedrooms)+" vacant rooms");
     System.out.println("The hotel has an occupancy rate of %"+(occupiedrooms/numrooms));


 }
}

Solution

  • Use +=. For example:

    int a = 10;  // a is 10
    a += 5;      // a is 15 (This line is equal to a = a + 5)
    a *= 2;      // a is 30 now (same as a = a * 2)