使用椭圆或省略号出错了

时间:2016-04-15 09:05:47

标签: java indexoutofboundsexception

我正在创建一个简单的程序,它可以获取姓名,年龄和喜欢的数字。问题是当用户选择输入多于1个收藏号码时会出现此异常。

exception

请帮我解决一下在测试类中仍然使用椭圆的问题 - > favnum2方法。*

测试类

import java.util.Scanner;
public class testing{
public static Scanner input;


public static void main(String[] args){

    boolean choicerepeat=true;
    int favnumoftimes;

    while(choicerepeat==true){
        input = new Scanner(System.in);
        testing2 obj1 = new testing2();
        String name="";
        int age=0;
        favnumoftimes=0;
        double favnum=0, favnumarr[]=new double[999];
        boolean choice1;

        System.out.print("What is your name? ");
        name = input.nextLine();
        System.out.print("What is your age? ");
        age = input.nextInt();

        obj1.message1(name);
        obj1.message2(age);

        System.out.print(name+" do you only have one favorite number? (If yes type 'true' else 'false' - NOTE: lowercase only) ");
        choice1 = input.nextBoolean();

        if(choice1==true)
            favnum1();
        else{
            System.out.println("How many favorite numbers do you have "+name+"? ");
            favnumoftimes = input.nextInt();
            for(int a=0;a<favnumoftimes;a++){
                System.out.print("Enter favorite number "+ (a+1) +": ");
                favnumarr[a]=input.nextDouble();
            }

            for(int a=0;a<favnumarr.length;a++){
                favnum2(favnumoftimes, favnumarr[a]);
            }
        }

        System.out.println();
        System.out.println("Do you want to restart the program? (true(Yes) else false(No)) ");
        choicerepeat = input.nextBoolean();

    }
}

public static void favnum1(){
    System.out.print("Enter favorite number: ");
    double favnumholder1 = input.nextDouble();
    System.out.println("Your favorite number is "+favnumholder1+" ." );
}

public static double favnum2(int favnumoftimesholder,double...favtemphold2){
    System.out.print("Your favorite numbers are ");
    for(int a=0;a<=favnumoftimesholder;a++){
        System.out.print(favtemphold2[a]+", ");
    }
    return 0;
}
}

testing2 class

public class testing2{
public static String message1(String nameholder){
    for(int a=0;a<nameholder.length();a++){
        char strholder = nameholder.charAt(a);
        if(Character.isDigit(a)){
            System.out.println("Names don't have numbers... ");
            break;
        }
        else continue;
    }
    System.out.println("\nHi "+nameholder+"! Welcome to my simple program. ");
    return nameholder;
}

public static int message2(int ageholder){
    System.out.println("Your age is "+ageholder+" years old? Oh my goodness. ");
    System.out.println();
    return ageholder;
}
}

1 个答案:

答案 0 :(得分:1)

问题是varargs创建的新数组的长度等于传递的参数数量。因此double...favtemphold2将创建一个新数组favtemphold2,因为您只传递1个元素(favnum2(favnumoftimes, favnumarr[a]);),该数组的长度为1。

您可能想要传递更多元素或整个数组,即favnum2(favnumoftimes, favnumarr);。由于double...基本上是double[]的语法糖,所以它们是相等的,并且为双vararg传递一个双数组将起作用。

警告以后使用varargs:小心Object...,因为数组也是对象。