Why I can’t get correct result , 28.4? public class Program { public static void main(String[] args) { int grace = 10; int edward = 21; int rose = 15; int mom = 46; int dad = 50; float average = (grace+edward+rose+mom+dad)/5; System.out.println(average); } }
10/25/2019 12:11:35 AM
Mali3 Answers
New AnswerYou defined the numbers as integers, so when they are divided, they are divided as integers, rounding the decimal to whole number. If you want to keep the decimal, try: //my code is in C#, adapt it to Java using System; public class Program { public static void Main(String[] args) { float grace = 10; float edward = 21; float rose = 15; float mom = 46; float dad = 50; float average = (grace+edward+rose+mom+dad)/5; Console.WriteLine(average); } }
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message