简体   繁体   English

在 Java 中的两个不同 arrays 之间除数

[英]Dividing numbers between two different arrays in Java

This is my first post.这是我的第一篇文章。 Thank you in advance for your understanding and help on this topic.预先感谢您对此主题的理解和帮助。 I am trying to divide two numbers between two different arrays.我试图在两个不同的 arrays 之间划分两个数字。 My data is being pulled from a.txt file.我的数据是从 a.txt 文件中提取的。 I'm given the name, gallons, and miles.我得到了名字、加仑和英里。 I'm trying to calculate MPG.我正在尝试计算 MPG。 So I would have to divide gallons[0] by miles[0], gallons[1] by miles[1],etc.... and display this in the list in the cmd window.所以我必须将加仑[0]除以英里[0],加仑[1]除以英里[1]等......并将其显示在cmd window的列表中。 Below is the code that I have so far.下面是我到目前为止的代码。 Please note that the pseudo'd out method at the bottom was what I was trying before I tried the method above it.请注意,底部的伪输出方法是我在尝试上面的方法之前尝试的方法。 I apologize in advance if this has been addressed/answered in a previous thread.如果在之前的帖子中已经解决/回答了这个问题,我提前道歉。

import java.util.Scanner;
import java.io.*;
public class mpgHOmework2{
    public static void main(String[]args) throws IOException{

    File fn = new File("mpg.txt");
    Scanner inputFile = new Scanner(fn);

    Scanner keyboard = new Scanner(System.in);

    String[] names = new String[10];
    int[] miles = new int[10];
    int[] gallons = new int[10];
    double mpg;

    fillArray(inputFile,names,miles,gallons);

    displayArray(names,miles,gallons,mpg(miles,gallons));

    }//end main method
//==================================================================
    public static void fillArray(Scanner input,String[] arrIn, int[] arr1In, int[] arr2In){

        for(int indx = 0; indx < arrIn.length; indx++){
            arrIn[indx] = input.nextLine();
            if(arrIn[indx].length() == 0)arrIn[indx] = input.nextLine();
            arr1In[indx] = input.nextInt();
            arr2In[indx] = input.nextInt();}


    }//end fillArray method
//==================================================================
    public static void displayArray(String[] arrIn,int[] arr1In, int[]arr2In, double mpg){

        System.out.printf("%-18s   %5s    %7s   %3s\n","Names","Miles","Gallons","MPG");
        System.out.printf("%-18s   %5s    %7s   %3s\n","==================","=====","=======","===");
        for(int indx = 0; indx < arrIn.length; indx++)
            System.out.printf("%-18s   %5d   %7d  %.2f\n",arrIn[indx],arr1In[indx],arr2In[indx],mpg);

    }//end displayArray method
//==================================================================
    public static double mpg(Scanner input,String[] arrIn, int[] arr1In, int[] arr2In){

        for(int indx = 0; indx < arrIn.length; indx++){
            arrIn[indx] = input.nextLine();
            if(arrIn[indx].length() == 0)arrIn[indx] = input.nextLine();
            arr1In[indx] = input.nextInt();
            arr2In[indx] = input.nextInt();
        return = arr1In[indx]/arr2In[indx];}
}enter code here
/*  public static double mpg(int[] arrOneIn, int[] arrTwoIn){

        double retValue = 0.0;
            int a = 0;
            int b = 0;
            for (int i = 0; i < arrOneIn.length-1; i++){
                return [i] = arrOneIn[i] / arrTwoIn[i];
                }

            return a/b;
    }//end mpg method*/
}//end class

This is the mpg.txt file.这是 mpg.txt 文件。 Sorry I just signed up 10 min ago and don't know how to navigate the site yet.抱歉,我 10 分钟前才注册,还不知道如何浏览网站。

John Murphy
1200  60
Katherine Cleary
1500 63
Isaac Fajerman
2600 87
Marsha Smiith
3500 100
Jason Meier
4100 196
Inna Johnson
750 34
Walter Philips
6900 265
Tracey Cannon
5024 187
Sheryl Rothman
6800 323
Jeff Gottlieb
450 18

Thanks for your time.谢谢你的时间。

-Rob -抢

Here is your main problem.这是你的主要问题。 Your mpg method should look like this.您的mpg方法应如下所示。 You need to return all the averages in an array so the return values need to be an array.您需要返回一个数组中的所有平均值,因此返回值需要是一个数组。

And you need to cast one of the int[] arrays to double so you don't drop the fractions in the division process.并且您需要将int[] arrays 之一转换为加倍,这样您就不会在除法过程中丢弃分数。


    public static double[] mpg(int[] arrOneIn, int[] arrTwoIn) {

        double[] retValues = new double[arrOneIn.length];
        for (int i = 0; i < arrOneIn.length; i++) {
            retValues[i] = (double) arrOneIn[i] / arrTwoIn[i];
        }
        return retValues;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM