-3
 import java.io.*;
  import java.util.Scanner;

  public class Main {

      public static void main(String[] args) {
          Scanner s = new Scanner(System.in);
          System.out.print("Enter total number of overs: ");
          int a = s.nextInt();
          System.out.print("Enter target Runs: ");
          int b = s.nextInt();
          System.out.print("Enter overs bowled");
          int c = s.nextInt();
          System.out.println("Enter runs scored");
          int d = s.nextInt();
          double e = d / c;
          float f = (b - d) / (a - c);
          double g = 250 / 40;
          System.out.println("\nName: " + e);
          System.out.printf("%.2f", f);
          System.out.println("ans" + g);
      }
  }

This is progam to estimate the run rate and the runrate required in cricket. when i find the average the number after decimal is shown as .0 ie. if the answer is 6.25 it gets displayed as 6.00 can you please help me rectify this

thanks in advance

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
user-6589801
  • 129
  • 1
  • 15
  • 1
    I'm voting to close this question as off-topic because the poster doesn't understand integer division. – duffymo Aug 09 '16 at 11:50
  • Covert the int to float in the calculation or before. For example `double e = (double) d / (double) c;` – Paolo Forgia Aug 09 '16 at 11:50
  • It's because integer division is different from floating point. You need to know how. 6/4 = 1 in integer division; 6.0/4.0 = 1.5 in floating point division. – duffymo Aug 09 '16 at 11:51
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – Mickael Aug 09 '16 at 11:52

1 Answers1

0

Because a,b,c and d all are int and they will give you integer result only, do a type casting to get desired result.

 double e =  ((double)d)/c;

and same holds true for

 float f= (b-d)/(a-c);

This also needs a floating point type to get a floating point result.

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33