• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Program wont print correct decimal value

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 + ".");



}


}
 
Saloon Keeper
Posts: 11057
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh.. Thank you haha. I swore I already tried that, but clearly I had not. Worked as it should!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic