Not Yet Answered # Sentinel control

server_crash 64 Discussion Starter cdt1983 3 jwenting 1,594 Discussion Starter cdt1983 3 server_crash 64 Discussion Starter cdt1983 3 ichihollow ichihollow cactuspancake Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

are you talking about something like this?

```
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x;
int count = 0;
int i;
do
{
System.out.println("enter a number");
i = Integer.parseInt(br.readLine());
x = x + i;
count ++;
}
while(i != -1);
int b = x / count;
System.out.println("Average is: " + b);
```

Don't exactly take my word for it, I'm doing this off the top of my head.

0

Sorry, I'm new to java. Let me make it a little more clear.

How can I have a user input 2 numbers. The first number entered will be divided by the first to find the average for those two numbers.

After the user enters a -1 it will end and display the Complete average for all the numbers. ie (average1 + average2 + average3 / 3) <--depending on how many numbers they enter.

Hope this is more clear.

0

So, you've laid out your entire algorithm.

Now all YOU have to do is translate that to Java. Noone here is going to do your homework for you, but if you get stuck after making a good start we'll be happy to give you a hand in completing it.

0

Well I spent a few hours on it and got it to compile and ask for the correct inputs. However, the program asks for miles driven and gallons used and it should display the miles per gallon. But it keeps displaying 0 as the result, unless the numbered entered is the same for both, ie 5miles, 5gallons, the result displayed is 1. Here is a part of the code where I think the problem is. If you could point me in the right direction I'd appreciate it. I don't expect anyone to do my homework for me.

```
//loop
while (tankCounter <= numOftanks)
{
// first user input
milesdrivenstring = JOptionPane.showInputDialog( "Enter the amount of miles driven" );
milesdriven = Integer.parseInt( milesdrivenstring );
// second user input
gallonsusedstring = JOptionPane.showInputDialog( "Enter the gallons of gas used" );
gallonsused = Integer.parseInt( gallonsusedstring );
individualmpg = gallonsused / milesdriven;
// display output
JOptionPane.showMessageDialog( null, "The miles per gallon for this tank is " + individualmpg,
"Results", JOptionPane.PLAIN_MESSAGE );
gallonstotal = gallonstotal + gallonsused;
milestotal = milestotal + milesdriven; // add milesto total
tankCounter = tankCounter + 1; //increment counter
}
```

So, you've laid out your entire algorithm.

Now all YOU have to do is translate that to Java. Noone here is going to do your homework for you, but if you get stuck after making a good start we'll be happy to give you a hand in completing it.

0

I haven't looked too much at the code, but I did notice that you used integers..This means there will be no decimal places, and it's possible that your results will turn out to be 0 because of that.

0

Thanks. I found out what the problem was. I had the division backwards. I corrected it by changing it to:

`individualmpg = milesdriven / gallonsused;`

Very simple mistake, that cause a big problem. I just figured out how to format the output to two decimal places so everything is working perfect!

Thanks for the help.

0

ok in good faith, i've done everything i can think of but can't figure this out. my objective is to use an infinite loop terminated by a sentinel value. then collect a minimum, maximum, and an average, of the values entered. my test batch of 12, 55, 62, 98 produces a min of 12, max of 98 and average of 65, when the calculated average is 56.75, i have to use int variables as part of the assignment, so collecting an answer of 56 OR 57 is fine. if my test batch is 12, 55, 62, 98, 56, my min is 12, max is 98 and average is 56!? i've repeated this with 12 and 24 in batches of 12. 24, result is 20. 12, 24, 18 gives me 18. as a last run, i ran 12, 55, 62, 98, 56, 57, gives me 56. these results are all over the board, any help?

code as follows

```
import java.util.Scanner;
public class ola2
{
public static void main(String args[])
{
Scanner input = new Scanner( System.in );
int x = 0;
int count = 0;
int i;
int min = 999999;
int max = 0;
int value = 0;
do
{
System.out.println( "Enter value" );
i = input.nextInt();
if ( i != -888 )
{
value = i;
}
if ( value < min )
{
min = value;
}
if ( value > max )
{
max = value;
}
x = x + value;
count ++;
}
while( i != -888 );
int b = x / count;
System.out.println( "The average input is: " + b );
System.out.println( "The max is: " + max );
System.out.println( "The min is: " + min );
}
}
```

*Edited 3 Years Ago by Dani*: Formatting fixed

0

its really great posting. its so useful for me. thanks for this.

umm i hope you noticed, it does not work, if you know how to fix it that would be really helpful. I know mr mugler would be happy if it worked right.

0

Sorry, I'm new to java. Let me make it a little more clear.

How can I have a user input 2 numbers. The first number entered will be divided by the first to find the average for those two numbers.

After the user enters a -1 it will end and display the Complete average for all the numbers. ie (average1 + average2 + average3 / 3) <--depending on how many numbers they enter.

Hope this is more clear.

Hey are you in my class? Haha every time I look something up on the internet I see people asking questions to the same homework problems I have :) I'm in libe50a with Henry Leitner... and I want to know how to use sentinels too! I know how to do the while loop and say while whatever != -1 but you and me know that that's not what we're looking for...

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...