Java Programming Help

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.

I'm kinda stumped here.

This is what I have so far.

import java.util.*;
import java.text.DecimalFormat;

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;

System.out.println("Exam score: ");
input = sc.nextLine();
examScores = Double.parseDouble(input);


}
}

>java
best advice is just install gentoo

what

You have no loops, come back when you actually try.

import java.util.*;
import java.text.DecimalFormat;

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




}
}

Alright OP, I can tell you're a complete newfag but everyone needs to start somewhere so here's what you do.

>kys

Now that we got that out of the way, here:

int count=0;
double highest = 0.0;
double lowest = 101.0;
double avg = 0;
double input=0;
while(true){
input = Double.valueOf(sc.NextLine());
if (inputhighest)
highest=input;
if(input

Motherfucker this fucks up on negatives. On a negative this loop will iterate at least once.

This shit doesn't even handle the average faggot, jesus.

Where is it fucking up on negs?

I was getting there.

#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

You don't check for negatives in the loop body, the loop will always run at least once, even if the input is negative.

Java.

Jaaaaaaaaavaaaaaaa.

also he said no arrays.

kek, yeah I was just fucking around and I don't feel like opening netbeans

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.

Sounds like you didn't actually copy the body of the loop then

>input = Double.valueOf(sc.NextLine());
>if (inputbreak;

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.

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.

So is that the same as

if (input < 0)
{

System.exit(0);
}

??

why is your average function so strange?

you could just say

if (count < 1)
{
avg = input;
}
else
{
avg = (avg + input) / 2;
}

>avg = (avg + input) / 2;
Should be:
avg =(avg + input)/count + 1;

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.

no m8, it's recursive

look at my attached pic

this is essentially my first real programming class ever

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

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.

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.

Do you guys not understand how programming and averages work?

This is literally the easiest formula there is.

I hope nobody thinks this shit is right. You're just proving the meme that CompScis can't do math.

>tfw tested my own formula and it was wrong

fucking math

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

if (count < 1)
{
avg = input;
}
else if (input > 0)
{
avg = (avg + input) / 2;
}

Well let's fucking test mine then.

We'll go ahead and do numbers 1-10.

start with 1.

2:
((1*1) + 2)/2 = 1.5

3:
((2*1.5)+3)/3 = 2

4:
((3*2)+4)/4 = 2.5

5:
((4*2.5)+5)/5 = 3

etc.

We end up with 5, which is correct.

Maybe next time you should fucking test it before calling it wrong.

Your function fails for the following:

10,10,10,10,15

Correct average: 11

Your function: 12.5

Is this a troll?
[math]\frac{6+7+9}{3} \neq \frac{ \frac{6+7}{2} +9}{2}[/math]

err, 5.5

Still correct. (I forgot I wasn't including 0).

that's because you're using integers, try using float or double

why would you do unnecessary calculations?

Oh wait you're right

Why would using integers even change anything?

with integer division you lose accuracy because of the rounding down that happens in programming ( 9/2 = 4)

if you divide enough integers by non-products you can lose a lot of information on the way to your answer

that's why when you want precision you use float or double

that's not equal to the function you wrote if this is you that's another function

appears to work though

avg =(avg + input)/count + 1;

use code tags stupid niger

Jesus fucking christ you don't even need arrays

Java should be compatible with C.
#define MAX 100
#define MIN 1
#define UNUSED 0
#define EXIT_SUCCESS 45
int main (void){

float avg=UNUSED;
int counts=UNUSED;
int highest=MIN;
int lowest=MAX;
int input=UNUSED;

while(input>=UNUSED){

//get input
puts("Enter your score:");
scanf("%d", &input);

//if the input is between the limits
if (input= MIN){

//determine if it's the max or min
if (input>=highest){
highest=input;
}
if (input

Oh and it works too

#include

int main()
{
int c, state;
state = 0;
while((c = getchar()) != EOF){
if(c == ' '){
if(state == 0){
state = 1;
putchar(c);
}
} else if (c != ' ') {
state = 0;
putchar(c);
}
}

return 0;
}


[16:28 1008]$ echo "this is a test" | 1-9
this is a test
[16:28 1009]$

Thanks

what the fuck

Not him but he is right. This is a simple way to find average and update the avg variable.

See what did.

How is he right? ((1 + 2)/2 + 3)/2 does not give you the mean of 1, 2, and 3.

avg = 0
input = 2
new avg =(0+2)/2 //=1

new avg = 1
new input = 3
new avg = (1+3)/2 //=2

avg keeps updating
so does the input

keyword: (((update)))

>((1 + 2)/2 + 3)/2
That's not what he said

>avg=(avg+input)/2;
For this to work you have to assigns avg to inout for the first time

avg of 15 is 15, not 7.5

>((1 + 2)/2 + 3)/2

his image says (1 + 2 + 3)/2 = ((1 + 2)/2 + 3)/2

that does not give you the average of all the inputs

No his image says (((1+2)/2)+3)/2

Ops such a silly mistake.
Here.
#include

#define MAX 100
#define MIN 1
#define UNUSED 0
#define EXIT_SUCCESS 45
int main (void){

float avg=UNUSED;
int counts=UNUSED;
int highest=MIN;
int lowest=MAX;
int input=UNUSED;

while(input>=UNUSED){

//get input
puts("Enter your score:");
scanf("%d", &input);

//if the input is between the limits
if (input= MIN){

//determine if it's the max or min
if (input>=highest){
highest=input;
}
if (input

Ignore see

>

>(1 + 2 + 3)/2 = ((1 + 2)/2 + 3)/2

It says (a + b + c)/3 = ((a + b)/2 + c)/2, which is still fucking wrong.

I didn't re-compile then

Do those answers make sense to you? Confirmed troll thread. I'm out.

> ((a + b)/2 + c)/2
No it doesn't

it says (a+b)/2 + ((a+b)/2+c)/2 + ...

Which is objectively correct

Fucking retards

(a+b+c+d)/2 = (a+b)/2 + (c+d)/2

Absolutely kill yourselves

wow how did this turn into a debate over mathematics

>tfw this thread could've been resolved easily if not for the fucking average function

my sides

Hello, it is the guy who made the shitty paint equations. I've come to redeem myself.

You are indeed correct, but that doesn't give you the full equation

Let (a + b) / 2 = g (g is the average of a and b)

Now let us imagine an additional quantity c

Our sum now follows (a + b + c) / 3, but we have lost a and b and we are only left with c, the number of quantities we have, and g

However, we know that g = (a + b)/2 therefore 2g = (a + b)

And since all we need to do is ((2g + c) / 3) or (((a +b) + c) / 3)

A way to write this in pseudocode would be:

(((numberofquantities - 1) * oldaverage) + newquantity) / numberofquantities

I have become mentally stronger today. Thanks Sup Forums.

...

just kill yourself you fucking moron

Why don't you fucks just keep a running total of all the scores combined and increment amount of scores entered then divide at the end?

Wastes less of muh pentium 2 cpu time as well.

download moar ram