Edward Lavaire Edward Lavaire - 22 days ago 5
Java Question

Program that will output three integers in descending ordrer

This program is meant to prompt a user to enter three integers, store the integers in three separate variables, and output the three integers in descending order (highest to lowest values).

import java.util.Scanner;

public class ProgramToo
{
public static void main(String [] args)
{
Scanner kbd = new Scanner(System.in);

System.out.println("Enter the first number:");
int num1 = kbd.nextInt();

System.out.println("Enter the second number:");
int num2 = kbd.nextInt();

System.out.println("Enter the three number:");
int num3 = kbd.nextInt();

int result = largeSmall(num1, num2, num3);
System.out.println(result);
}
public static int largeSmall(int one, int two, int three)
{
if(one > two && two > three)
{
System.out.println(one + " " + two + " " + three);
}
else if(two > one && one > three)
{
System.out.println(two + " " + one + " " + three);
}
else if(three > two && two > one)
{
System.out.println(three + " " + two + " " + one);
}
else
{
System.out.println(one + " " + three + " " + two);
}
return largeSmall(one, two, three);
}
}


When I run this program, it outputs the integers a million times and crashes. Why?

Answer

Looks like you almost had it there. I assume you are a new student. If you just change the method to void (doesn't require a return value), you will get the answer you need from just calling the method. You had the method and your main both looping through println. I only deleted a few lines and changed the method signature to get it working.

public class Application {

    public void start() {

          Scanner kbd = new Scanner(System.in);

          System.out.println("Enter the first number:");
          int num1 = kbd.nextInt();

          System.out.println("Enter the second number:");
          int num2 = kbd.nextInt();

          System.out.println("Enter the three number:");
          int num3 = kbd.nextInt();

          largeSmall(num1, num2, num3);

          }



    public static void largeSmall(int one, int two, int three)
          {
          if(one > two && two > three)
          {
             System.out.println(one + " " + two + " " + three);
          }
          else if(two > one && one > three)
          {
             System.out.println(two + " " + one + " " + three);
          }
          else if(three > two && two > one)
          {
             System.out.println(three + " " + two + " " + one);
          }
          else
          {
             System.out.println(one + " " + three + " " + two);
          }



    }//end start method

}//end application class