如何允许预定义数量的条目到java程序

时间:2014-02-08 09:07:12

标签: java command-line

我的程序允许用户输入20个条目。每个条目作为一个条目输入,然后从命令行按Enter。我想补充一下该程序的能力 “你剩下20个参赛作品” 然后 “你剩下19个参赛作品” 等等......当用户向程序输入更多数据时。

如果有人能告诉我将其添加到当前代码中的最简单方法,我将不胜感激。

import java.util.ArrayList;
import java.util.Scanner;

class project02Diaz
{   
    private static Double calculate_average( ArrayList<Double> myArr )
    {
        Double sum = 0.0;
        for (Double number: myArr)
        {
            sum += number;
        }
        return sum/myArr.size(); 
    }

    public static void main( String[] args )
    {
        Scanner           scan  = new Scanner(System.in);
        ArrayList<Double> myArr = new ArrayList<Double>();
        int               count = 0;

        System.out.println("Enter one number at a time be averaged, you have 20 entries remaining:");

        for (String inputs = scan.nextLine() ; !inputs.matches("[qQ]")  ; inputs = scan.nextLine())
        {
            if (count == 20)
            {
                System.out.println("You entered more than 20 numbers, you suck!");
                break;
            }
            try{
                myArr.add(Double.valueOf(inputs));
                count++; 
                System.out.println("Please enter another number or press Q for your average");
            }
            catch (NumberFormatException e) {
                System.out.println("Try again you tard! You entered more than one number, or not a valid number at all.");
                continue;                   
            }      

        }
        Double average = calculate_average(myArr);
        System.out.println("Your average is: " + average);
    }
}

2 个答案:

答案 0 :(得分:2)

你又来了!好吧,也要做那个。没有太多你需要改变的地方:只需将你的计数器从20减计,并每次输出它的stauts。此外,如果达到0,请停止。

public static void main( String[] args )
{
    Scanner           scan        = new Scanner(System.in);
    ArrayList<Double> myArr       = new ArrayList<Double>();
    int               entriesLeft = 20; // changed counter that counts upwards to one counting down.

    System.out.println("Enter one number at a time be averaged, you have 20 entries remaining:");

    // added additional condition to be checked: we now also stop if maximum number of entries ie reached.
    for (String inputs = scan.nextLine() ; entriesLeft > 0 && !inputs.matches("[qQ]")  ; inputs = scan.nextLine())
    {
        try{
            myArr.add(Double.valueOf(inputs));
            entriesLeft--; //counting one down
            System.out.println("Please enter another number or press Q for your average");
            // telling user how much numbers are left
            System.out.println("You have " + entriesLeft + "numbers left.");
        }
        catch (NumberFormatException e) {
            System.out.println("Try again you tard! You entered more than one number, or not a valid number at all.");
            continue;                   
        }      

    }
    Double average = calculate_average(myArr);
    System.out.println("Your average is: " + average);
}

请注意,将整数entriesLeft附加到字符串不是非常有效,更好的方法是调用Integer.toString(entriesLeft)

答案 1 :(得分:0)

import java.util.ArrayList;
import java.util.Scanner;
class project02Diaz
{   
    private static Double calculate_average( ArrayList<Double> myArr )
{
    Double sum = 0.0;
    for (Double number: myArr)
    {
        sum += number;
    }
    return sum/myArr.size(); 
}

    public static void main( String[] args )
    {
        Scanner           scan  = new Scanner(System.in);
        ArrayList<Double> myArr = new ArrayList<Double>();
        int               count = 0;

        System.out.println("Enter one number at a time be averaged, you have 20 entries remaining:");

        for (String inputs = scan.nextLine() ; !inputs.matches("[qQ]")  ; inputs = scan.nextLine())
        {
        System.out.println("You have "+ String.valueOf(20 - count -1) +" number of enteries left");
        if (count == 19)
            {
                System.out.println("You entered more than 20 numbers, you suck!");
                break;
            }
            try{
                myArr.add(Double.valueOf(inputs));
                count++; 
                System.out.println("Please enter another number or press Q for your average");
            }
            catch (NumberFormatException e) {
                System.out.println("Try again you tard! You entered more than one number, or not a valid number at all.");
                continue;                   
            }      
        }
        Double average = calculate_average(myArr);
        System.out.println("Your average is: " + average);
    }
}