英文:
Dividing numbers between two different arrays in Java
问题
这是我的第一篇帖子。非常感谢您提前在这个主题上的理解和帮助。我正在尝试在两个不同的数组之间除法两个数字。我的数据是从一个.txt文件中提取的。我有名字、加仑和英里数。我试图计算每加仑的英里数(MPG)。因此,我需要将gallons[0]除以miles[0],gallons[1]除以miles[1],依此类推...并在cmd窗口的列表中显示这些值。以下是我目前的代码。请注意,在底部被注释掉的方法是我之前尝试的方法。如果之前的帖子中已经提到/回答了这个问题,我提前道歉。
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(int[] arr1In, int[] arr2In) {
double retValue = 0.0;
int a = 0;
int b = 0;
for (int i = 0; i < arr1In.length - 1; i++) {
return [i] = arr1In[i] / arr2In[i];
}
return a / b;
}//end mpg method
}//end class
这是mpg.txt文件的内容:
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
感谢您的时间。
-Rob
英文:
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. My data is being pulled from a .txt file. I'm given the name, gallons, and miles. I'm trying to calculate 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. 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. Sorry I just signed up 10 min ago and don't know how to navigate the site yet.
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
答案1
得分: 0
这是你的主要问题。你的 mpg
方法应该像这样。你需要将所有的平均值放入一个数组中,因此返回值需要是一个数组。
而且你需要将 int[]
数组中的一个转换为 double,这样在除法过程中就不会丢失小数部分。
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;
}
英文:
Here is your main problem. Your mpg
method should look like this. 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.
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;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论