Try-Catch java中的正数

时间:2017-01-14 11:39:39

标签: java numbers try-catch

即时尝试此代码,但它无法正常工作,代码应该再次询问您的号码,然后再输入正数。抱歉我的英文不好,tnx!

System.out.print("Precio: ");
precio[contPre] = Double.parseDouble(teclado.readLine());

try {
    double precioCatch = Double.parseDouble(String.valueOf(precio[contPre]));
    if (precioCatch < 0) 
        throw new Exception();
}catch (Exception e) {
    System.out.println("Number must be >0");
    continue;
}

1 个答案:

答案 0 :(得分:0)

谢谢你的答案,我找到了解决方案并且有效:

precio[contPre] = Double.parseDouble(teclado.readLine());
if (0 > precio[contPre]) {
    while (0 > precio[contPre]) {
        System.out.print("Number must be a positive: ");
        precio[contPre] = Double.parseDouble(teclado.readLine());
    }
}