Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编辑异常e以捕获字母而不是小数_Java_Arrays_Exception_Floating Point_Try Catch - Fatal编程技术网

Java 编辑异常e以捕获字母而不是小数

Java 编辑异常e以捕获字母而不是小数,java,arrays,exception,floating-point,try-catch,Java,Arrays,Exception,Floating Point,Try Catch,我正在写一个以10个浮点数作为输入的程序。然而,每当我输入小数点时,程序就会向我发送一个错误。我的问题是:如何编辑当前的try-catch异常以只捕获字母等,并允许输入小数(然后将它们存储到数组中)。另外,不管这个问题如何,我的程序也多次输出平均值,并且总是说它等于0 节目如下: import java.util.Scanner; 公共课平均分{ public static void main(String[] args) { new Average().average(new do

我正在写一个以10个浮点数作为输入的程序。然而,每当我输入小数点时,程序就会向我发送一个错误。我的问题是:如何编辑当前的try-catch异常以只捕获字母等,并允许输入小数(然后将它们存储到数组中)。另外,不管这个问题如何,我的程序也多次输出平均值,并且总是说它等于0

节目如下:

import java.util.Scanner;
公共课平均分{

public static void main(String[] args) {

    new Average().average(new double[10]);
}

public double average(double[] number) {
    Scanner scanner = new Scanner(System.in);

    int x = 0;
    double sum = 0;
    double[] numberList = new double[10]; //array to hold all numbers
    double[] largerList = new double[10]; //array to hold numbers greater than the average


    int numberIndex = 0;
    int largerIndex = 0;



    System.out.printf("Please enter 10 floating-point numberes.\nIf more than 10 values are entered, the numbers following 10 are ignored.\nIf less than 10 numbers are entered, the program will wait for you to enter 10.\n");

    for (int i = 0; i < 10; i++) {


        try { //try catch exception to catch decimal inputs as well as more /less than 10 integers
            x = scanner.nextInt();
            sum += numberList[x]; //add up all inputs to find sum
        } catch (Exception e) {
            System.out.println("Invalid input! Please reenter 10 integer values.");
            scanner = new Scanner(System.in);
            i = -1;
            numberIndex = 0;
            largerIndex = 0;
            numberList = new double[10];
            largerList = new double[10];
            continue;
        }
    }

    for (int i = 0; i < number.length; i++) {
        sum = sum + number[i];
        double average = sum / number.length;

        //return average;

        if (x > average) {
            largerList[largerIndex] = x; //add negative input to negativeList array
            largerIndex = largerIndex + 1;

        }
        System.out.println("Average value of your input is: " + average);
        System.out.println();

    }



    for (int i = 0; i < largerIndex; i++) {
        System.out.println(largerList[i]);
    }
    return 0;





}
publicstaticvoidmain(字符串[]args){
新均线()平均线(新双[10]);
}
公众双倍平均(双倍[]数字){
扫描仪=新的扫描仪(System.in);
int x=0;
双和=0;
double[]numberList=new double[10];//用于保存所有数字的数组
double[]largerList=new double[10];//用于保存大于平均值的数字的数组
整数指数=0;
int largerIndex=0;
System.out.printf(“请输入10个浮点数。\n如果输入的值超过10,则忽略10后面的数字。\n如果输入的数字少于10,程序将等待您输入10。\n”);
对于(int i=0;i<10;i++){
try{//try catch exception捕获十进制输入以及大于/小于10个整数
x=scanner.nextInt();
sum+=numberList[x];//将所有输入相加以找到sum
}捕获(例外e){
System.out.println(“输入无效!请重新输入10个整数值”);
扫描仪=新扫描仪(System.in);
i=-1;
numberIndex=0;
大指数=0;
numberList=新的双精度[10];
largerList=新的双精度[10];
继续;
}
}
for(int i=0;i平均值){
largerList[largerIndex]=x;//将负输入添加到negativeList数组
largerIndex=largerIndex+1;
}
System.out.println(“输入的平均值为:“+Average”);
System.out.println();
}
对于(int i=0;i

}

您正在使用nextInt()函数,该函数只返回整数。整数不能包含小数。请参考API并查看nextFloat()和nextDouble()方法。

对于您的平均问题,您的print语句进入For循环,因此将执行
number.length
次。将打印语句移出循环。您还需要将平均变量的声明置于循环之外。另外,您应该只需要循环来计算和,而不需要每次都计算平均值

double average;
for(/*loop conditions*/)
{
    sum = sum + number[i];
}
average = sum / number.length;
System.out.println("Average value of your input is: " + average);
System.out.println();

您不指定捕获哪些值,只指定异常的类型。如果您只希望在某些情况下抛出(或不抛出)异常,则必须根据需要在抛出异常之前安排代码进行预测试。