0

我编写了一些代码,要求用户输入值,标记值为 -99,然后显示最大值和最小值。它正在工作,但将 -99 显示为最小值。所以我摆弄它,将 && != -99 作为最小值,但后来发生了无限循环,所以我把那个代码拿出来了。但是我必须删除一些重要的东西,因为现在它没有给我最大和最小的它只是进入一个无限循环。

你能给我一些指点吗?

这是我的代码;

public class Practice
{
    public static void main(String[] args)
    {
        int smallest = 0; int largest = 0; int integer;

        // Create a Scanner object for keyboard input.
        Scanner input = new Scanner(System.in);

        System.out.println("Enter an integer: ");
        System.out.println("Enter -99 when finished.");
        System.out.println();

        int n = input.nextInt();
        integer = input.nextInt();
        smallest = integer; 

        while (integer != -99)
        {
            for(int i = 2; i < n; i++)
            {
                integer = input.nextInt();
                if(integer > largest)
                {
                    largest = integer;
                }
                if(integer < smallest)
                {
                    smallest = integer;
                }
            }
        }
        System.out.println("The largest number is:" + largest);
        System.out.println("The smallest number is : "  + smallest);
    }
}
4

3 回答 3

0

这将消除最小答案总是返回值 0 的问题。

// Import all required packages
import java.util.Scanner;


public class Problem10
{
    public static void main(String[] args)
    {
        // I'm breaking these up to be a little more readable
        int smallest = 999999999;
        int largest = 0;
        // let's rename this something... not integer
        int userInt;

        // Create a Scanner object for keyboard input.
        Scanner input = new Scanner(System.in);

        // Let's make the prompt a little more descriptive
        System.out.println("Enter some integers: ");
        System.out.println("Enter -99 when finished.");
        System.out.println();

        // Let's get rid of all these other variables and just get userInt
        userInt = input.nextInt();

        // Assuming we want to exit if the first number they enter is -99 we'll
        //  test before doing anything
        while (userInt != -99)
        {   
            if(userInt > largest)
            {   
                largest = userInt;
            }
            if(userInt < smallest)
            {   
                smallest = userInt;
            }
            // Now that we've evaluated the first number entered we'll get
            //  the next number before we loop again so we'll be ready to see
            //  if we've hit an exit condition
            userInt = input.nextInt();
        }
        // This should be all you need to do, and after the loop is finished
        //  you can report.
        System.out.println("The largest number is:" + largest);
        System.out.println("The smallest number is : "  + smallest);
    }
}
于 2014-09-07T07:37:36.527 回答
0

不需要嵌套for循环。你应该能够只用一个while循环来解决这个问题。当你解决这个问题时,你可能会发现你有比你需要的更多的变量。

于 2014-09-07T06:51:02.313 回答
0

好的,这里有几件事(假设我正确理解了您的程序的目标):

public class Practice
{
    public static void main(String[] args)
    {
        // I'm breaking these up to be a little more readable
        int smallest;
        int largest;
        // let's rename this something... not integer
        int userInt;

        // Create a Scanner object for keyboard input.
        Scanner input = new Scanner(System.in);

        // Let's make the prompt a little more descriptive
        System.out.println("Enter some integers: ");
        System.out.println("Enter -99 when finished.");
        System.out.println();

        // Let's get rid of all these other variables and just get userInt
        userInt = input.nextInt();

        // To avoid doing anything else if we get -99 for the first number
        //  we'll test and exit accordingly.
        if (userInt == -99)
        {
            return;
        }

        smallest = userInt;
        largest = userInt;

        // Assuming we want to exit if the first number they enter is -99 we'll
        //  test before doing anything
        while (userInt != -99)
        {   
            // Note: this is not the cleanest, because we'll be wasting an
            //  iteration on data we've already stored in smallest & largest
            //  but it shouldn't hurt and I'm trying to minimize changes to
            //  your original structure.
            if(userInt > largest)
            {   
                largest = userInt;
            }
            if(userInt < smallest)
            {   
                smallest = userInt;
            }
            // Now that we've evaluated the first number entered we'll get
            //  the next number before we loop again so we'll be ready to see
            //  if we've hit an exit condition
            userInt = input.nextInt();
        }

        // This should be all you need to do, and after the loop is finished
        //  you can report.
        System.out.println("The largest number is:" + largest);
        System.out.println("The smallest number is : "  + smallest);
    }
}

请注意,这仍然不会处理任何异常,并且可能不是最干净的整体方法,但我认为它更符合您正在寻找的内容。(我确保避免尝试任何异常捕获方法,因为我不确定您是否已经了解了这些方法)。

我真的不确定这两个嵌套循环在您的原始代码中的目的是什么,因为您真正想做的就是不断要求新值并根据存储的最大和最小来评估它们。

嵌套循环通常只应在处理多维数据时进行。你当然不会在这里做。

我也很困惑为什么你在那里有一些额外的变量,所以我强烈建议确保你可以在单步执行代码时考虑每一个变量(手动或使用调试器 - 你可能应该学习如何使用调试器)。

于 2014-09-07T01:44:24.607 回答