I am using a scanner class to average numbers together. I am using a method to do the averaging. I do not want the program to run if there are more than 20 args. I cant seem to get this to work. I am very new at java and trying to learn.

I appreciate any help I can get. Thanks!

import java.util.Scanner;

class programTwo {


    public static void main (String[] args) {

        Scanner scan = new Scanner(System.in);
        double x = 0.00d;

        if (args != null) {
            System.out.println ("Enter your numbers to be averaged. Remember no more than 20!:");
            x = scan.nextInt();

            if (x <= 21) {
                System.out.println("Please do not add more than 20 numbers");   
            }  
       } else {
       }

    }

    public double average(double [] values) {
        double average = 0.0;
        if ((values != null) && (values.length > 0)) {
            for (double value : values) {
                average += value;
            }
        average /= values.length; 
        }
        return average;
    }

}
有帮助吗?

解决方案 2

Here is a solution (cleaning up a lot of your code as well) that gets all the numbers on one line after the start of the program:

import java.util.Scanner;

class programTwo {


    public static void main (String[] args) {

        Scanner scan = new Scanner(System.in);
        double values[] = new double[20];
        int count = 0;
        System.out.println ("Enter your numbers to be averaged. Remember no more than 20!:");
        String inputs = scan.nextLine();
        scan = new Scanner(inputs); // create a new scanner out of our single line of input
        while(scan.hasNextDouble())
        {
            if(count == 20)
            {
                System.out.println("You entered too many numbers! Fail.");
                return;
            }
            values[count] = scan.nextDouble();
            count += 1;
        }

        System.out.println("Your average is: " + average(values, count));
    }

    public static double average(double [] values, int count) {
        double average = 0.0;
        for (double value : values) {
            average += value;
        }
        average /= count;

        return average;
    }

}

I got thinking you might want to use the args that are passed to main, since you use a null check, so you want to run your program like this:

java programTwo num1 num2 num3 num4 num5

etc. If that's the case, we have another solution:

class programTwo {


    public static void main (String[] args) {
        if(args.length > 20)
        {
            System.out.println("You entered too many numbers! Fail.");
            return;
        }
        double values[] = new double[args.length];
        for(int i=0; i< args.length; ++i)
            values[i] = Double.valueOf(args[i]);

        System.out.println("Your average is: " + average(values));
    }

    public static double average(double [] values) {
        double average = 0.0;
        for (double value : values) {
            average += value;
        }
        average /= values.length;

        return average;
    }

}

其他提示

Just run a while loop that breaks when 20 "args" is met or until a break like -1 is entered. Then if you are taking double values, you should use x = scan.nextDouble(). You also do not have a place where you are inserting the values into your array. At the end of your while loop you could put x into an array of doubles.

private double x;
private double Foo[] = new Foo[20];
private int this = 0; //Your counter

while(this < 20 && x != -1)
{
   x = scan.nextDouble();
   Foo[this++] = x;

}

Then carry out your public double Average by adding up the values in the array and dividing by (double)this

The args != null check is unnecessary. One way to accomplish what you want is to accept numbers while the scanner has a next number (scanner.hasNext() perhaps) and break if the number of inputs thus far is less than 20. Since the number of double numbers is unknown, you're better off using an ArrayList.

List<Double> doubles = new ArrayList<Double>();

and calling the add method on doubles

doubles.add(x); Then pass this to a method that averages the values in the arraylist.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top