简体   繁体   中英

I'm receiving the wrong array values for this loop

For a homework problem I was asked to write a program that keeps track of the sales for different types of salsa. Printing out the Types of salsa and their amount sold, total, most and least sold salsa. I seem to have everything working except for some reason in my loop where I'm printing out values for the amount sold it only shows the first value.

import java.util.Scanner;
public class HW0604 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("How many types of salsa were sold: ");
        int x = input.nextInt();
        String salsa[] = salsa(x);
        int jars[] = (jars(salsa));
        int most = most(jars);
        int least = least(jars);
        int total = total(jars);
        print(salsa,jars,most,least,total);
    }
    public static String[] salsa(int x){
        Scanner input = new Scanner(System.in);
        String y[] = new String[x];
        for(int i=0;i<x;i++){
            int j=i+1;
        System.out.print("What is the name of Salsa number " + j +" : ");
        String salsa = input.nextLine();
            y[i]= salsa;
        }
        return y;
    }
    public static int[] jars(String x[]){
        Scanner input = new Scanner(System.in);
        int y[] = new int[x.length];
        for(int i=0;i<x.length;i++){
            System.out.print("How many jars of " + x[i] + " salsa were sold: "); 
            y[i] = input.nextInt();     
            }
        return y;
    }
    public static int total(int x[]){
        int y = 0;
        for(int i=0;i<x.length;i++){
            y = y + x[i];
        }
        return y;
    }
    public static int most(int x[]){
        int y = 0;
        int z = x[0];
        int j = 0;
        for(int i = 1;i<x.length;i++){
            if(x[i]>z){
                x[i]=z;
                y = j;
                j++;
            }   
        }
        return y;   
    }
    public static int least(int x[]){
        int y = 0;
        int z = x[0];
        int j = 0;
        for(int i = 1;i<x.length;i++){
            if(x[i]<z){
                x[i]=z;
                y = j;
                j++;
            }   
        }
        return y;   
    }
    public static void print(String salsa[],int jars[],int most,int least, int total){
        System.out.println("Salsa Sales Report");
        System.out.println();
        for(int i = 0; i<salsa.length;i++){
            System.out.println(salsa[i] + " sold: " + jars[i]);     
        }
        System.out.println();
        System.out.println("Total Jars sold: " + total);
        System.out.println("Most jars sold: " + salsa[most]);
        System.out.println("Least jars sold: " + salsa[least]);
    }
}

Can anyone help me determine what is wrong? I used the same method to input integers into an array and to print them before and it never had this problem is there anything conflicting with the jars[] array?

In most() and least() methods, you are modifying the contents of array passed in the argument and hence, it overwrites the original values. Below are the changes needed:

in most() method, change x[i]=z; to z=x[i];

and, in least() method, change x[i]=z; to z=x[i];

x[i]=z actually replaces all the values with minimum value in supplied array ( jars in our case) and that's why, print method shows same values.

In the methods most and least you need to do z=x[i]; instead of x[i]=z;

Instead of finding the least and most, you are actually replacing the values in the original array by doing x[i]=z .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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