Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Where to declare a method within a program?

$
0
0
Hello. :)

I am currently writing a program that adds together any number of integers entered by the user, adds them together and outputs both the sum and the largest number the user entered - for this particular part of the program I am using a method called 'returnLargest', since the number of values to be added together is not fixed. However, I am a bit stumped as to where abouts in the program the method should be declared.
For some reason, the compiler won't let me declare the method either just inside main or before/after it. :S

Any help/guidance would be greatly appreciated. :)

Here is my code:

import java.util.*;

public class AddUp01
{
		static int returnLargest(int currentValue)
		
		{
		
		currentValue = input.nextInt();
		
		if (currentValue > largest)
		
		largest = currentValue;
		
		return largest;
		
		}
	
public static void main(String[] args)
{		
  	int limit = -9999;
    int sum = 0;
    int largest = 0;
    int values = 0;
	int currentValue = 0;
    
  	Scanner input = new Scanner(System.in);  
  	
    System.out.print("Enter N number of values to add together: ");
    values = input.nextInt();
      
      System.out.print("Enter value: "); 
      currentValue = input.nextInt();    
      
      while (currentValue >=1 && currentValue <=100)          
          
      {
          sum = sum + currentValue;
          currentValue = input.nextInt();
      }
      
      if (currentValue == -9999)
          
      {
          System.out.println("Invalid value entered!");
      }
      
      System.out.println(largest);
      System.out.println("sum = " + sum);
  }
  
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>