-
Mod and If Statement
Hi there,
For some reason I can't get my mod to make my variable in the if statement to go up a number. Any help would be appreciated!! it is suppose to add the remainder (if any) and make the numberOfRolls go up by one, did I not do set it correctly? Do you need to see more of the code? 35 is the sqft of the roll btw, and you can only order a whole roll at a time.
Code:
numberOfRolls = (roomExtra/35.0)+(roomExtra%35.0);
if(numberOfRolls >0)
{
numberOfRolls = numberOfRolls +1;
}
System.out.println("The total number of rolls needed: " + f.format(numberOfRolls));
-
Re: Mod and If Statement
Hello,
I'm sorry, but the problem statement doesn't appear to be very clear to me. As long as your entire code isn't too long, could you please paste the entire code? I mean, if roomExtra was 71, then numberOfRolls would be 2+1 = 3. Then add another?
-
Re: Mod and If Statement
Absolutely! Thank you so much for looking at it!!
Code:
import java.util.Scanner;
import java.text.DecimalFormat;
public class WallpaperRolls
{
public static void main(String[] args)
{
//Create Scanner object
Scanner input = new Scanner(System.in);
//Create variables
float lengthOfRoom;
float heightOfRoom;
float widthOfRoom;
double roomArea, totalArea;
double roomExtra;
double numberOfRolls;
double totalCost, pricePerRoll;
DecimalFormat f = new DecimalFormat("0.00");
//Get room dimensions
System.out.print("What is the length of the room (in ft)?: ");
lengthOfRoom = input.nextFloat();
System.out.print("What is the height of the room (in ft)?: ");
heightOfRoom = input.nextFloat();
System.out.print("What is the width of room (in ft)?: ");
widthOfRoom = input.nextFloat();
// Get price per roll
System.out.print("What is the price of the wallpaper?:$");
pricePerRoll = input.nextDouble();
//Calculate area
roomArea = ((2 * lengthOfRoom * heightOfRoom) + (2 * lengthOfRoom * widthOfRoom));
//Calculate mistakes
roomExtra = ( roomArea*1.10);
//Calculate number of rolls
numberOfRolls = (roomExtra/35.0)+(roomExtra%35.0);
if(numberOfRolls >0)
{
numberOfRolls = numberOfRolls +1;
}
System.out.println("The total number of rolls needed: " + f.format(numberOfRolls));
//Calculate total area
totalArea = roomArea;
System.out.println("The total area of room is " + f.format(totalArea));
//Calculate total cost
totalCost = (numberOfRolls * pricePerRoll);
System.out.println("The total cost of wallpaper:$" + f.format(totalCost));
}
}
-
Re: Mod and If Statement
Thanks for posting the code. It appears to work as required for me (adds an additional roll inside the if statement). The result I got was 9.14 rolls. I used the following inputs as test conditions.
length = 5
width = 5
height = 5
roomExtra = 110
110/35 = 3.14
110%35 = 5
3.14 + 5 = 8.14
-
Re: Mod and If Statement
Ok! thank you very much! I guess I just thought that it should be a whole number and not a decimal number, so I thought that it wasn't working properly. thank you so much for your help!
-
Re: Mod and If Statement
I've looked at your code a little more closely and something doesn't make sense to me. For example, if my roomExtra was 72 sqft, you would really just need 3 rolls, right? If so, your code would calculate that you would need 5 rolls. Am I missing something here?
-
Re: Mod and If Statement
hhhmmm...I didnt realize that! thank you for pointing that out, any suggestions? i am not sure how that is happening. I am very new to Java. Thank you so much for catching that!
-
Re: Mod and If Statement
//Calculate number of rolls
numberOfRolls = (int) (roomExtra/35.0); //round down to integer
if(roomExtra%35.0 >0) if remainder, add a roll
{ numberOfRolls = numberOfRolls +1; }
-
Re: Mod and If Statement
You are AMAZING!!! thank you so much!!!!!! I can't tell you how much I appreciate it!! have a wonderful night!