|
|
how to display the number entered in descending orderb
import java.util.Scanner;//Scanner class set up
I'm trying to print a program that will take the input and display the
number in descending order and gives out the average for it? It compiles but
it won't give me the output... Any suggestions?
public class Average
{
public static void main(String args[])// main method begins to program
{
Scanner input = new Scanner(System.in);
int counter=1 ; // set counter to 0
int numberOfValues;
float AverageNumber; // set AverageNumber to 0
int total=0;
System.out.printf("Indicate how many values you want to input:");//
display the value you want to input
numberOfValues = input.nextInt();// prompt for user to input
while (counter <= numberOfValues)// while loop to initialize the
loop for the value
{
System.out.printf("Indicate numbers you want to calculate for
average:");// display the number you want to get smallest
int inputNumber = input.nextInt();// prompts for the number
for(inputNumber=0;inputNumber>0;inputNumber--)
total = total + inputNumber;
counter= counter+1;
}
AverageNumber = total/ numberOfValues;
System.out.println(AverageNumber);// display the average for user
}
}
|
|
0
|
|
|
|
Reply
|
Jeremy
|
5/5/2007 2:28:26 AM |
|
Jeremy <jltuck81@comcast.net> wrote:
> import java.util.Scanner;//Scanner class set up
>
> I'm trying to print a program that will take the input and display the
> number in descending order and gives out the average for it? It compiles but
> it won't give me the output... Any suggestions?
My suggestion: simplify! You are trying to do a lot all at once. Get
some part of the problem working first, then go on to the next.
I'd start by getting the average to display properly. Later, when you
have to display the numbers in descending order, you'll have to do more
complex stuff: probably involving an array and the Arrays.sort standard
API call.
To understand what's going wrong with your average, consider the
following:
> int inputNumber = input.nextInt();// prompts for the number
>
This reads a number from the integer, and stores it into inputNumber.
> for(inputNumber=0;inputNumber>0;inputNumber--)
>
> total = total + inputNumber;
This first sets inputNumber to 0, which means you are completely
throwing away whatever the user just entered. Then it checks to see if
inputNumber is greater than zero; which of course it's not, since you
just set inputNumber to zero. So it skips the body of the for loop
(which is the total = ... statement), and...
> counter= counter+1;
Then you increment the counter.
Is that what you wanted? I imagine not. Fixing that would be a good
first step.
--
Chris Smith
|
|
0
|
|
|
|
Reply
|
Chris
|
5/5/2007 2:51:01 AM
|
|
I'm sorry, I"m lost for real, but it doesn't matter, because i got it to
work and i figured out how to deal wtih some stuff. I didn't see anything
wrong with average part, and this came from a book how to program in java
sixith edition... but really thanks for your comments... i learned something
from it.
"Chris Smith" <cdsmith@twu.net> wrote in message
news:MPG.20a59efe8569f242989883@news.altopia.net...
> Jeremy <jltuck81@comcast.net> wrote:
>> import java.util.Scanner;//Scanner class set up
>>
>> I'm trying to print a program that will take the input and display the
>> number in descending order and gives out the average for it? It compiles
>> but
>> it won't give me the output... Any suggestions?
>
> My suggestion: simplify! You are trying to do a lot all at once. Get
> some part of the problem working first, then go on to the next.
>
> I'd start by getting the average to display properly. Later, when you
> have to display the numbers in descending order, you'll have to do more
> complex stuff: probably involving an array and the Arrays.sort standard
> API call.
>
> To understand what's going wrong with your average, consider the
> following:
>
>> int inputNumber = input.nextInt();// prompts for the number
>>
>
> This reads a number from the integer, and stores it into inputNumber.
>
>> for(inputNumber=0;inputNumber>0;inputNumber--)
>>
>> total = total + inputNumber;
>
> This first sets inputNumber to 0, which means you are completely
> throwing away whatever the user just entered. Then it checks to see if
> inputNumber is greater than zero; which of course it's not, since you
> just set inputNumber to zero. So it skips the body of the for loop
> (which is the total = ... statement), and...
>
>> counter= counter+1;
>
> Then you increment the counter.
>
> Is that what you wanted? I imagine not. Fixing that would be a good
> first step.
>
> --
> Chris Smith
|
|
0
|
|
|
|
Reply
|
Jeremy
|
5/7/2007 2:33:46 AM
|
|
|
2 Replies
1264 Views
(page loaded in 0.04 seconds)
|
|
|
|
|
|
|
|
|