簡體   English   中英

我收到此循環的錯誤數組值

[英]I'm receiving the wrong array values for this loop

對於一項家庭作業問題,我被要求編寫一個程序來跟蹤不同類型的莎莎醬的銷售額。 打印出莎莎的類型及其售出的數量,總銷量,售出數量最多和售出最少的莎莎。 我似乎可以正常工作,但出於某種原因,在我的循環中,出於打印售出金額的值,它僅顯示第一個值。

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]);
    }
}

誰能幫我確定出什么問題了? 我使用相同的方法將整數輸入到數組中並在以前打印它們,但從來沒有這個問題與jars []數組有沖突嗎?

most()least()方法中,您正在修改在參數中傳遞的數組的內容,因此,它將覆蓋原始值。 以下是所需的更改:

most()方法中,更改x[i]=z; z=x[i];

並用least()方法更改x[i]=z; z=x[i];

x[i]=z實際上將所有值替換為所提供數組中的最小值(在我們的示例中為jars ),這就是為什么print方法顯示相同值。

most方法中,您least需要執行z=x[i]; 而不是x[i]=z;

實際上,您不是在尋找最小和最大的對象,而是通過x[i]=z替換原始數組中的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM