TransWikia.com

How to can I sum some numbers in a random series?

Stack Overflow Asked by Richard Caballero on December 20, 2021

I’m trying to make a program that generates random two-digit integers until I get a 10 or a 20. To then find the mount of numbers, the sum of the numbers less than 10, the sum of the numbers equal to 15, the sum of the numbers greater than 70. Can someone help me, please?
This is my code:

//Variables
    int numRandom=0, less10, equal15, more70;
    //Process
    txtS.setText("Random numbers: " + "n");
    for (int mountNum=0; numRandom==40 || numRandom==20; mountNum++) {
        numRandom = (int) (99 * Math.random());
        txtS.append(numRandom + "n");}

One Answer

You could just store the values directly and create variables for each case.

This is an example for you less than 10 case. (The 'if statement' would be contained inside your for loop).

int sumLessThanTen = 0;
int countLessThanTen = 0;
...
if(numRandom < 10){
    sumLessThanTen += numRandom;
    countLessThanTen++
}

Answered by Lucas Silva on December 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP