Complete beginner (college student) - code not working!!
:-?
Hi, I am new to Java and part of my college assignment is to complete a series of exercises creating java programs and test them in the command window.
I have missed some lessons due to illness/being away and now i'm struggling with a few and I can't figure out what i'm doing wrong although I know it must be something relatively simple if I knew how...
My code is supposed to ask for an input 10 times, then calculate and output the total of all commission then output the average. At first I had commission++ below the input which was adding 1 plus the input each time which was wrong and I can't figure out whether i am missing a variable or if my maths is just totally wrong (not skilled in either area).
Please help....
Code java:
import java.util.Scanner;
class commission
{
public static void main (String [] args)
{
Scanner input = new Scanner(System.in);
int commission, totcom, staff, comav;
staff = totcom = comav = 0; // set the number to 0
for (staff = 1; staff <=10; staff++)
{
System.out.println("Please input the amount of commission");
commission = input.nextInt();
//commission+;
totcom = (commission * staff );
comav = (commission / staff);
}
System.out.println("================================");
System.out.println("The amount of commission overall is " + totcom);
System.out.println("================================");
System.out.println("The average amount of commission is " + comav);
}
}
Re: Complete beginner (college student) - code not working!!
Please copy and paste here the contents of the command prompt window where you execute the code. It should show the input to the program and what the program prints out.
On windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Also Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Complete beginner (college student) - code not working!!
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\latham Family>e:
E:\>cd java/5.4 commission
E:\Java\5.4 Commission>javac commission.java
E:\Java\5.4 Commission>java commission
Please input the amount of commission
5
Please input the amount of commission
6
Please input the amount of commission
4
Please input the amount of commission
3
Please input the amount of commission
4
Please input the amount of commission
5
Please input the amount of commission
6
Please input the amount of commission
7
Please input the amount of commission
8
Please input the amount of commission
8
============================== ==
The amount of commission overall is 80
============================== ==
The average amount of commission is 0
E:\Java\5.4 Commission>[COLOR="Silver"]
--- Update ---
Taken out duplicate copy
Re: Complete beginner (college student) - code not working!!
You haven't explained what the problem with the output is.
If the problem is the 0, its due to integer arithmetic. for example: 6/8= 0
To get decimal places make one of the operands a double: 6.0/8 = 0.75
Otherwise please explain what the problem is. Show what you expect the output to be.
Re: Complete beginner (college student) - code not working!!
The total amount of numbers inputted there should come to 56, however it's displaying 80 at the output as I am assuming it's multiplying 10 (the times the loops goes round) by the last value inputted (8) when I want it to calculate all of the inputted numbers as a total.
I'm not sure if i've missed the fact I need to 'store' each of the numbers somewhere to enable me to calculate it or if my maths is totally wrong... I think maybe both.
--- Update ---
I haven't dealt with the average output yet, I figure if I can resolve the calculation problem then I will be able to average the number
Re: Complete beginner (college student) - code not working!!
When you use a calculator to add up a group of numbers, you keep a running total and don't clear the total after each number is added. The same with a program that is to total some values. You add each new number to the total instead of assigning the new number to the total. Assigning clears out the previous value.
Re: Complete beginner (college student) - code not working!!
What is happening is that the commission variable is being replaced by each new user entry.
When it comes to totalling the average commission, it uses the most recent entry (commission) instead of the sum of entries (totcom).
--
Try also to use Java conventions when it comes to naming classes. It should always have a capital letter.
When using a scanner, it's also advisable to close it after use.