查找while循环中的最大整数和最小整数

时间:2016-09-05 06:23:26

标签: java while-loop

import java.util.Scanner;
public class Lab4 
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter integers (Zero to exit): ");

        //integers by value
        int n = in.nextInt();
        int evenNumber = -1;
        int oddNumber = 0;
        int count = 0;
        int largest = n;
        int smallest = n;
        int counter = 1;
        int total = 0;
        int average = 0;

        //boolean to end loop
        boolean done = false;

        //start of loop
        while (!done)
        {
            n = in.nextInt();
            total = total + n;
            //end loop method
            if (n == 0)
            {
                done = true;
            }
            //count number of integers entered
            else
            {
            count = count + 1;
            }
            //count of even numbers
            if ( n % 2 == 0){
                evenNumber = evenNumber + 1;
            }
            //count of odd numbers
            else
            {
                oddNumber = oddNumber + 1;
            }
            //find min and max integers !NOT WORKING!
            if (n > largest){
            }
            if (n < smallest){

            }
            if (count > 0){
                average = total / count;
            }
            else{
                    System.out.println("No Data Entered.");
                }
            counter++;
        }
        System.out.println("Total number of integers entered is: "+ count);
        System.out.println("Total even numbers entered: "+ evenNumber);
        System.out.println("Total odd numbers entered: "+ oddNumber);
        System.out.println("The largest integer: "+ largest);
        System.out.println("The smallest integer: "+ smallest);
        System.out.println("The average value is: "+ average);
    }
}

上面是我的代码,我已经尝试了一切,但我没有得到任何错误,但是最小和最大的返回没有正确回来,如果用户输入负整数,我只获得最大整数,如果是正数,则只有最小值。任何帮助都会很棒!

4 个答案:

答案 0 :(得分:4)

您正在检查n最大或最小。但你没有做任务。试试这个:

if (n > largest){
  largest = n ;
}
if (n < smallest){
  smallest = n ;
}

答案 1 :(得分:3)

您可以使用Math.min(int, int)Math.max(int, int)分别找到smallestlargest。您只需要一个count。您可以使用int声明多个,变量,我会使用+++=来使代码更加惯用。您可以使用break来终止循环。此外,不要执行整数数学来确定平均值。将它们放在一起可能看起来像

// integers by value
int n = in.nextInt();
int evenNumber = 0, oddNumber = 0, count = 0, total = 0;
int largest = n, smallest = n;
// start of loop
while (true) {
    n = in.nextInt();
    // end loop method
    if (n == 0) {
        break;
    }
    total += n;
    count++;
    // count of even numbers
    if (n % 2 == 0) {
        evenNumber++;
    } else {
        oddNumber++;
    }
    largest = Math.max(n, largest);
    smallest = Math.min(n, smallest);
}
System.out.println("Total number of integers entered is: " + count);
System.out.println("Total even numbers entered: " + evenNumber);
System.out.println("Total odd numbers entered: " + oddNumber);
System.out.println("The largest integer: " + largest);
System.out.println("The smallest integer: " + smallest);
double average = total / (double) count;
System.out.println("The average value is: " + average);

可以<{1}}通过在break循环条件中执行测试作业来消除while是另一个相当常见的习语)

while ((n = in.nextInt()) != 0) {
    total += n;
    count++;
    if (n % 2 == 0) {
        evenNumber++;
    } else {
        oddNumber++;
    }
    largest = Math.max(n, largest);
    smallest = Math.min(n, smallest);
}

答案 2 :(得分:0)

另一种可能的解决方案是使用ListCollections

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    List<Integer> list = new ArrayList<Integer>();

    int evenNumber=0;
    int oddNumber=0;
    int total=0;
    int n;

    System.out.print("Please enter integers (Zero to exit): ");

    while(true){
        n = in.nextInt();
        if(n==0) {
            break;
        } else {
            list.add(n);
            total += n;
            if(n%2==0){
                evenNumber++;
            } else {
                oddNumber++;
            }
        }
    }

    Collections.sort(list);

    System.out.println("Total number of integers entered is: "+ list.size());
    System.out.println("Total even numbers entered: "+ evenNumber);
    System.out.println("Total odd numbers entered: "+ oddNumber);
    System.out.println("The largest integer: "+ list.get(list.size() - 1));
    System.out.println("The smallest integer: "+ list.get(0));
    System.out.println("The average value is: "+ total/list.size());
}

答案 3 :(得分:0)

错误是

if (n > largest) {
  largest = n; // Probably already tried
}
if (n < smallest) {
  smallest = n; // Probably already tried
}

结合需要

int largest = Integer.MIN_VALUE;
int smallest = Integer.MAX_VALUE;

不幸的设计决定在循环之前使用第一个元素(nextInt)来进行正确的最大和最小初始化,这可能导致了这种疏忽。

或者你可以做到:

average

并将其余部分置于0,并仅在循环中调用nextInt。

由于int average = count == 0 ? 0 : (total + count/2)/count; double average = count == 0 ? 0.0 : ((double) total) count; 未在循环内使用,您可以在外部确定它。 你在那里进行整数除法,所以可以考虑舍入:

#include <iostream>
#include <climits>
#include<vector>
#include<list>
void primitive_calculator(int number)
{
    std::vector<int> min_steps(number+1,INT_MAX);
    std::list<int> path[number+1];
    min_steps[0]=0; min_steps[1]=0;
    path[0].push_back(0);
    path[1].push_back(1);
    for (int i=2 ; i<=number ; i++)
    {
        if(i%3==0)
        {
            if(min_steps[i/3] < min_steps[i])
            {
                min_steps[i]=min_steps[i/3]+1;
                path[i]=path[i/3];
                path[i].push_back(i);
            }
        }
        if(i%2==0)
        {
            if( min_steps[i/2] < min_steps[i])
            {
                min_steps[i]=min_steps[i/2]+1;
                path[i]=path[i/2];
                path[i].push_back(i);
            }
        }

        if( min_steps[i-1] < min_steps[i])
        {
             min_steps[i]=min_steps[i-1]+1;
             path[i]=path[i-1];
             path[i].push_back(i);
        }
    }
    std::cout<<min_steps[number]<<"\n";
    while(!path[number].empty())
    {
        std::cout<<path[number].front()<<" ";
        path[number].pop_front();
    }

}
int main()
{
    int number;
    std::cin>>number;
    primitive_calculator(number);
    return 0;
}