• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java is smarter than me. Please help.

SithSolo1

Diamond Member
===== denotes the start and end of a problem area.

public class ConsoleIOTest {
public static void main(String[] args){

System.out.println("This program is designed to calaculate how many boxes of Oaties breakfast");
System.out.println("cereal are contained in one metric ton and the weight of one box of Oaties in");
System.out.println("metric tons. It will also calculate the number of whole cases and extra boxes");
System.out.println("of Oaties in one metric ton.");
System.out.println();
System.out.println("All of this will be done using the weight you supply for one box of Oaties.");
System.out.println();
System.out.println();


double x;
x= 35273.92;

double weight;
===== Here I need to know how to ask them to enter another number if they inter a negative. As it is now it tells them to inter a positive number but it continues on with the calculations. I want it to keep asking them until they enter a positive number or until they have entered 5 or so wrong numbers. If they enter 5 or so wrong numbers I need the program to quit.
System.out.println("Please enter the weight of one box of Oaties ceral in ounces.");
System.out.println("eg.(2, 5.05, 67.2, etc):");
System.out.println();
weight= JLiveRead.readLineDouble();
System.out.println();

if (weight<0){
System.out.println("Please enter a positive number.");
}
=====
===== Here I need to know how to round up to the nearest whole number. I have no clue how I would even start to do that. This would also help on my next problem.
System.out.println("You entered " + weight + " ounces for the weight of one box of Oaties");
System.out.println();
System.out.println("There are " + x/weight
+ " boxes of Oaties in one metric ton.");
System.out.println();
System.out.println("One box of Oaties weighs " + weight/x
+ " metric tons.");
int boxes;
===== Here I need to know how show how many whole cases + extra boxes are in a metric ton. All I have so far is how many cases are in a ton.
System.out.println();
System.out.println("Please enter the number of Oaties boxes in a case.");
System.out.println("eg.(2, 5, 67, etc):");
System.out.println();
boxes= JLiveRead.readLineInt();
System.out.println();

System.out.println("You entered " + boxes + " boxes of Oaties in one case.");
System.out.println();
System.out.println("There are " + x/(boxes*weight)
+ " cases of Oaties in one metric ton.");
=====


}
}


Thx
J

If you don't feel good about doing it for me push me in the right direction with some examples.
 
--snip The following part uses a while loop - until a positive number is entered, it will ask for a new number

weight= JLiveRead.readLineDouble();
System.out.println();

while(weight<0){
System.out.println("Please enter a positive number.");
weight= JLiveRead.readLineDouble();
}

--snip (int) casts onto an integer, but rounds down. +1 makes sure it rounds up.

System.out.println("There are " + (int)(x/weight) +1
+ " boxes of Oaties in one metric ton.");
System.out.println();
System.out.println("One box of Oaties weighs " + (int)(weight/x) +1
+ " metric tons.");

--snip

HTH
 
--snip (int) casts onto an integer, but rounds down. +1 makes sure it rounds up.

Will lead to unwanted behaviour if the user enters an integer (eg, double 4.0 will be cast to integer 4 and raised to 5).
 
True dat...

But another if statement will take care of that.

if (((int)(x/weight))==(x/weight))
{
System.out.println("There are " + (x/weight)
+ " boxes of Oaties in one metric ton.");
}
else
{
System.out.println("There are " + (int)(x/weight) +1
+ " boxes of Oaties in one metric ton.");
}

 
Back
Top