+ 5

Little correction please. I couldn't get test case 4

Average word problem Java You are given a statement and you are asked to find average letter per word import java.util.Scanner; public class Program { public static void main(String[] args) { int b=0; Scanner sc = new Scanner(System.in); String s = sc.nextLine(); s = s.replaceAll("\\p{Punct}",""); char[] arr = s.toCharArray(); for (int i=0; i<arr.length; i++) { if(arr[i]==' '){ b++;} } double totalword = arr.length-b; double numberofword = b+1; double average =totalword/numberofword; double o = Math.round(average); if(o<average){ int r = (int)o; System.out.println(r+1); } else { int t = (int)average; System.out.println(t); } } }

11th Dec 2020, 12:33 PM
Shamil Bedru
Shamil Bedru - avatar
1 Réponse
+ 2
Use this, int average = (int) Math.ceil( totalword/numberofword); System.out.println (average); Instead of /* double average =totalword/numberofword; double o = Math.round(average); if(o<average){ int r = (int)o; System.out.println(r+1); } else { int t = (int)average; System.out.println(t); } */ BTW your code works if you change, int t = (int)average; into int t = (int) Math.ceil (average); Remember, the task was to round UP to the nearest whole number.
13th Dec 2020, 5:27 AM
Minho
Minho - avatar