简体   繁体   中英

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

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");}

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++
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM