Recommended Posts

Well i was given this assignment and am without my book right now (should have it with me tomorrow). I don't remember the teacher going over this (this is one of few classes am actually awake all the way through). I hope somebody here can help me out:

Write a program that will prompt for the user to input an integer. Continue to take input until a sentinal

value is given. (I recommend a negative number) Once input is complete, print out the mean, min, and the max

of the inputted numbers.

You'll need to use an infinite loop and break it when you get the sentinal value. You will also need variables

for sum, mean, min, and max. Stick with the integer type throughout the program.

If any one can steer me in the right direction, I'd greatly appreciate it. Honestly I just don't remember ever remember going over anything dealing with user input at the command prompt.

* Ahhh, I do have something in my notes (my last class was 2 weeks ago, gimme a break) but it still doesn't make sense to me

Input java.util.scanner;
Scanner input = new scanner (System.in);
Int x;
X = input.next Int();

while(true)
{
//take input
If(sentinel_value== -1)
{
Break
}
//test to see if it’s the min
//test to see if it’s the max
//add it to the sum
//increment my counter
}
Print
Determine mean?

Now I was talking to my friend that's in the class with me over the break and he said he couldn't get his to work but that was as far as I got talkin with him.

Edited by Honda_Boy
Link to post
Share on other sites

here's what I've written so far. Whether I'm even close to being on the right track or not, I don't know. I just know I'm getting 5 errors when trying to compile.

public class threems{
public static void main(String Args[]){
Java.util.scanner;
int x;
x = input.next;
Int();
while (true)
{
if(sentinel_value==-1)
}
}
}

it's getting three errors.

also, why it sucks to work with only one screen on this crap:

toomanywindows.jpg

The only other system set up to program is my old Desktop that's in pieces right now cause of the new one. I got lazy and decided to work on the laptop. Big mistake.

Link to post
Share on other sites

Your approach is correct but your syntax is a bit off. If that's what's giving you trouble... I'll have to think for a minute. Syntax is a pain until you internalize it.

You have my sympathy screen-wise.

Incidentally, bookmark or download the JDK documentation if you haven't done so yet. The API docs are worth their weight in gold. Take a look at the entry for java.util.Scanner, for example.

Edited by jcl
Link to post
Share on other sites

OK I know this much is right (or at least I think it is)

import java.util.Scanner;
public class threems{
public static void main(String Args[]){
Scanner input = new Scanner(System.in);

Now I need to make it ask for input then give me a Min, Max, and Mean from it. I'm just not sure how to do that.

Link to post
Share on other sites

After finally locating a practice program similar to what I am trying to create, I came up with this code so far.

import java.util.Scanner;
public class threems{
public static void main(String Args[]){
Scanner input = new Scanner(System.in);
int x;
int sum;
int xCounter;
int mean;

sum = 0;
mean = 0;
{
System.out.print("Enter values to determine Max, Min, and Mean: ");}
x = input.nextInt();
while (x != -1);
{
sum = sum + x;
xCounter = xCounter + 1;
System.out.print("Enter next Value or -1 to quit: ");
x = input.nextInt();
}
if (xCounter != 0)
{
mean = (int) sum / xCounter;
System.out.printf("Mean = ", mean);
}
}
}

The problem is my xCounter right now. For some reason the xCounter = xCounter + 1 line won't work. It keeps saying xCounter may have not been initialized.

Edited by Honda_Boy
Link to post
Share on other sites
The problem is my xCounter right now. For some reason the xCounter = xCounter + 1 line won't work. It keeps saying xCounter may have not been initialized.

You need to provide an initial value to xCounter a la sum and mean. (Local variables must be explicitly given a value before they can be used.)

Edited by jcl
Link to post
Share on other sites

import java.util.Scanner;
public class threems{
public static void main(String Args[]){
Scanner input = new Scanner(System.in);
int x;
int sum;
int mean;
int xCounter;
sum = 0;
xCounter = 0;

System.out.print("Enter values to determine Max, Min, and Mean: ");
x = input.nextInt();
while (x != -1)
{
sum = sum + x;
xCounter = xCounter + 1;
System.out.print("Enter next Value or -1 to quit: ");
x = input.nextInt();
}
if (xCounter != 0)
{
mean = sum / xCounter;
System.out.print("Mean: ");
System.out.print(mean);
}
else
System.out.println("No Values were entered");
}
}

Alright I got it to at least do the Mean but I don't have a clue how to do Max and min.

and yeah i figured out the xCounter on my own. I just forgot to change something from earlier. That was just stupidity on my part.

Edited by Honda_Boy
Link to post
Share on other sites
Alright I got it to at least do the Mean but I don't have a clue how to do Max and min.

The outline in your first post has the answer: test each input against the current min and max and update them if needed. The only tricky bit is picking the initial values for min and max.

Edited by jcl
Link to post
Share on other sites
The outline in your first post has the answer: test each input against the current min and max and update them if needed. The only tricky bit is picking the initial values for min and max.

OK, something from my last class popped up in my head. I remember him talking about that, I just can't remember how to test them. I think I forgot to write it down (damn ADD.....and 3 hour long class).

Link to post
Share on other sites

Finally got it working:

import java.util.Scanner;
public class threems{
public static void main(String Args[]){
Scanner input = new Scanner(System.in);
int x;
int sum = 0;
int mean;
int xCounter = 0;
int max = 0;
int min = 10000;

System.out.print("Enter values to determine Max, Min, and Mean: ");
x = input.nextInt();
while (x != -1)
{
sum = sum + x;
xCounter = xCounter + 1;
while (x > max)
{
max = x;
}
while (x < min)
{
min = x;
}
System.out.print("Enter next Value or -1 to quit: ");
x = input.nextInt();
}
if (xCounter != 0)
{
mean = sum / xCounter;
System.out.println("Mean: " + mean + " Max: " + max + " Min: " + min);
}
else
{
System.out.println("No Values were entered");
}
}
}

Link to post
Share on other sites

some notes that don't fix your problem but may help in the future.

first I never use magic numbers (that is I never test values against a number explisitly, this way you can change the test condition latter with a configuration file when the code is more complex.

example

int sentinel = -1;

while (x != sentinel)

this way in larger code projects the test case also means more to you, after a while you may not remember what -1 is.

also you could change this to

int sentinel = 0;

while (x > sentinel)

this way any negative number would end the loop

another thing

you can use += instead of item = item + x;

so it would be item += x;

and to increment a value you can use ++;

so item = item + 1 can be written

item++;

the same as item = item -1 could be

item--;

and why do you have while statements to test the value of min or max? should those not be if statements?

if(x > max)

{

max = x;

}

else if (x < min)

{

min = x;

}

and should the input be inside your while statement?

you have this

System.out.print("Enter values to determine Max, Min, and Mean: ");

x = input.nextInt();

twice when it could be once if it came after your while statement

while (x != -1)

I would also change that to a boolean value and test x after the input and make the value true

example

sentinel = 0;

boolean test = true;

while(test)

{

System.out.print("Enter values to determine Max, Min, and Mean: ");

x = input.nextInt();

if (x < sentinel)

{

test = false;

}

//do rest of work

}

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...