[英]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?
我使用相同的方法将整数输入到数组中并在以前打印它们,但从来没有这个问题与jars []数组有冲突吗?
In most()
and least()
methods, you are modifying the contents of array passed in the argument and hence, it overwrites the original values. 在
most()
和least()
方法中,您正在修改在参数中传递的数组的内容,因此,它将覆盖原始值。 Below are the changes needed: 以下是所需的更改:
in most()
method, change x[i]=z;
在
most()
方法中,更改x[i]=z;
to z=x[i];
到
z=x[i];
and, in least()
method, change x[i]=z;
并用
least()
方法更改x[i]=z;
to z=x[i];
到
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. x[i]=z
实际上将所有值替换为所提供数组中的最小值(在我们的示例中为jars
),这就是为什么print方法显示相同值。
In the methods most
and least
you need to do z=x[i];
在
most
方法中,您least
需要执行z=x[i];
instead of x[i]=z;
而不是
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
. 实际上,您不是在寻找最小和最大的对象,而是通过
x[i]=z
替换原始数组中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.