Click here to Skip to main content
15,886,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the whole question:

1. Write a method:
+ inDescendingOrder(a:int, b:int, c:int): boolean

2. inDescendingOrder takes three parameters and returns true if a, b and c are in descending order and false otherwise.

3. Write a main method that asks the user for three numbers and then uses them to call inDescendingOrder() and reports if the initial 3 numbers were given in descending order or not.

A typical interaction might look like: Welcome to inDescendingOrder:
Enter the first number: 8
Enter the second number: 4
Enter the third number: 5
Those numbers were not entered in descending order. Where eight, four and five were the numbers entered by the user.









(When I try to run the code in the jGRASP, even if the numbers are not in descending order, it still says it's in descending order).

I want the code to be in if/ else statements and not arrays.

What I have tried:

Java
import java.util.Scanner;
public class DescendingOrder{
   public static void main(String[] args){
      System.out.println("Welcome to inDecending Order");
      int a = readInt("Enter first number:  ");
      int b = readInt("Enter second number: ");  
      int c = readInt("Enter third number: ");
      inDescendingOrder(a,b,c);
      if(true){
         System.out.println("Those number were entered in decending order");
      }else if(false){
         System.out.println("Those number were not entered in decending order");
       }else{
         System.out.println("Error");
      //inDescendingOrder(a,b,c);
      }
   }
   public static boolean inDescendingOrder(int a, int b, int c){
      boolean flag;
      if ((a<= b) && (a <= c)){
         return flag= false;
      }else if ((a<=b) && (a>=c)){
         return flag= false;
     
        // return "Those number were not entered in decending order";
      }else if ((a >= b) && (a >= c)){
         return flag = true;
         //return "Those number were entered in decending order";
      }else{
         return true;
      }
   }  

   public static int readInt(String prompt){    
      Scanner sc = new Scanner(System.in);  
      System.out.println(prompt);  
      return sc.nextInt();  
   }  
}
Posted
Updated 10-Dec-22 19:32pm
v2

1 solution

THink about what "Descending order" means: the only way that is can be correct is if a is the largest number, and c is the smallest.
So check for that specific case:
int inDescendingOrder(int a, int b, int c)
   {
   if ((a >= b) && (b >= c))
      return true;
   return false;
   } 
 
Share this answer
 

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