gpt4 book ai didi

Java Try & Catch 示例

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

所以我这里有这段代码,但我不明白为什么我的 Try 语句带有下划线。当我将鼠标悬停在它上面时,它说 b3 已经在方法 main 中定义了。我需要做什么才能使它按照显示的方式工作。 (其他一切都有效)

public class ReviewTest {
public static void main(String[] args)
{
boxes b1 = new boxes();
System.out.printf("My first Box = %s\n", b1.toString());
System.out.printf("My first Box = %s\n", b1);

boxes b2 = new boxes(10.0, 12.4, 13.9);
System.out.printf("My second Box = %s\n" , b2);

System.out.printf("The volumne of b2 = %.3f\n", b2.volume());
System.out.printf("The surface Area of b2 = %.3f\n", b2.surfaceArea());

boxes b3 = null;

try {
boxes b3 = new boxes(-1,4.3, 12);
}
catch (IllegalArgumentException ex)
{
System.out.println(ex.getMessage());
}
}

最佳答案

这是因为您已经定义了b3。要解决此问题,您需要执行以下操作:

就在 try/catch block 之前,您定义了框 b3 = null;。您需要做的是进入 try block 内部,并删除 b3 = newboxes(-1,4.3, 12);< 之前的 boxes 部分.

关于Java Try & Catch 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29221380/

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