Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Percentage Calculation

807598Nov 17 2005 — edited Nov 18 2005
I am having some trouble getting percent to calculate properly, for some reason it always calculates 100% no matter what i put in, if you could take a look and tell me whats wrong this one has me stumped

Heres the code:

public class ASMT12_LemireE
{

static Console c; // The output console

public static void main (String[] args)
{
c = new Console (); //'c' is the output console

// Place your program here:

double Rent;
double FoodCost;
double CarLease;
double CellPhoneCharge;
double Total;





//Title
c.setCursor (1, 30);
c.println ("MONTHLY BUDGET");

//Rent
do
{
c.setTextColor (Color.red);
c.print ("Please enter your Rent:"); //Question
c.setTextColor (Color.blue);
Rent = c.readDouble (); //response
if (Rent < 1 || Rent > 10000)
c.println ("please enter a number between 1-10000");
}
while (Rent < 1 || Rent > 10000);

//Food
do
{
c.setTextColor (Color.red);
c.print ("Please enter your food cost:"); //Question
c.setTextColor (Color.blue);
FoodCost = c.readDouble (); //response
if (FoodCost < 1 || FoodCost > 10000)
c.println ("please enter a number between 1-10000");
}
while (FoodCost < 1 || FoodCost > 10000);

//Car Lease
do
{
c.setTextColor (Color.red);
c.print ("Please enter your car lease amount:"); //Question
c.setTextColor (Color.blue);
CarLease = c.readDouble (); //response
if (CarLease < 1 || CarLease > 10000)
c.println ("please enter a number between 1-10000");
}
while (CarLease < 1 || CarLease > 10000);

//CellPhoneCharge
do
{
c.setTextColor (Color.red);
c.print ("Please enter your cell phone charge:"); //Question
c.setTextColor (Color.blue);
CellPhoneCharge = c.readDouble (); //response
if (CellPhoneCharge < 1 || CellPhoneCharge > 10000)
c.println ("please enter a number between 1-10000");
}
while (CellPhoneCharge < 1 || CellPhoneCharge > 10000);

//clear screen
c.setTextColor (Color.pink);
c.println ("Press any key to see a breakdown of your budget");
c.getChar ();
c.clear ();


//Title for second page
c.println ("MONTHLY BUDGET");

//Stuff in Chart
c.setCursor(3, 12);
c.println("ITEMS");
c.setCursor(5, 12);
c.println("RENT");
c.setCursor(6, 12);
c.println("FOOD");
c.setCursor(8, 10);
c.println("CAR LEASE");
c.setCursor(9, 10);
c.println("CELL PHONE");
c.setCursor(13, 12);
c.println("TOTAL");
c.setCursor(3, 23);
c.println("EXPENSE");
c.setCursor(3, 32);
c.println("AMOUNT");
c.setCursor(3, 39);
c.println("PERCENT OF TOTAL");

//Chart
c.drawLine (50, 30, 440, 30); //Top Line
c.drawLine (50, 260, 440, 260); //Bottom Line
c.drawLine (50, 30, 50, 260); //Left Line
c.drawLine (440, 30, 440, 260); //right line
c.drawLine (50, 70, 440, 70); //Second horizontal line
c.drawLine (50, 100, 440, 100); //third line
c.drawLine (50, 130, 440, 130); //fourth line
c.drawLine (50, 160, 440, 160); //fifth line
c.drawLine (50, 190, 440, 190); //sixth line
c.drawLine (50, 220, 440, 220); //7th line
c.drawLine (170, 30, 170, 260); //second vert. line
c.drawLine (300, 30, 300, 260); //Last vertical line

//Print Expense Amount
c.drawString ("" + Rent, 218, 98);
c.drawString ("" + FoodCost, 218, 125);
c.drawString ("" + CarLease, 218, 152);
c.drawString ("" + CellPhoneCharge, 218, 182);
c.setCursor(13,28);
c.setTextColor(Color.black);
c.print((Rent + FoodCost + CarLease + CellPhoneCharge));

//Percent for Rent
double score = Rent;
double total = FoodCost;
double percent = ((double)Rent/total) * 100;
java.text.DecimalFormat fmt = new java.text.DecimalFormat("#.#");
c.setCursor(20, 20);
c.println(""+fmt.format(percent)+"%");







}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 16 2005
Added on Nov 17 2005
6 comments
224 views