Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Nested while loops problem - when yes is being selected at the end, program needs to re-run code

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Nested while loops problem - when yes is being selected at the end, program needs to re-run code

    public class BTU_Calc_V1 {
    // declaring variables
    private static int roomnumber;
    private static int countr;
    private static double length;
    private static double width;
    private static double height;
    private static double volume;
    private static double totvolume;
    private static char uom;
    private static double BTUs;
    private static int selection;
    private static double totBTUs;
    private static double Room_1_BTUs;
    private static double Room_2_BTUs;
    private static double Room_3_BTUs;
    private static double Room_4_BTUs;
    private static double Room_5_BTUs;
    private static double Room_6_BTUs;
    private static char More_Rooms;
    public static void main(String[] args) {


    System.out.print("Enter the amount of rooms required ");
    roomnumber = Keyboard.readInt();
    System.out.println("counter="+countr+"roomnumber=" + roomnumber);

    while(countr==roomnumber)
    {


    System.out.print("Enter the unit of measure metres 'M 'or feet 'F' ");
    uom = Keyboard.readChar();

    for (countr = 1; countr <= roomnumber; countr++) {
    System.out.print("What is the length in " + uom + " ");
    length = Keyboard.readDouble();
    System.out.print("What is the width in " + uom + " ");
    width = Keyboard.readDouble();
    System.out.print("What is the height in " + uom + " ");
    height = Keyboard.readDouble();
    volume = length * width * height;
    //totvolume = volume + totvolume;
    //System.out.println("Volume is " + volume);
    System.out.println("");
    System.out.println("The Following is a list of room types");
    System.out.println("");
    System.out.println("1. Bedroom upper floor built \n2. Bedroom upper floor roof \n3. Kitchen upper floor built\n4. Kitchen upper floor roof\n5. Hot room");
    System.out.println("");
    System.out.print("Chose one of the above");
    System.out.println("");
    selection = Keyboard.readInt();

    switch (uom) {

    case 'M': {
    if (selection == 1) {
    BTUs = volume * 212;
    } else if (selection == 2) {
    BTUs = volume * 229.54;
    } else if (selection == 3) {
    BTUs = volume * 247.2;
    } else if (selection == 4) {
    BTUs = volume * 264.85;
    } else {
    BTUs = volume * 282.52;
    }

    totBTUs = BTUs + totBTUs;

    if (countr == 1)
    Room_1_BTUs = BTUs;
    }
    if (countr == 2) {
    Room_2_BTUs = BTUs;
    }
    if (countr == 3) {
    Room_3_BTUs = BTUs;
    }
    if (countr == 4) {
    Room_4_BTUs = BTUs;
    }
    if (countr == 5) {
    Room_5_BTUs = BTUs;
    }
    if (countr == 6) {
    Room_6_BTUs = BTUs;

    }
    break;

    case 'F': {

    if (selection == 1) {
    BTUs = volume * 6;
    } else if (selection == 2) {
    BTUs = volume * 6.5;
    } else if (selection == 3) {
    BTUs = volume * 7;
    } else if (selection == 4) {
    BTUs = volume * 7.5;
    } else {
    BTUs = volume * 8;
    }

    if (countr == 1)
    Room_1_BTUs = BTUs;
    }
    if (countr == 2) {
    Room_2_BTUs = BTUs;
    }
    if (countr == 3) {
    Room_3_BTUs = BTUs;
    }
    if (countr == 4) {
    Room_4_BTUs = BTUs;
    }
    if (countr == 5) {
    Room_5_BTUs = BTUs;
    }
    if (countr == 6) {
    Room_6_BTUs = BTUs;
    totBTUs = BTUs + totBTUs;
    break;
    }

    }
    System.out.println(countr + " " + roomnumber);

    }


    System.out.println("Y. Yes \nN. No ");
    More_Rooms = Keyboard.readChar();

    {
    switch (More_Rooms) {
    case 'Y': {

    roomnumber=roomnumber+1;
    System.out.println("counter="+countr+"roomnumber=" + roomnumber);



    break;
    }
    case 'N': {
    System.out.println("Room 1 " + Room_1_BTUs);
    System.out.println("Room 2 " + Room_2_BTUs);
    System.out.println("Room 3 " + Room_3_BTUs);
    System.out.println("Room 4 " + Room_4_BTUs);
    System.out.println("Room 5 " + Room_5_BTUs);
    System.out.println("Room 6 " + Room_6_BTUs);
    System.out.println("Total BTUs are " + totBTUs);
    break;
    }
    case 3:
    JOptionPane.showMessageDialog(null, "Invald slection please select Y(Yes) or N(No)");
    break;

    }

    }

    }


    }
    }
    Last edited by PATRICK_AGIUS; December 8th, 2021 at 09:03 AM. Reason: Still learning while loops

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Nested while loops problem - when yes is being selected at the end, program needs to re-run code

    What happens when you compile and execute the code?
    Can you copy the contents of the command prompt window and paste it here so I can see what happens. Add some comments where the results are not what you expected.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Nested loops
    By Rookie in forum Loops & Control Statements
    Replies: 7
    Last Post: May 14th, 2014, 04:11 PM
  2. Replies: 2
    Last Post: February 24th, 2014, 10:48 PM
  3. Re: Nested for loops
    By capt_jaggy in forum Loops & Control Statements
    Replies: 5
    Last Post: March 18th, 2013, 05:21 AM
  4. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 11:43 PM
  5. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 03:31 PM