gpt4 book ai didi

java - 尝试计算在总 (14) 餐中吃鱼/鸡/肉的百分比并显示出来。它用全 0 填充数组

转载 作者:行者123 更新时间:2023-12-01 17:30:34 25 4
gpt4 key购买 nike

计算出数组中的人吃(鱼、鸡、肉)的次数后,我需要计算他们吃该选择的次数的百分比并显示它。当我在“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com