Java 如何从数组中计算输入

Java 如何从数组中计算输入,java,Java,我正在处理的这个阵列有问题。在for循环中,我需要以某种方式计算基数百分比。索引0处的元素将存储OBP。如何保留用户输入的信息以计算OBP?多谢各位 for (int index = 0; index < years.length; index++) { System.out.print("For Year " + (index +1 ) + "\nEnter number of hits: "); years[index] = keyboa

我正在处理的这个阵列有问题。在for循环中,我需要以某种方式计算基数百分比。索引0处的元素将存储OBP。如何保留用户输入的信息以计算OBP?多谢各位

     for (int index = 0; index < years.length; index++)
     {
       System.out.print("For Year " + (index +1 ) + "\nEnter number of hits: ");
       years[index] = keyboard.nextInt();

       System.out.print("For Year " + (index +1) + "\nEnter number of walks: ");
       years[index] = keyboard.nextInt();

       System.out.print("For Year" + (index +1) + "\nEnter the number of times player"
                          + "has been hit by a pitch:");
       years[index] = keyboard.nextInt();

       System.out.print("For Year" + (index +1) + "\nEnter the number of at bats:");
       years[index] = keyboard.nextInt();

       System.out.print("For Year" + (index +1) + "\nEnter the number of sacrafice flies" 
                          + "that year: ");
       years[index] = keyboard.nextInt();

     }
for(int index=0;index
您需要将用户输入的值存储在变量中。您可以简单地将每个值存储在单独的变量中,然后在结束时进行计算,而不是使用输入数组(这是一种方法)

请参见下面的示例代码:

public static void main(String args[]) {
    // let's take incoming values from the user for our calculations
    Scanner keyboard = new Scanner(System.in);

    // we'll use this array to store the values entered by the user
    // in position zero of the array we'll store the OBP
    // in positions 1-5 we'll store the inputs as entered by the uesr
    float[] values = new float[6];

    // we'll use this flag to determin if we should ask the user input again or quit the program
    boolean letsDoThisAgain = true;

    while (letsDoThisAgain){
        System.out.println("Enter a Year: ");
        int year = keyboard.nextInt();

        System.out.println("For Year " + year + ", enter number of:");
        System.out.println("Hits: ");
        values[1] = keyboard.nextFloat();

        System.out.println("Walks: ");
        values[2] = keyboard.nextFloat();

        System.out.println("Number of times player has been hit by a pitch: ");
        values[3] = keyboard.nextFloat();

        System.out.println("Number of at bats: " );
        values[4] = keyboard.nextFloat();

        System.out.println("Number of sacrifice flies: ");
        values[5] = keyboard.nextFloat();

        // calculate the OBP
        values[0] = (values[1] + values[2] + values[3] - values[5] ) / values[4]; // or however you calculate it

        System.out.println("OBP: " + values[0]);

        System.out.println("------");
        System.out.println("Do you want to do it again? (y/n): ");
        String quitOrDoItAgain = keyboard.next();

        if( "n".equalsIgnoreCase(quitOrDoItAgain)){
            letsDoThisAgain = false;
        }
    }
    System.out.println("Thanks for playing... Good Bye!");
}

通过使用适当的数据结构,如
Map,包括在存储
之前对对象player stats进行OBP计算,但这是一个内存中的解决方案,仅持续到程序运行为止。如果您喜欢持久,请查看数据库端

此外,从您展示代码的方式来看,您似乎总是在年[index]上过度书写值

如果您只想使用一个数组,那么

years[index] = year[index] (math operation) keyboard.nextInt()

对于这个用例,我建议在HashMap中使用HashMap

循环前:

HashMap<Integer, HashMap<String, Integer>> years = new HashMap<>();
HashMap<String, Integer> entry = new HashMap<>();
最终你会得到如下结果:

{2019={hits=5, walks=10}}
检索结果也很简单:

// retrieve map of data for a specific year:
years.get(2019)

result: {hits=5, walks=10}

// retrieve specific data for a specific year:
years.get(2019).get("hits")

result: 5

非常感谢你的帮助。我应该在问题中加上这个,这是我的错。这个赋值是对数组的介绍,所以它必须通过数组使用,不幸的是。@Jusinr518啊,我明白了。。。当我回到我的办公室时,我会更新我的答案desk@Jusinr518我已经更新了我的答案,使用数组来存储输入,而不是单个变量。非常感谢您抽出一天的时间来帮助我解决我的问题。这太完美了,一定会指引我走向正确的方向。@Jusinr518我很高兴能帮上忙。别忘了给有用的答案投票:)当然,我个人会创建一个简单的类,例如User,每个数据类型都有单独的字段。
// retrieve map of data for a specific year:
years.get(2019)

result: {hits=5, walks=10}

// retrieve specific data for a specific year:
years.get(2019).get("hits")

result: 5