适用于Java的最小和最大整数

时间:2018-10-24 21:56:24

标签: java

在尝试询问之前,我已尝试对此进行研究,但我陷入了困境。我上网浏览youtube,这本书和讲师,她建议使用stackoverflow作为提出问题的资源。

我在执行代码时遇到问题。我将代码放在下面。尽管没有在IDE(Eclipse)中产生任何错误,但它不会产生最小和最大的整数。

我想学习它时没有剪切并粘贴它。最小和最大的整数返回0。我对三个整数使用-1、29、99。

我希望有人可以帮助我朝着正确的方向前进。

// Fig. 2.7: Addition.java
// Addition program that inputs two numbers then displays their sumimport java.util.scanner; // program uses class scanner

import java.io.PrintStream;
import java.util.Scanner;

// program uses class scanner

public class Addition2 {
    // main method begins execution of Java application

    public static void main(String[] args){
    // create a Scanner to obtain input from the command window

        Scanner input = new Scanner(System.in);

        System.out.println ("Please Enter Three Integers Below!!! ");

        System.out.print("Enter First integer: "); // prompt
        int number1 = input.nextInt() ; //read first number from user

        System.out.print("Enter Second integer: "); // prompt
        int number2 = input.nextInt() ; // read second number from user

        System.out.print("Enter Third integer: "); // prompt
        int number3 = input.nextInt() ; // read second number from user

        int sum1 = number1 + number2 + number3; // add numbers, then store total in sum

        int Avg1 = ((number1+number2+number3)/3); // multiply numbers

        int sum3 = number1 * number2 * number3; // subtract numbers

        int Largest = Max(number1, number2, number3);

        int Smallest = Min(number1, number2, number3);  

        System.out.printf("Avg 1 is the Average: %d%n", Avg1); // display sum

        System.out.printf("Sum 3 is Product: %d%n", sum3); // display sum

        System.out.printf("Of -1, 29, and 99: %d%n", Smallest); //display smallest number

        System.out.printf("Of -1, 29, and 99: %d%n", Largest); //display largest number

        if (number1 < number2)
            Largest = number1;

        if (number1 < number3)
            Largest = number1;

        if (number2 < number1)
            Largest = number2;

        if (number2 < number3)
            Largest = number2;

        if (number3 < number2)
            Largest = number3;

        if (number3 < number1)
            Largest = number3;

        if (number1 > number2)
            Smallest = number1;

        if (number1 > number3)
            Smallest = number1;

        if (number2 > number1)
            Smallest = number2;

        if (number2 > number3)
            Smallest = number2;

        if (number3 > number2)
            Smallest = number3;

        if (number3 > number1)
            Smallest = number3;

    } //end method main

    private static int Max(int number1, int number2, int number3) {
        // TODO Auto-generated method stub
        return 0;
    }

    private static int Min(int number1, int number2, int number3) {
            // TODO Auto-generated method stub
            return 0;
    }
} // end class Addition

2 个答案:

答案 0 :(得分:0)

在更新它们之前,您正在打印最大和最小。您的印刷声明为public SelectListItem[] CountriesList; public SelectListItem[] CurrenciesList;

So i have the fields like this previously 
class Inv(models.Model):
    description = models.CharField()  
    model_number = models.CharField()  
    manufacturer = models.CharField()
    def __str__(self):
        return self.id

应该在主方法的结尾。

编辑:您还应该重新考虑查找最大和最小数字的逻辑。您只需对2个数字进行比较即可获得3个数字的列表,以在其中找到最大值或最小值

答案 1 :(得分:0)

寻找一个较小的问题。

两个数字 a b 的最大值是多少?

int max(int a, int b) {
    if(a >= b) {
        return a;
    } else {
        return b;
    }
}

三个数字 a b c 的最大值是多少?

max(a, b, c) => max(max(a, b), c)