Trying to calculate percent times fish/chicken/meat were eaten out of total (14) meals and display it. It populates the array with all 0's

huangapple 未分类评论45阅读模式
英文:

Trying to calculate percent times fish/chicken/meat were eaten out of total (14) meals and display it. It populates the array with all 0's

问题

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

    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
英文:

After calculating the number of times the people in the array ate (Fish, Chicken, Meat) I then need to calculate the percentage of times they ate that selection and display it. When I attempt it in the 'fillArray' method the results are all populating as 0. What am I doing wrong/how can I correct it?

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(&quot;foodtracking.txt&quot;);
          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&lt;n.length; indx++)			//current low position
		for(int indx1=indx+1; indx1&lt;n.length; indx1++)	// current position in the array I am comparing to
			if(n[indx].compareToIgnoreCase(n[indx1]) &gt; 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 &lt; 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(&quot;%18s  %4s      %7s  %4s %10s  %10s  %10s\n&quot;,&quot;     Name     &quot;,&quot;Fish&quot;,&quot;Chicken&quot;,&quot;Meat&quot;,&quot;% Fish&quot;,&quot;% Chicken&quot;,&quot;% Meat&quot;);
    System.out.printf(&quot;%18s  %4s   %7s  %4s %10s  %10s  %10s\n&quot;,&quot;--------------&quot;,&quot;-------&quot;,&quot;-------&quot;,&quot;----&quot;,&quot;------&quot;,&quot;---------&quot;,&quot;------&quot;);

	for(int indx = 0; indx &lt; n.length; indx++) System.out.printf(&quot;%-18s    %-2d         %-2d    %-2d  %-2d\n&quot;,n[indx],f[indx],c[indx],m[indx],mp);


	return;



}//end displayArray method

} // end class

答案1

得分: 0

`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
英文:

int is not pass by reference it is pass by value.

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(&quot;foodtracking.txt&quot;);
        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&lt;n.length; indx++)            //current low position
            for(int indx1=indx+1; indx1&lt;n.length; indx1++)  // current position in the array I am comparing to
                if(n[indx].compareToIgnoreCase(n[indx1]) &gt; 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 &lt; 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(&quot;%18s  %4s      %7s  %4s %10s  %10s  %10s\n&quot;,&quot;     Name     &quot;,&quot;Fish&quot;,&quot;Chicken&quot;,&quot;Meat&quot;,&quot;% Fish&quot;,&quot;% Chicken&quot;,&quot;% Meat&quot;);
        System.out.printf(&quot;%18s  %4s   %7s  %4s %10s  %10s  %10s\n&quot;,&quot;--------------&quot;,&quot;-------&quot;,&quot;-------&quot;,&quot;----&quot;,&quot;------&quot;,&quot;---------&quot;,&quot;------&quot;);

        for(int indx = 0; indx &lt; n.length; indx++) System.out.printf(&quot;%-18s    %-2d         %-2d    %-2d  %-2d\n&quot;,n[indx],f[indx],c[indx],m[indx],mp[indx]);


        return;



    }//end displayArray method

} // end class

huangapple
  • 本文由 发表于 2020年4月10日 03:49:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/61129131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定