0

I am working on my first project for K-State and its a fundamentals class but I'm a little rusty on my programming and haven't worked with Java much. In the second part we calculate how many pizzas to order for a lan party and assume there a couple constants like how many slices per pizza(20) and how many slices each person will eat(2) and right now I am trying to have it round up a double to calculate the number of pizzas needed. I just can't get it to work properly and I know I'm doing something wrong I just don't know what. So if there were 18 people attending, 18 * 2 = 36. We would need 2 pizzas because each one has 20 slices. So the math that I am trying to do is (18 * 2) / 20 so you would get 1.8, I would like this number to be rounded up to 2 so that I can store it and use it to display and calculate how many slices are left over.

/*
    Proj1.java
    Tristan Stevens / Thursday 6:00pm / Khan

    Calculates user's overall grade percentage and how many pizzas to order/slices leftover
*/

import java.util.*;
import java.lang.*;

public class Proj1
{  
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);

        String input = "";
        final double Total = 290;
        double Grade = 0.0;
        double Points = 0.0;


        System.out.print("Enter in Project score #1 (0-30): ");
        input = s.nextLine();
        Points = Points + Double.parseDouble(input);

        System.out.print("Enter in Project score #2 (0-30): ");
        input = s.nextLine();
        Points = Points + Double.parseDouble(input);

        System.out.print("Enter in Project score #3 (0-30): ");
        input = s.nextLine();
        Points = Points + Double.parseDouble(input);

        System.out.print("\n");

        System.out.print("Enter the midterm exam score (0-100): ");
        input = s.nextLine();
        Points = Points + Double.parseDouble(input);

        System.out.print("Enter the final exam score: (0-100): ");
        input = s.nextLine();
        Points = Points + Double.parseDouble(input);

        Grade = (Points / Total) * 100;

        String output = String.format("Overall grade percentage: %.2f", Grade);
        System.out.println(output + "%");

        System.out.print("\n");

        int People = 0;
        final int slicesPeople = 2;
        final int slicesPizza = 20;

        System.out.print("What is the number of people expected at the pizza party? ");
        input = s.nextLine();
        People = Integer.parseInt(input);

        System.out.print("\n");

        double Pizzas = 0.0;
        Pizzas = Math.ceil((slicesPeople * slicesPizza) / slicesPizza);

        int slicesLeft = 0;

        System.out.format("For %d people, that would be %d pizza(s) with each having %d slices each." , People, Pizzas, slicesPeople);
        System.out.print("\n");
        System.out.format("There would be %d slice(s) leftover.", ((slicesPizza * Pizzas) - (People * slicesPeople)));

        System.out.print("\n");
        System.out.print("\n");
        System.out.print("\n");

    }
}
  • Instead of using `Math.ceil()` use `Math.floor()` – Coder Jan 27 '17 at 19:40
  • @Tristan, the way your code is set up right now, you should be getting a **IllegalFormatConversionException** when it is run and the reason for this is that, because you have declared the **Pizzas** variable (should be pizzas to follow naming conventions) as a double data type and then once the calculation is carried out this variable will ultimately end up holding `2.0`. When you display this result with **System.out.format()** you need to use the **%f** tag to represent this variable instead of **%d**..... – DevilsHnd - 退職した Jan 27 '17 at 20:28
  • Since you want to round **up** to integer anyways, just declare **Pizzas** (pizzas) as **int** then initialize it this way as an example: `int pizzas = (int) Math.ceil((slicesPeople * slicesPizza) / slicesPizza);`. Notice the cast to **int**. Now you can just leave your printout **format** the way you initially have it. – DevilsHnd - 退職した Jan 27 '17 at 20:28

0 Answers0