Quantcast
Channel: Tech Support Guy
Viewing all articles
Browse latest Browse all 38405

PLEASE READ! Java, print largest number

$
0
0
Hello. :)

I have written a program in Java that reads in a sequence of x amount of numbers, terminated by -9999 and adds them together, printing the result to the screen.
My problem is that I also want to make it so the program prints out the largest number the user entered as well as the sum of the sequence.
I should point-out that I must do this using METHODS ONLY.

I am currently using Windows XP.

Thanks in advance for any assistance offered.

Code:

import java.util.*;

public class AddUp01
{
       
  public static void main(String[] args)
  {
          static int largest(currentValue())
         
          {
                  if(currentValue > largest)
          {
                  largest = currentValue;
                  return largest;
          }
         
          }
         
          int limit = -9999;
    int sum = 0;
      int values = 0;
      int largest = 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("sum = " + sum);
  }
}


Viewing all articles
Browse latest Browse all 38405

Trending Articles