-5

Hey people of Stackoverflow! I just have a question about an error that I came across while doing this lesson on Java online. So this is the code:

import java.util.Scanner;
public class GradesAndPoints {
    public static void main(String[] args) {

        System.out.print("Type in your score between (0-27): ");
        Scanner ask = new Scanner(System.in);
        int num = ask.nextInt();

        int result = (num/27);

        System.out.println(result);

The error is: whenever I run the code with the variable "num" being any int value, it prints out to be 0. Can someone explain to me why this error occurs and a solution I can implement to solve this?

  • 1
    *whenever I run the code with the variable "num" being any int value, it prints out to be 0.* Really? Have you tried 27? – shmosel Jun 30 '17 at 02:04

1 Answers1

0

The way you're doing this, you're diving integers. This, by definition, will get you an integer that is truncated.

5 / 10 = 0

If you turn one of them into a float (by adding a . at the end), you will get floating point division, which is what you're looking for.

5. / 10 = 0.5
5 / 10. = 0.5
5.0 / 10.0 = 0.5
ZeldaZach
  • 478
  • 1
  • 9
  • 18