Results 1 to 9 of 9
Thread: Mod and If Statement
- 03-25-2012, 07:46 AM #1
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
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.
Java 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));
- 03-25-2012, 08:00 AM #2
Member
- Join Date
- Feb 2012
- Location
- Phoenix, AZ
- Posts
- 26
- Rep Power
- 0
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?
- 03-25-2012, 08:05 AM #3
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
Re: Mod and If Statement
Absolutely! Thank you so much for looking at it!!
Java 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)); } }
- 03-25-2012, 08:27 AM #4
Member
- Join Date
- Feb 2012
- Location
- Phoenix, AZ
- Posts
- 26
- Rep Power
- 0
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
- 03-25-2012, 08:30 AM #5
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
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!
- 03-25-2012, 08:37 AM #6
Member
- Join Date
- Feb 2012
- Location
- Phoenix, AZ
- Posts
- 26
- Rep Power
- 0
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?
- 03-25-2012, 08:41 AM #7
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
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!
- 03-25-2012, 08:57 AM #8
Member
- Join Date
- Feb 2012
- Location
- Phoenix, AZ
- Posts
- 26
- Rep Power
- 0
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; }
- 03-25-2012, 09:00 AM #9
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
for statement
By mattcash83 in forum New To JavaReplies: 8Last Post: 10-18-2011, 03:48 AM -
need help in sql statement
By chyeeqi in forum JDBCReplies: 6Last Post: 03-28-2010, 08:49 PM -
if statement help please!!
By soc86 in forum New To JavaReplies: 5Last Post: 12-02-2008, 03:56 PM -
If statement, please help??
By soc86 in forum New To JavaReplies: 5Last Post: 11-23-2008, 03:58 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 05:45 PM
Bookmarks