永发信息网

请问下我这个JAVA程序是哪里出错了

答案:6  悬赏:70  手机版
解决时间 2021-07-30 10:32
  • 提问者网友:暗中人
  • 2021-07-29 19:10

//对象

package shangjizuoye;

import java.util.*;

public class zuoye_3 {
String[] thing = new String[2];
double[] price = new double[2];
String custNo;
int num;

Scanner input = new Scanner(System.in);
public void sellstore(){
thing[0] = "1.addidas运动鞋";
price[0] = 420.0;
thing[1] = "2.addidasT恤";
price[1] = 500.0;
thing[2] = "3.Nike运动鞋";
price[2] = 1000.0;
System.out.println(thing[0]+"\t"+price[0]);
System.out.println(thing[1]+"\t"+price[1]);
System.out.println(thing[2]+"\t"+price[2]);
}





public void display(){
System.out.println("我行我素购物管理系统 > 购物结算");
System.out.println("\n\n\n");
System.out.println("******************************************************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("\n\n\n");
System.out.println("请输入会员号");
custNo = input.next();
String answer = "";
double total=0;
do{
for(int i = 0;i<=2;i++){

System.out.println("请输入商品编号:");
thing[i] = input.next();
System.out.println("请输入购买数量:");
num = input.nextInt();
total = num*price[i];
System.out.println(thing[i]+"\t"+"¥"+price[i]+"\t"+"¥"+total);
System.out.println("\n");

}
System.out.println("是否继续(y/n):");
answer = input.next();
}while(answer.equals("y"));
double sale = 0.8;
System.out.println("折扣:"+sale);
total = total*0.8;
System.out.println("金额总计:¥"+total);
int finalgive = 400;
System.out.println("实际交费:¥"+finalgive);
double backmoney = 0;
backmoney = finalgive - total;
System.out.println("找钱:¥"+backmoney);
int score = 0;
score = (int)total/100;
System.out.println("本次购物所获得的积分是:"+score);




}

}

测试类:

package shangjizuoye;

public class Testzuoye_3 {

public static void main(String[] args) {
zuoye_3 t = new zuoye_3();
t.sellstore();
t.display();

}

}

麻烦来个高手帮下忙,谢谢了!

最佳答案
  • 五星知识达人网友:时间的尘埃
  • 2021-07-29 19:15

你好,很高兴为你回答,请看如下解释↓↓↓↓↓


========================================================================


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at JavaTest.zuoye_3.sellstore(Testzuoye_3.java:17)
at JavaTest.Testzuoye_3.main(Testzuoye_3.java:94)


这是你的java程序的报错信息。


是数组越界了。。你在定义String和double两个数组的时候数组长度是2,后面使用过程中却使用了三个数组元素,所以越界了,修改很简单的,看我的代码


String[] thing = new String[3];
double[] price = new double[3];


只要把这两行代码的数组大小设为3即可。。


======================================================================


祝你愉快

全部回答
  • 1楼网友:玩世
  • 2021-07-30 00:32

数组越界.....

楼主要注意下类名哦... 不能小写的

  • 2楼网友:风格不统一
  • 2021-07-29 23:01

数组越界,控制台有错误提示,去看看错误提示,多看看就懂了

  • 3楼网友:逐風
  • 2021-07-29 21:32
数组越界 定义为2的数组去插入3个 String[] thing = new String[2]; double[] price = new double[2]; thing[0] = "1.addidas运动鞋"; price[0] = 420.0; thing[1] = "2.addidasT恤"; price[1] = 500.0; thing[2] = "3.Nike运动鞋"; price[2] = 1000.0;
  • 4楼网友:痴妹与他
  • 2021-07-29 21:27

数组越界

上面的数组定义的大小是2

所以你赋值的时候最大下标应该只能到1

  • 5楼网友:一把行者刀
  • 2021-07-29 20:52

import java.util.*;

class zuoye_3 { String[] thing = new String[3]; double[] price = new double[3]; String custNo; int num;

Scanner input = new Scanner(System.in); public void sellstore(){ thing[0] = "1.addidas运动鞋"; price[0] = 420.0; thing[1] = "2.addidasT恤"; price[1] = 500.0; thing[2] = "3.Nike运动鞋"; price[2] = 1000.0; System.out.println(thing[0]+"\t"+price[0]); System.out.println(thing[1]+"\t"+price[1]); System.out.println(thing[2]+"\t"+price[2]); }

public void display(){ System.out.println("我行我素购物管理系统 > 购物结算"); System.out.println("\n\n\n"); System.out.println("******************************************************************************"); System.out.println("请选择购买的商品编号:"); System.out.println("\n\n\n"); System.out.println("请输入会员号"); custNo = input.next(); String answer = ""; double total=0; do{ for(int i = 0;i<=2;i++){

System.out.println("请输入商品编号:"); thing[i] = input.next(); System.out.println("请输入购买数量:"); num = input.nextInt(); total = num*price[i]; System.out.println(thing[i]+"\t"+"¥"+price[i]+"\t"+"¥"+total); System.out.println("\n");

} System.out.println("是否继续(y/n):"); answer = input.next(); }while(answer.equals("y")); double sale = 0.8; System.out.println("折扣:"+sale); total = total*0.8; System.out.println("金额总计:¥"+total); int finalgive = 400; System.out.println("实际交费:¥"+finalgive); double backmoney = 0; backmoney = finalgive - total; System.out.println("找钱:¥"+backmoney); int score = 0; score = (int)total/100; System.out.println("本次购物所获得的积分是:"+score);

}

}

public class Testzuoye_3 {

public static void main(String[] args) { zuoye_3 t = new zuoye_3(); t.sellstore(); t.display();

}

}

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯