The Artima Developer Community
Sponsored Link

Java Answers Forum
METHODS

2 replies on 1 page. Most recent reply: Oct 23, 2007 7:30 PM by Doug Gunnoe

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
shelby WILLIAMS

Posts: 3
Nickname: lostinjava
Registered: Oct, 2007

METHODS Posted: Oct 20, 2007 1:47 PM
Reply to this message Reply
Advertisement
I AM HAVING TROUBLE DISPLAYING THE SUM OF THE NUMBERS CORRECTLY IN MAIN. CAN ANYONE HELP?
public class sum_Num
{
public static void main(String [] args)
{
int sum = 0;
int n = 10;
compute_sum(n);
System.out.println(“The sum is “ + sum);
System.exit(0);
}

public static void compute_sum(int x)
{
int sum = 0;
int num = 1;
while (num <=x)
{
sum += num;
num++;
}
}
}


Dave H

Posts: 16
Nickname: slebtack
Registered: Apr, 2007

Re: METHODS Posted: Oct 22, 2007 4:07 AM
Reply to this message Reply
You are not returning the answer from compute_sum() to main().

Doug Gunnoe

Posts: 22
Nickname: dgun
Registered: May, 2007

Re: METHODS Posted: Oct 23, 2007 7:30 PM
Reply to this message Reply
public class sum_Num
{
public static void main(String [] args)
{
int sum = 0;
int n = 10;
sum = compute_sum(n);
System.out.println(“The sum is “ + sum);
System.exit(0);
}

public static int compute_sum(int x)
{
int sum = 0;
int num = 1;
while (num <=x)
{
sum += num;
num++;
}
return sum;
}
}

Flat View: This topic has 2 replies on 1 page
Topic: METHODS Previous Topic   Next Topic Topic: java exam

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use