3 Replies - 506 Views - Last Post: 05 February 2012 - 01:50 PM Rate Topic: -----

#1 ajordan24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-January 12

I can't figure out what's wrong with this program?

Posted 05 February 2012 - 01:41 PM

I'm making a program for class about finding the percentage of time it takes to make a java program. I made the code and the output is all wrong and I can't figure out what the problem is.

 

import java.util.*;
public class Project 
{

	public static void main(String[] args) 
	{
		int time;
		int design;
		int code;
		int debug;
		int test; 
		
		Scanner input = new Scanner(System.in);
		
		System.out.println("Enter the number of minutes spent on each of the following project tasks: "); 
		System.out.println(" ");
		
		System.out.println("Designing: ");
		design = input.nextInt();
		
		System.out.println("Coding: ");
		code = input.nextInt(); 
		
		System.out.println("Debugging: "); 
		debug = input.nextInt(); 
		
		System.out.println("Testing: ");
		test = input.nextInt(); 
		
		time = design+code+debug+test; 
		
		System.out.println(" ");
		System.out.println("Designing\t" + design/time + "%");
		System.out.println("Coding\t\t" + code/time + "%");
		System.out.println("Debugging\t" + debug/time + "%");
		System.out.println("Test\t\t" + test/time + "%");
		
			
		

	}

}




This is what the output looks like:


Enter the number of minutes spent on each of the following project tasks:

Designing:
120
Coding:
240
Debugging:
30
Testing:
30

Designing 0%
Coding 0%
Debugging 0%
Test 0%

Is This A Good Question/Topic? 0
  • +

Replies To: I can't figure out what's wrong with this program?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: I can't figure out what's wrong with this program?

Posted 05 February 2012 - 01:46 PM

An int/int returns 0 for fractional amounts. Cast either operand to a double and you should be fine. You'll then need to adjust the result to a percentage.
Was This Post Helpful? 1
  • +
  • -

#3 ajordan24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-January 12

Re: I can't figure out what's wrong with this program?

Posted 05 February 2012 - 01:48 PM

View PostGregBrannon, on 05 February 2012 - 01:46 PM, said:

An int/int returns 0 for fractional amounts. Cast either operand to a double and you should be fine. You'll then need to adjust the result to a percentage.


Wow I can't believe I didn't realize that! Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#4 htc(hot-taco-cheese)  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 04-December 10

Re: I can't figure out what's wrong with this program?

Posted 05 February 2012 - 01:50 PM

You are dividing two integers and the answer is a decimal less than 1 thus giving you zero.
To get a percent, you will need to use doubles because they will calculate the answer to a decimal.

Edit* whoops. Not fast enough...

This post has been edited by htc(hot-taco-cheese): 05 February 2012 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1