作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
计算出数组中的人吃(鱼、鸡、肉)的次数后,我需要计算他们吃该选择的次数的百分比并显示它。当我在“fillArray”方法中尝试时,结果全部填充为 0。我做错了什么/如何纠正它?
import java.util.Scanner;
import java.io.*;
public class SortArrayHomework{
public static void main(String[] args) throws IOException {
int[] fish = new int[10];
int[] chicken = new int[10];
int[] meat = new int[10];
String[] names = new String[10];
File fn = new File("foodtracking.txt");
Scanner inputFile = new Scanner(fn);
int mp = 0;
fillArray(inputFile,names,fish,chicken,meat,mp);
sortArraynames(names,fish,chicken,meat,mp);
displayArray(names,fish,chicken,meat,mp);
} // end main
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public static void sortArraynames(String[] n,int[] f, int[] c, int[] m,int mp){
String temp;
int stemp;
int stemp1;
int stemp2;
for(int indx = 0; indx<n.length; indx++) //current low position
for(int indx1=indx+1; indx1<n.length; indx1++) // current position in the array I am comparing to
if(n[indx].compareToIgnoreCase(n[indx1]) > 0){
temp = n[indx]; //swp the two values
n[indx] = n[indx1];
n[indx1] = temp;
stemp = f[indx]; //swp the two values
f[indx] = f[indx1];
f[indx1] = stemp;
stemp1 = c[indx]; //swp the two values
c[indx] = c[indx1];
c[indx1] = stemp1;
stemp2 = m[indx]; //swp the two values
m[indx] = m[indx1];
m[indx1] = stemp2;}
}//end sortArray method
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public static void fillArray(Scanner fileIn,String[] names, int[] fish, int[] chicken, int[] meat, int mp){
int total;
for(int indx = 0; indx < names.length; indx++){
names[indx] = fileIn.nextLine();
if(names[indx].length() == 0)names[indx] = fileIn.nextLine();
fish[indx] = fileIn.nextInt();
chicken[indx] = fileIn.nextInt();
meat[indx] = fileIn.nextInt();
total = ((fish[indx] + chicken[indx]) + meat[indx]);
mp = (meat[indx] / total)*100;}
return;
}//end fillArray method
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//n is the name array
//f is the fish array
//c is the chicken array
//m is the meat array
public static void displayArray(String[] n,int[] f, int[] c, int[] m,int mp){
System.out.printf("%18s %4s %7s %4s %10s %10s %10s\n"," Name ","Fish","Chicken","Meat","% Fish","% Chicken","% Meat");
System.out.printf("%18s %4s %7s %4s %10s %10s %10s\n","--------------","-------","-------","----","------","---------","------");
for(int indx = 0; indx < n.length; indx++) System.out.printf("%-18s %-2d %-2d %-2d %-2d\n",n[indx],f[indx],c[indx],m[indx],mp);
return;
}//end displayArray method
} // end class
最佳答案
int
不是按引用传递,而是按值传递。
import java.util.Scanner;
import java.io.*;
public class SortArrayHomework{
public static void main(String[] args) throws IOException {
int[] fish = new int[10];
int[] chicken = new int[10];
int[] meat = new int[10];
String[] names = new String[10];
File fn = new File("foodtracking.txt");
Scanner inputFile = new Scanner(fn);
int[] mp = new int[10];
fillArray(inputFile,names,fish,chicken,meat,mp);
sortArraynames(names,fish,chicken,meat);
displayArray(names,fish,chicken,meat,mp);
} // end main
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public static void sortArraynames(String[] n, int[] f, int[] c, int[] m){
String temp;
int stemp;
int stemp1;
int stemp2;
for(int indx = 0; indx<n.length; indx++) //current low position
for(int indx1=indx+1; indx1<n.length; indx1++) // current position in the array I am comparing to
if(n[indx].compareToIgnoreCase(n[indx1]) > 0){
temp = n[indx]; //swp the two values
n[indx] = n[indx1];
n[indx1] = temp;
stemp = f[indx]; //swp the two values
f[indx] = f[indx1];
f[indx1] = stemp;
stemp1 = c[indx]; //swp the two values
c[indx] = c[indx1];
c[indx1] = stemp1;
stemp2 = m[indx]; //swp the two values
m[indx] = m[indx1];
m[indx1] = stemp2;}
}//end sortArray method
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public static void fillArray(Scanner fileIn,String[] names, int[] fish, int[] chicken, int[] meat, int[] mp){
int total;
for(int indx = 0; indx < names.length; indx++){
names[indx] = fileIn.nextLine();
if(names[indx].length() == 0)names[indx] = fileIn.nextLine();
fish[indx] = fileIn.nextInt();
chicken[indx] = fileIn.nextInt();
meat[indx] = fileIn.nextInt();
total = ((fish[indx] + chicken[indx]) + meat[indx]);
mp[indx] = (int) (((double) meat[indx] / total) * 100d);
}
return;
}//end fillArray method
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//n is the name array
//f is the fish array
//c is the chicken array
//m is the meat array
public static void displayArray(String[] n,int[] f, int[] c, int[] m,int[] mp){
System.out.printf("%18s %4s %7s %4s %10s %10s %10s\n"," Name ","Fish","Chicken","Meat","% Fish","% Chicken","% Meat");
System.out.printf("%18s %4s %7s %4s %10s %10s %10s\n","--------------","-------","-------","----","------","---------","------");
for(int indx = 0; indx < n.length; indx++) System.out.printf("%-18s %-2d %-2d %-2d %-2d\n",n[indx],f[indx],c[indx],m[indx],mp[indx]);
return;
}//end displayArray method
} // end class
关于java - 尝试计算在总 (14) 餐中吃鱼/鸡/肉的百分比并显示出来。它用全 0 填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61129131/
我是一名优秀的程序员,十分优秀!