Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
C#
/**
 * Write a description of class MinimumMaximum here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
import java.util.Scanner;
public class MinimumMaximum
{

    public MinimumMaximum()
    {
        Scanner number = new Scanner(System.in);
        int min=0, max=0, lcv = 0, num, num1;
        String maxmin;

          while ((lcv < 10) && (lcv >= 0))
        {
            lcv++;
            System.out.print("Please enter a number: ");
            num = number.nextInt();
            max = max(max,num);
        min = min(min,num);
        }










        maxmin= maxmin(max,min);
        System.out.println(maxmin);


    }


    public int max(int max, int num)
    {


        max = 1;




            if (num > max)

            max = num;

           else

           max = max;




    return max;
}

 public int min(int min,int num)
    {



        min = 0;



            if (num < min)
            min = num;

           else if (min > num)
           min = min;

        return min;
    }










    public String maxmin(int max, int min) {
        String maxmin = "The greatest value is " + max + " and the lowest value is "  + min + ".";
        return maxmin;
}

}
Posted
Comments
Member 11190214 22-Apr-15 13:35pm    
I'm supposed to enter 10 numbers and then retrieve the largest and smallest numbers using methods. But every time I run the program only the last number entered is recognized as the largest number with no regards to the previous input.

1 solution

Check your variable naming. You messed up there, to much "max" and "min".

You can reference the class variable with "this.".

also you messed up with return values and references: You are passing the "max" by value and then you return that value.
That is too much. Pass back OR assign values:

Java
private void isMax(int num) // no return value
{
  if (num > this.max){
     this.max = num;
  }
}
 
public int min(int min,int num) // with return value
{
  if (num < min){
    return num;
  }
  return min; // default return
}


Please choose one version and rewrite the other.

EDIT:
Passing Information to a Method or a Constructor[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900