Question

A group of 3 gamers agreed to compete while playing their favorite game on 3 rounds. Every gamer has 3 scores collected over

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello , here are the code and sample outputs of the problem using for loop , if - else conditions.

Code:-

class game
{
    public static void main(String[] args)
    {
        // integer arrays are used to store the scores
        int []ahmed ={883,132,95};// array of size 3 to store ahmed scores.
        int []ali={95,116,110};//array of size 3 to store ali scores.
        int [] sarah={200,400,300};//array of size 3 to store sarah scores.
        float ahmed_avg=0,ali_avg=0,sarah_avg; // these are the variables to store the averages.
        int i,ahmed_sum=0,ali_sum=0,sarah_sum=0;// variables to store their sums.

        for (i=0;i<3 ;i++)// for loop to calculate the sum of their individual scores
        {
            ahmed_sum=ahmed_sum+ahmed[i];
            ali_sum+=ali[i];
            sarah_sum+=sarah[i];           
        }

        ahmed_avg=ahmed_sum/3; // calculate all the averages of their scores
        ali_avg=ali_sum/3;
        sarah_avg=sarah_sum/3;

        System.out.println("The average score for Ahmed is "+ahmed_avg);
        System.out.println("The average score for Ali is "+ali_avg);
        System.out.println("The average score for sarah is "+sarah_avg);

        // if else conditions to check the highest average and prints them.
        if(ahmed_avg>ali_avg && ahmed_avg >sarah_avg){
            System.out.println("The winner is Ahmed with average high score on 3 rounds as: "+ahmed_avg);

        }
        else if(ali_avg>ahmed_avg && ali_avg>sarah_avg){
            System.out.println("The winner is Ali with average high score on 3 rounds as: "+ali_avg);
        }
        else{
            System.out.println("The winner is Sarah with average high score on 3 rounds as: "+sarah_avg);
        }

    }
}

Sample Output:-

rakesh@rakesh:~/Desktop$ javac Chegg.java
rakesh@rakesh:~/Desktop$ java game
The average score for Ahmed is 370.0
The average score for Ali is 107.0
The average score for sarah is 300.0
The winner is Ahmed with average high score on 3 rounds as: 370.0

class game public static void main(String[] args) // integer arrays are used to store the scores int [lahmed ={883, 132,95};/

rakesh@rakesh:~/Desktops javac Chegg.java rakesh@rakesh:-/Desktop$ java game The average score for Ahmed is 370.0 The average

Add a comment
Know the answer?
Add Answer to:
A group of 3 gamers agreed to compete while playing their favorite game on 3 rounds....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT