Can i know this java code got a problem or not? if has something wrong , what is the wrong? how to solve it? If no wrong, may be | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can i know this java code got a problem or not? if has something wrong , what is the wrong? how to solve it? If no wrong, may be

import java.util.Random; // A class to find the sum and count of the even and odd numbers generated randomly. public class RandomTest { public static void main (String[] args){ private final Random rand; private int evenSum, evenCnt; private int oddSum, oddCnt; // Constructor for objects of class RandomTest public RandomTest() { rand = new Random(); evenSum = 0; evenCnt = 0; oddSum = 0; oddCnt = 0; } //A method to find the sum and the count of even and odd numbers separately. public void summing() { int x; for (int i = 1; i<6; i++) { x = (int) rand.nextInt(50); if ( x % 2 == 0 ) { evenSum += x ; evenCnt++ ; } else { oddSum += x; oddCnt++ ; } System.out.print (x + " "); } System.out.println ( ); System.out.println ("Even Sum = "+evenSum + " Odd Sum = "+oddSum); System.out.println ("Even Count = "+ evenCnt + " Odd Count = "+oddCnt); } } }

22nd Mar 2022, 4:56 AM
lr oi
1 Answer
0
// move main() line at the 3rd line from end // Run summing() there public static void main(String[] args) { new RandomTest().summing(); } } // end of code
22nd Mar 2022, 7:09 AM
zemiak