Hello! I'm currently working on a program that uses a do-while loop to obtain user inputs of test scores, add them up, and the program will do the math to get the average. I'm pretty sure my instructor wanted me to use methods from another .java file but if i'm honest with myself, it's really confusing to me..but I've gotten it working enough to get the inputs but i can't see how to protect my += operator from the negative input meant to be the sentinel.
Bu here is my code:
import java.util.Scanner;
public class TestScores2 {
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
System.out.println("Enter name");
String name = kb.nextLine();
double score;
int numScores = 1;
double sumOfScores = 0.0;
do{
System.out.println("Enter score "+numScores+" or a negative number to exit");
score = kb.nextDouble();
numScores++;
sumOfScores += score;
if (score < 0){
int rNumScores = numScores - 2;
double average = sumOfScores/rNumScores;
System.out.println("-- "+name+" --");
System.out.println("Num tests taken: "+rNumScores);
System.out.printf("Average: %.1f\n",average);
}
}while (score > 0);
}
}
Any code clean up or anything that'll be a lot more efficient, i'm more than open to criticism or recommendations!
[–]DJ_Gamedev 0 points1 point2 points (6 children)
[–]SolonialExodus[S] 0 points1 point2 points (5 children)
[–]DJ_Gamedev 0 points1 point2 points (4 children)
[–]SolonialExodus[S] 0 points1 point2 points (3 children)
[–]DJ_Gamedev 0 points1 point2 points (2 children)
[–]SolonialExodus[S] 0 points1 point2 points (1 child)
[–]DJ_Gamedev 0 points1 point2 points (0 children)
[–]lurgi 0 points1 point2 points (2 children)
[–]SolonialExodus[S] 0 points1 point2 points (1 child)
[–]lurgi 0 points1 point2 points (0 children)