so I'm learning java and I'm trying to write a program that prompts the user for exam scores (values 1-100), and when the user enter some negative number to end the entering sequence, it will then print out the number of scores entered, the highest score, the lowest score, and the average of all the scores.
I'm being challenged to do this with simple comparison statements and loops, and WITHOUT using arrays.
public class Lab3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); DecimalFormat df = new DecimalFormat("0.00"); String input; double examScores;
public class Lab3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); DecimalFormat df = new DecimalFormat("0.00"); String input; double examScores; double highScore = 0.00; double lowScore = 101.00; double averageScore; int numInput = 0;
do { System.out.println("Enter exam score (1-100), or enter a negative number to end: "); input = sc.nextLine(); examScores = Double.parseDouble(input);
if (examScores < lowScore) { examScores = lowScore; } else if (examScores > highScore) { examScores = highScore; } numInput++;
} while (examScores >= 0);
} }
Oliver Gonzalez
Alright OP, I can tell you're a complete newfag but everyone needs to start somewhere so here's what you do.
Motherfucker this fucks up on negatives. On a negative this loop will iterate at least once.
Luke Price
This shit doesn't even handle the average faggot, jesus.
David Gutierrez
Where is it fucking up on negs?
I was getting there.
Julian Hill
#include #include #include
Here you go OP! All you had to do was use the vector class :)
int main() { int input; int highest_score = 1; int lowest_score = 100; int average = 0; std::vector scores;
while (std::cin >> input && input > 0) { scores.push_back(input); if (input > highest_score) { highest_score = input; } if (input < lowest_score) { lowest_score = input; }
} std::cout
Daniel Hughes
You don't check for negatives in the loop body, the loop will always run at least once, even if the input is negative.
Michael Gutierrez
Java.
Jaaaaaaaaavaaaaaaa.
also he said no arrays.
Jeremiah Foster
kek, yeah I was just fucking around and I don't feel like opening netbeans
Ryder Lewis
So I'm using your method modified a little bit but not in a way to ruin it, but at the end when I print out the values I want, it prints the negative number entered to end the program as the Low Score.
Robert Kelly
Sounds like you didn't actually copy the body of the loop then
This being at the top of the while loop stops the loop from executing when the input is negative. If this is there then it is actually impossible for the lowest to get set to negative.
Lincoln Perez
To add onto this.
The average would be off too, it just isn't as apparent because the method I used only partially adjusts the average each time.
No, but if you're really new I can see why you might think that
break; just exits the loop, not the program.
System.exit(0) will exit the program itself, you'll normally see that in the catch block of a try{}catch(){} which is something you probably haven't learned yet, so just think of System.exit() only applying to fatal errors.
break; is a reserved word and just exits loops, (for, while and do).
It's also used in switch statements but that's its own case.
Aaron Ortiz
no m8, it's recursive
look at my attached pic
Michael Morgan
this is essentially my first real programming class ever
Caleb Jones
to explain further what you're essentially doing is getting the average of the first two values, then using your that average and the next input you get a new average and you continue this process until the program ends
Sebastian Hughes
yeah this would work too.
My average function basically made it a sum of fractions, your's reads better but they effectively do the same thing.
My way of thinking of a running average is that the average after multiple inputs is the equivalent of having every single entry in the set be that average, so when you add some new entry it would be the equivalent of:
((average * count) + new entry) / (count + 1)
so when I wrote it out on one line it looked like:
(average * count) / (count+1) + (new entry)/(count+1)
That was always easier for me to memorize.
Jackson Phillips
Yeah no worries dude. Java is a fine starting language and teaches fundamentals very well.
Sup Forums hates Java though so...
Some advice:
It's worth it to ask questions and read ahead. Stackoverflow is your friend.
Knowing programming humor will get you in any programmer's good graces quickly enough.
The user is a fucking retard, ALWAYS.
C is basically Latin.
A recurring problem among programmers is that we all push our favorite language too hard. Learn what you're taught for the first few years, use the first class that changes from Java to REALLY learn how to master a new language and quickly.]
You'll always know Java better than any other language because it was your first.
Logan Stewart
Do you guys not understand how programming and averages work?
This is literally the easiest formula there is.
Jacob Robinson
I hope nobody thinks this shit is right. You're just proving the meme that CompScis can't do math.
Brandon Johnson
>tfw tested my own formula and it was wrong
fucking math
Leo Baker
haha jk I was right
the negative was bringing my number down at the end
phew, thought I lost my common sense for a second there