Edward Lavaire
Edward Lavaire

Reputation: 31

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?

Upvotes: 0

Views: 10756

Answers (6)

Nathan
Nathan

Reputation: 1

import java.util.Scanner;

public class AscendingAndDescending//defining a class AscendingAndDescending  

{

   public static void main(String[] args) //main method

   {

       int n1,n2,n3,min,max,m;

       Scanner in = new Scanner(System.in);//creating Scanner class object

       System.out.print("Enter an integer: ");//print message

       n1 = in.nextInt();//input value

       System.out.print("And another: ");//print message

       n2 = in.nextInt();//input value

       System.out.print("And just one more: ");//print message

       n3 = in.nextInt();//input value

       min = n1; //use min variable that holds value

       max = n1; //use mix variable that holds value

       if (n2 > max) max = n2;//use if to compare value and hols value in max variable

       if (n3 > max) max = n3;//use if to compare value and hols value in max variable

       if (n2 < min) min = n2;//use if to compare value and hols value in min variable

       if (n3 < min) min = n3;//use if to compare value and hols value in min variable

       m = (n1 + n2 + n3) - (min + max);//defining m variable that arrange value

       System.out.println("Ascending: " + min + " " + m + " " + max);//print Ascending order value

       System.out.println("Descending: " + max + " " + m + " " + min);//print Descending order value

   }

}

Upvotes: 0

daoud daoud
daoud daoud

Reputation: 1

int num=0; Scanner kbd = new Scanner(System.in); num = kbd.nextInt(); System.out.println(num); Run this program but enter text instead of an integer. The program should crash and tell you what kind of exception was thrown by the nextInt method. Wrap this code inside a try/catch block where you catch the exception that is thrown. Add a loop so the user must enter the number again if text is entered.

Upvotes: 0

Feras Abushahla
Feras Abushahla

Reputation: 1

Scanner k = new Scanner(System.in);
System.out.println("Enter the first number");
int c = k.nextInt();
System.out.println("Enter the second number");
int c2 = k.nextInt();
System.out.println("Enter the third number");
int c3 = k.nextInt();
int max = 0, mid = 0, min = 0;
if (c > c2 && c > c3) {
    max = c;
    mid = (c2 > c3) ? c2 : c3;
    min = (c2 > c3) ? c3 : c2;
    System.out.println("In ascending :"+min+","+mid+","+max);
    System.out.println("In desascending :"+max+","+mid+","+min);
} else if (c2 > c && c2 > c3) {
    max = c2;
    mid = (c > c3) ? c : c3;
    min = (c > c3) ? c3 : c;
    System.out.println("In ascending :"+min+","+mid+","+max);
    System.out.println("In desascending :"+max+","+mid+","+min);
} else if (c3 > c && c3 > c2) {
    max = c3;
    mid = (c > c2) ? c : c2;
    min = (c > c2) ? c2 : c;
    System.out.println("In ascending :"+min+","+mid+","+max);
    System.out.println("In desascending :"+max+","+mid+","+min);
}

Upvotes: 0

k_g
k_g

Reputation: 4463

blm's solution will work, but I thought it might be useful to know what was wrong with yours. You were constantly calling your same function over and over again. To fix it, do the following

  1. Change the return type to void
  2. Remove the return statement recursion
  3. Replace int result = largeSmall(num1, num2, num3); System.out.println(result); with largeSmall(num1, num2 num3);.

Upvotes: 0

Christian Hapgood
Christian Hapgood

Reputation: 31

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

Upvotes: 0

Brandon
Brandon

Reputation: 317

Your solution is really over-engineered. Just do something like this:

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();

  Integer[] arr = new Integer[3]
  arr[0] = num1;
  arr[1] = num2;
  arr[2] = num3;
  Arrays.sort(arr, Collections.reverseOrder());
  System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
}   

Upvotes: 2

Related Questions