gpt4 book ai didi

Java:扫描仪 float 错误

转载 作者:行者123 更新时间:2023-12-01 23:57:11 26 4
gpt4 key购买 nike

我试图在 float 组中引入一组 float 字:

protected float[] a = new float [100];

public void setCoef(){

System.out.println("Introduceti coeficientii: ");
for (int i = 0; i <= this.grad; i++)
{
Scanner in = new Scanner(System.in);
this.a[i] = in.nextFloat();
}

}

但是当我输入 2.3 时,它会生成此异常:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextFloat(Scanner.java:2388)
at polinom.PolinomR.setCoef(PolinomR.java:35)
at polinom.PolinomReal.grade_coef(PolinomReal.java:14)
at polinom.Operatii.main(Operatii.java:43)

最佳答案

InputMismatchException :

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

您收到此异常是因为您尝试插入错误的值。

确保您插入的是 2.3,而不是 2,3 或数字以外的其他内容。

如果您输入2.3,您的程序应该可以正常运行

关于Java:扫描仪 float 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15396801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com