如何获取变量从最小到最大排序的输出?

时间:2018-09-22 02:40:28

标签: java netbeans

我有一个几乎可以完成的编程课的实验室,我相信我已尽我所能执行了所有步骤,但无法获得三个变量int1,int2和int的结果。 int3可以在控制台输出中正确排序。也许这是我嵌套的条件语句的问题。我们的教授没有很好地解释我们需要很好地使用的条件语句的复杂性。 我将在下面发布我的代码。

以下是他制作的流程图的说明: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);
            }
    }
}

应该注意,我们不允许使用任何更有效的方法(例如循环或数组)来解决作业中的问题,因为我们还没有像班级那样一起学习,他不希望我们继续前进在本书中大声笑。我们可以使用switch语句,但是我在这里没有使用它,因为我认为我不需要它。

所有帮助将不胜感激。如果他们是新的Java程序员并且在他们的项目中遇到类似的问题,那么这可能会对其他人有所帮助。

编辑:这是我努力工作的一部分说明:“然后,程序将使用决策语句按升序对它们进行排序,并在单独的行中按升序将它们输出到控制台。”

1 个答案:

答案 0 :(得分:1)

您几乎做到了完美。只是很小的变化。else if仅在if失败时才会被执行。因此,例如,输入8 9 7,您将不会交换8和9,因为在第一个if条件下8不大于9,并且第二个else if将永远不会执行。

正确的代码将是使用3个独立的if语句。

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;
}

此外,按照作业中的指定,将temp替换为hold