JAVA数组和方法调用问题
packagejava_test;importjava.util.*;publicclasstest3{int[]sl={0,0,0,0,0,0,0};String[]n...
package java_test;
import java.util.*;
public class test3 {
int[] sl={0,0,0,0,0,0,0};
String[] name={"商品1","商品2","商品3","商品4","商品5","商品6","商品7"};
public void shopping()
{
int number=10;
Scanner input=new Scanner(System.in);
System.out.println("* * * * * * * * * * * * * * * * * * * *");
System.out.println("\t商品编号\t商品名称\t价格\t数量");
System.out.println("\t1\t商品1\t200\t"+number);
System.out.println("\t2\t商品2\t400\t"+number);
System.out.println("\t3\t商品3\t1200\t"+number);
System.out.println("\t4\t商品4\t600\t"+number);
System.out.println("\t5\t商品5\t300\t"+number);
System.out.println("\t6\t商品6\t2800\t"+number);
System.out.println("\t7\t商品7\t3000\t"+number);
System.out.println("* * * * * * * * * * * * * * * * * * * *");
System.out.println("输入购买的商品编号:");
int bh=input.nextInt();
System.out.println("输入购买的商品数量:");
int spsl=input.nextInt();
sl[bh-1]=spsl;
System.out.println("已加入购物车!可进入购物车界面更改或付款!");
}
public void buy()
{
System.out.println("* * * * * * * * * * * * * * * * * * * *");
System.out.println("\t\t【购物清单】");
for(int i=0;i<sl.length;i++)
{
System.out.println("\t"+name[i]+"\t"+sl[i]);
}
}
}
java问题,我写了两个方法,在main方法中调用shopping方法,输入购物编号和购物数量后,再调用buy方法,显示的购物清单为什么数量全是0,我输入购物数量后不是应该储存到类的数组里了吗?循环这个数组出来全是0? 展开
import java.util.*;
public class test3 {
int[] sl={0,0,0,0,0,0,0};
String[] name={"商品1","商品2","商品3","商品4","商品5","商品6","商品7"};
public void shopping()
{
int number=10;
Scanner input=new Scanner(System.in);
System.out.println("* * * * * * * * * * * * * * * * * * * *");
System.out.println("\t商品编号\t商品名称\t价格\t数量");
System.out.println("\t1\t商品1\t200\t"+number);
System.out.println("\t2\t商品2\t400\t"+number);
System.out.println("\t3\t商品3\t1200\t"+number);
System.out.println("\t4\t商品4\t600\t"+number);
System.out.println("\t5\t商品5\t300\t"+number);
System.out.println("\t6\t商品6\t2800\t"+number);
System.out.println("\t7\t商品7\t3000\t"+number);
System.out.println("* * * * * * * * * * * * * * * * * * * *");
System.out.println("输入购买的商品编号:");
int bh=input.nextInt();
System.out.println("输入购买的商品数量:");
int spsl=input.nextInt();
sl[bh-1]=spsl;
System.out.println("已加入购物车!可进入购物车界面更改或付款!");
}
public void buy()
{
System.out.println("* * * * * * * * * * * * * * * * * * * *");
System.out.println("\t\t【购物清单】");
for(int i=0;i<sl.length;i++)
{
System.out.println("\t"+name[i]+"\t"+sl[i]);
}
}
}
java问题,我写了两个方法,在main方法中调用shopping方法,输入购物编号和购物数量后,再调用buy方法,显示的购物清单为什么数量全是0,我输入购物数量后不是应该储存到类的数组里了吗?循环这个数组出来全是0? 展开
展开全部
//程序没问题啊
import java.util.*;
public class test3 {
int[] sl={0,0,0,0,0,0,0};
String[] name={"商品1","商品2","商品3","商品4","商品5","商品6","商品7"};
public void shopping()
{
int number=10;
Scanner input=new Scanner(System.in);
System.out.println("* * * * * * * * * * * * * * * * * * * *");
System.out.println("\t商品编号\t商品名称\t价格\t数量");
System.out.println("\t1\t\t商品1\t\t200\t"+number);
System.out.println("\t2\t\t商品2\t\t400\t"+number);
System.out.println("\t3\t\t商品3\t\t1200\t"+number);
System.out.println("\t4\t\t商品4\t\t600\t"+number);
System.out.println("\t5\t\t商品5\t\t300\t"+number);
System.out.println("\t6\t\t商品6\t\t2800\t"+number);
System.out.println("\t7\t\t商品7\t\t3000\t"+number);
System.out.println("* * * * * * * * * * * * * * * * * * * *");
System.out.println("输入购买的商品编号:");
int bh=input.nextInt();
System.out.println("输入购买的商品数量:");
int spsl=input.nextInt();
sl[bh-1]=spsl;
System.out.println("已加入购物车!可进入购物车界面更改或付款!");
input.close();
}
public void buy()
{
System.out.println("* * * * * * * * * * * * * * * * * * * *");
System.out.println("\t\t【购物清单】");
for(int i=0;i<sl.length;i++)
{
System.out.println("\t"+name[i]+"\t"+sl[i]);
}
}
public static void main(String[] args) {
test3 t = new test3();
t.shopping();
System.out.println(Arrays.toString(t.sl));
t.buy();
}
}
追问
谢谢,刚刚那个问题解决了,我写了四个类,你导入一下看看可以吗?test3类里有一个query方法,为什么我存储数据进数组后输出的全是null,没有显示我输入的数据?
追答
代码哪?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询