Multiplayer Coding
/*
Create a program that calculates the average for an unknown number of marks to determine if the student gets the credit. The student gets the credit if the mark is between 50-100%. The program will also keep track of the number of marks below 50%, marks between 50% and 69%, and marks 70% or above.
Output:
Average
Credit or no credit
Number of marks below 50%
Number of marks between 50% and 69%
Number of marks 70% or above
*/
import java.util.Scanner; //import Scanner
class Main {
public static void main(String[] args) {
//create Scanner
Scanner input = new Scanner(System.in);
//declare variables
double average;
boolean credit = true;
int belowFifty = 0;
int betweenFiftySixtyNine = 0;
int aboveSeventy = 0;
double mark;
int numberOfMarks = 0;
int repeat = 1;
double sum = 0;
//while loop as long as repeat is equal to 1
while (repeat == 1)
{
//user input
System.out.println("Enter Mark (rounded to nearest 1):");
mark = input.nextDouble();
//if mark between 0 and less than 50
if (mark < 50 && mark >= 0)
{
credit = false;
//add to belowFifty total
++belowFifty;
//add to mark total
numberOfMarks++;
//calculate sum
sum = mark + sum;
//calculate average
average = sum/numberOfMarks;
//print average and credit
System.out.println("Average = " + average);
System.out.println("Credit? " + credit);
}
//if mark between 50 and 69
else if (mark >= 50 && mark <= 69)
{
credit = true;
//add to betweenFiftySixtyNine total
++betweenFiftySixtyNine;
//add to mark total
numberOfMarks++;
//calculate sum
sum = mark + sum;
//calculate average
average = sum/numberOfMarks;
//print average and credit
System.out.println("Average = " + average);
System.out.println("Credit? " + credit);
}
//if mark between 70 and 100
else if (mark >= 70 && mark <= 100)
{
credit = true;
////add to aboveSeventy total
++aboveSeventy;
//add to mark total
numberOfMarks++;
//calculate sum
sum = mark + sum;
//calculate average
average = sum/numberOfMarks;
//print average and credit
System.out.println("Average = " + average);
System.out.println("Credit? " + credit);
}
//otherwise invalid input
else
{
System.out.println("Invalid input");
}
//output of results
System.out.println("Marks below 50: " + belowFifty);
System.out.println("Marks between 50 and 69: " + betweenFiftySixtyNine);
System.out.println("Marks above 70: " + aboveSeventy);
//asks user if want to repeat
System.out.println("Repeat? 1 = yes, 0 = no");
repeat = input.nextInt();
}
}
}
Post on your Learning Blog with a DETAILED EXPLANATION of your code, try to use all the programming TERMINOLOGY from this topic
First, I pasted in the instructions at the top of the code as a comment so I could reference back to it. Since I wanted to get inputs from the user, I needed to import Scanner. I then declared the variables I was planning on using in my code. I set some values so that the loops would function. I used a while loop to repeat code as long as the repeat variable was equal to 1. I could’ve use a do-while loop to run the code at least once, but I was more comfortable with using while loops. In the beginning of the loop, the user is asked for their mark rounded to the nearest 1 and the mark is set as that value. The code then goes through else and else if statements to check which category it falls in. It will when determine whether a credit is achieved, an average, and add it to the range that the mark falls in. It then prints the results that it calculated. There is an else statement in case there is an input that doesn’t fall into any of the if or else if statements. After going through the if and else if statements, the program prints out the total number of marks that fell within a certain range. It then asks the user if they’d like to repeat. Since the while loop will continue as long as the repeat variable is equal to 1, the user can decide whether they want to continue inputting numbers by entering 1 or end by entering any other int value. This way, the program can continue for an unknown number of marks.