简体   繁体   中英

How to get the output of my variables to sort from least to greatest?

I have a lab for a programming class that I almost have complete and I believe I have followed all the steps to the best of my ability, except that I can't get the result of my three variables int1, int2, and int3 to properly sort in the console output. Maybe it's an issue with my nested conditional statements. Our professor didn't quite adequately explain the intricacies of the conditional statements we needed to use that well. I will post my code below.

Here are the instructions with a flowchart he made: https://www.dropbox.com/s/q9hfuo5nzmbvrg3/Lab03%20Assignment.pdf?dl=0

 package lab03;
import java.util.Scanner;
public class Lab03 {
    public static void main(String[] args) {
        // Create the scanner input
        Scanner input;
        input=new Scanner(System.in);
        // Prompt the user to enter the first integer between 0 and 99.
        int input1;
        System.out.println("Please enter an integer between 0 and 99: ");
        input1 = input.nextInt();
        // Check if input1 is within a valid range.
        if (input1 <0 || input1 > 99) {
          // If the input is outside of the range, give them a second chance to correct their error.
          System.out.println("Please try again and enter an integer THAT MUST BE between 0 and 99: ");
          input1 = input.nextInt();
        }
          // Check input1 again.
        if (input1 <0 || input1 > 99) {
            // Still invalid a second time? Exit the program!!!
                  System.out.println("Still outside of the valid range. Program ending...");
                  return;
        }
            else {
            // Prompt the user to enter the second integer between 0 and 99.
            int input2;
            System.out.println("Please enter a second integer between 0 and 99: ");
            input2 = input.nextInt();
            // Check if input2 is within valid range.
            if (input2 <0 || input2 > 99) {
                // If the input is outside of the valid range, give them another chance to correct their mistake.
                System.out.println("Please attempt this one more time and enter a second integer THAT IS REQUIRED TO BE between 0 and 99: ");
                input2 = input.nextInt();
                // Check input2 again.
                if (input2 <0 || input2 > 99) {
                    // Still invalid for the 2nd time? Exit the program!!!!!
                    System.out.println("Your second integer is still outside of an acceptable range. Program is now terminating...hasta la vista, baby!");
                    return;
                }
            }
            // Declare max and min variables.
            int max;
            int min;
                /* Compare the values of input1 and input2 to determine which one is "min" and which one is "max" so that the message shown to user makes sense.
                i.e. if the two integers are 20 and 80. 20 is smaller so it must be the min, while 80 is larger so it must be the max.
                It also compensates if the user inserts the larger number for the first input prompt and a smaller one for the second prompt
                Our program will automatically fix the order.
            */
                if (input1 < input2) {
                    min = input1;
                    max = input2;
                }
                else {
                    min = input2;
                    max = input1;
                }
            // Use the functions to assign values for int1, int2, and int3 using the combo of the min and max values as well as the Math.random method in the Math class.
            double int1;
                int1 = min + (int)(Math.random() * ((max - min) + 1));
            double int2;
                int2 = min + (int)(Math.random() * ((max - min) + 1));
            double int3;
                int3 = min + (int)(Math.random() * ((max - min) + 1));
            // Swap values of the integers if needed to allow for correct sorting output later on.
                if (int1 > int2) {
                double temp = int1;
                int1 = int2;
                int2 = temp;
            }
                else if (int2 > int3) {
                double temp = int2;
                int2 = int3;
                int3 = temp;
            }
                else if (int1 > int2) {
                double temp = int1;
                int1 = int2;
                int2 = temp;
            }
            // Output the range of min and max.
            System.out.println("The range begins at " + min + " and ends at " + max);
            // Output three random sorted integers
            System.out.println("Three sorted random integers between " + min + " and " + max + " are: ");
            // For the three integers, we must determine if the integer is odd or even so that the appropriate label can be appended to the output. A number is even if it is divisible by 2, else it is odd.
            if (int1 % 2 == 0) {
                System.out.println((int) int1 + " Even");
            }
            else {
                System.out.println((int) int1 + " Odd");
            }
            if (int2 % 2 == 0) {
                System.out.println((int) int2 + " Even");
            }
            else {
                System.out.println((int) int2 + " Odd");
            }
            if (int3 % 2 == 0) {
                System.out.println((int) int3 + " Even");
            }
            else {
                System.out.println((int) int3 + " Odd");
            }
            // Define sum
            double sum = int1 + int2 + int3;
            // Define product
            double product = int1 * int2 * int3;
            // Define quotients
            double quotient1 = (int1 / int2);
            double quotient2 = (quotient1 / int3);
            double quotient = (quotient2);
            // Display sum output
            System.out.println("Sum = " + (int) sum);
            // Display product output
            System.out.println("Product = " + (int) product);
            // Display quotient output
            System.out.println("Quotient (Int1 / Int2 / Int3) = " + quotient);
            }
    }
}

It should be noted that we are not allowed to use any more efficient methods like loops or arrays of solving the problem in the assignment, as we haven't learned that together as a class yet and he doesn't want us going ahead in the book lol. We can use the switch statement but I didn't use it in here because I don't think I need it.

All help would be appreciated. Maybe this will help out someone else if they are a new Java programmer and have a similar issue in their projects.

EDIT: This is the part of the instructions that I am struggling to make work: "The program will then sort them in ascending order using decision statements and output them to the console in ascending order on separate lines."

You have done it almost perfect. Just a small change. else if will only be exceuted when if fails. So, for example, input 8 9 7 , you won't swap 8 and 9 as 8 is not greater than 9 in 1st if condition and the 2nd else if will never be executed.

The correct code would be to use to 3 independent if statements.

if (int1 > int2) {
    double hold = int1;
    int1 = int2;
    int2 = hold;
}
if (int2 > int3) {
    double hold = int2;
    int2 = int3;
    int3 = hold;
}
if (int1 > int2) {
    double hold = int1;
    int1 = int2;
    int2 = hold;
}

Also, replace temp with hold as specified in your assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM