• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program wont print correct decimal value  RSS feed

 
Aaron Stevens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is this: The program does what it is supposed to until the final line. It is supposed to display an average, but the decimal value always displays ".0" instead of the correct value. I'm not getting any errors so I am at a loss. I read a little bit about locale issues, but I have another very simple program that displays a decimal value correctly.

Any help would be appreciated. I attached the code below.

Thanks

//CODE

import java.io.*;
import java.util.Scanner;


public class Lab1_5 {

public static void main(String[] args) throws IOException {

Scanner keyboard = new Scanner(System.in);

//Declare variables
int yearOneWin;
int yearTwoWin;
int yearThreeWin;
int yearFourWin;
int yearFiveWin;
int totalWins;
double averageWins;

//User input
System.out.print("Enter the number of wins for year 1: ");
yearOneWin = keyboard.nextInt();

System.out.print("Enter the number of wins for year 2: ");
yearTwoWin = keyboard.nextInt();

System.out.print("Enter the number of wins for year 3: ");
yearThreeWin = keyboard.nextInt();

System.out.print("Enter the number of wins for year 4: ");
yearFourWin = keyboard.nextInt();

System.out.print("Enter the number of wins for year 5: ");
yearFiveWin = keyboard.nextInt();

//Calculate total wins
totalWins = yearOneWin + yearTwoWin + yearThreeWin + yearFourWin + yearFiveWin;

//Calculate average wins
averageWins = totalWins / 5;

//Display the average wins from all five years
System.out.println("The average number of wins over the past five years is " + averageWins + ".");



}


}
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here you are performing an integer divide that will truncate any fractional part before assigning it to the averageWins.

Try
 
Aaron Stevens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh.. Thank you haha. I swore I already tried that, but clearly I had not. Worked as it should!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!