Java Basic-2:if/switch/loops/exception/assertion

本文详细介绍了Java中的条件语句if和switch的使用方法,循环语句for、while、do-while的特点及应用,异常处理机制try-catch-finally的流程,并讨论了断言的基本用法。
Java Basic-2
关于if、switch
--------------
part 1: if
1.if(expression):括号是必须的。
2.expression必须是boolean型的变量或者是可以解析成boolean的表达式。
3.注意在if中“=”和“==”的区别。
4.parenthese:小括号,curly brace:大括号,indentation:缩进。
--------------
part 2:switch
1.switch(x),x:byte/short/int/char only.
2.case x:x必须是具体的值或者是常量。
3.记住还有break。
4.default:当没有东西匹配的时候,default发挥作用,default的位置可以随意放置,
  当没东西匹配的时候,default run,此时如果没有break,程序会从break的位置顺
  序执行,直到遇到break或switch结束。
--------------
part 3:loops
1.Java的循环有3种:for/while/do-while.
2.for(declaration/initialization;boolean;iteration).
3.for中的变量要么提前声明,要么在for中声明。
4.for中声明的变量,生命周期只在for中。
5.在for的第一部分,可以初始化多个变量,用“,"隔开。
6.do-while至少执行一次。
7.break跳出循环。
8.continue跳出本次,执行下一次。
9.return跳出函数。
--------------
part 4:exception
1.exception分为2种:checked & unchecked
2.try-catch-finally,throws
3.无论是否有exception抛出,无论exception是否被catch,fianlly都会执行。
4.当产生的exception关闭了JVM,fianlly就不会执行了。
5.call stack
6.可以继承Exception,来产生自己的异常类。
5.call stack
6.可以继承Exception,来产生自己的异常类。
7.当有多个catch时,顺序应从特定到一般,或者说从Exception的子类到父类,由下到上。
--------------
part 5:Assertion
1.在1.4之后,assert(x>1),assert is a keyword.
2.assertion在deployed时候是默认是关闭的。
3.java -ea/java -da
4.assertion可以针对class或package使用。
5.java -ea -da:MyClass TestClass.
6.在public函数里不要使用assertion。
7.Asserion具体什么时候用,到底有什么用,还不清楚。
lixing@lixing:~/fast_livo2$ catkin_make Base path: /home/lixing/fast_livo2 Source space: /home/lixing/fast_livo2/src Build space: /home/lixing/fast_livo2/build Devel space: /home/lixing/fast_livo2/devel Install space: /home/lixing/fast_livo2/install Creating symlink "/home/lixing/fast_livo2/src/CMakeLists.txt" pointing to "/opt/ros/noetic/share/catkin/cmake/toplevel.cmake" #### #### Running command: "cmake /home/lixing/fast_livo2/src -DCATKIN_DEVEL_PREFIX=/home/lixing/fast_livo2/devel -DCMAKE_INSTALL_PREFIX=/home/lixing/fast_livo2/install -G Unix Makefiles" in "/home/lixing/fast_livo2/build" #### -- The C compiler identification is GNU 9.4.0 -- The CXX compiler identification is GNU 9.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Using CATKIN_DEVEL_PREFIX: /home/lixing/fast_livo2/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/noetic -- This workspace overlays: /opt/ros/noetic -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3") -- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Found PY_em: /usr/lib/python3/dist-packages/em.py -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing() -- Using CATKIN_TEST_RESULTS_DIR: /home/lixing/fast_livo2/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under '/usr/src/googletest': gtests will be built -- Found gmock sources under '/usr/src/googletest': gmock will be built -- Found PythonInterp: /usr/bin/python3 (found version "3.8.10") -- Found Threads: TRUE -- Using Python nosetests: /usr/bin/nosetests3 -- catkin 0.8.12 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing 4 packages in topological order: -- ~~ - vikit_common -- ~~ - vikit_py -- ~~ - vikit_ros -- ~~ - fast_livo -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- +++ processing catkin package: 'vikit_common' -- ==> add_subdirectory(rpg_vikit/vikit_common) Current CPU archtecture: x86_64 -- Found OpenCV: /usr/local/opencv3.4 (found version "4.8.0") -- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") -- Checking for module 'eigen3' -- Found eigen3, version 3.4.0 -- Found Eigen: /usr/local/include/eigen3 -- Eigen found (include: /usr/local/include/eigen3) -- +++ processing catkin package: 'vikit_py' -- ==> add_subdirectory(rpg_vikit/vikit_py) -- +++ processing catkin package: 'vikit_ros' -- ==> add_subdirectory(rpg_vikit/vikit_ros) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy -- +++ processing catkin package: 'fast_livo' -- ==> add_subdirectory(fast-livo2-native) -- Build Type: Release -- Current CPU architecture: x86_64 -- Using general x86 optimizations: -O3 -march=native -mtune=native -funroll-loops -- Processor count: 8 -- Multithreading enabled. Cores: 4 -- OpenMP found -- mimalloc not found, proceeding without it -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy -- Could NOT find livox_ros_driver2 (missing: livox_ros_driver2_DIR) -- Could not find the required component 'livox_ros_driver2'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found. CMake Error at /opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package): Could not find a package configuration file provided by "livox_ros_driver2" with any of the following names: livox_ros_driver2Config.cmake livox_ros_driver2-config.cmake Add the installation prefix of "livox_ros_driver2" to CMAKE_PREFIX_PATH or set "livox_ros_driver2_DIR" to a directory containing one of the above files. If "livox_ros_driver2" provides a separate development package or SDK, be sure it has been installed. Call Stack (most recent call first): fast-livo2-native/CMakeLists.txt:83 (find_package) -- Configuring incomplete, errors occurred! See also "/home/lixing/fast_livo2/build/CMakeFiles/CMakeOutput.log". See also "/home/lixing/fast_livo2/build/CMakeFiles/CMakeError.log". Invoking "cmake" failed
最新发布
07-22
<think>我们正在处理一个ROS工作空间的构建问题。用户尝试使用`catkin_make`构建`fast_livo2`项目,但遇到了一个错误:找不到`livox_ros_driver2`包。 错误信息: ``` CMake Error at /opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package): Could not find a package configuration file provided by "livox_ros_driver2" with any of the following names: livox_ros_driver2Config.cmake livox_ros_driver2-config.cmake Add the installation prefix of "livox_ros_driver2" to CMAKE_PREFIX_PATH or set "livox_ros_driver2_DIR" to a directory containing one of the above files. If "livox_ros_driver2" provides a separate development package or SDK, be sure it has been installed. ``` 这个错误表明在构建过程中,CMake无法找到名为`livox_ros_driver2`的ROS包。`livox_ros_driver2`是Livox激光雷达设备的ROS驱动程序。 解决方案: 1. 确保已经安装了`livox_ros_driver2`包。如果没有安装,需要从源代码或通过包管理器安装。 2. 如果已经安装,确保环境变量设置正确,以便CMake能够找到它。 具体步骤: - 安装`livox_ros_driver2`: 通常,我们可以从GitHub仓库克隆并编译这个包。 例如: ``` cd ~/fast_livo2/src git clone https://github.com/Livox-SDK/livox_ros_driver2.git cd .. catkin_make ``` 注意:由于`fast_livo2`项目依赖这个包,所以需要将它放在工作空间的`src`目录下。 - 如果已经安装在其他位置,需要将`livox_ros_driver2`的安装路径添加到`CMAKE_PREFIX_PATH`环境变量中,或者在CMake命令中设置`livox_ros_driver2_DIR`变量指向包含配置文件的目录。 由于用户的工作空间路径是`/home/lixing/fast_livo2`,我们建议将`livox_ros_driver2`克隆到`src`目录下,然后重新运行`catkin_make`。 另外,注意错误信息中提到的`livox_ros_driver2_DIR`,如果包已经安装但不在标准路径,可以设置这个变量。例如: ``` cd ~/fast_livo2/build cmake -D livox_ros_driver2_DIR=/path/to/livox_ros_driver2/cmake .. ``` 但通常将包放在工作空间的`src`目录下是最简单的。 因此,回答用户的问题:构建失败是因为缺少`livox_ros_driver2`包,需要将它克隆到工作空间的`src`目录下再重新构建。 现在,根据要求,在回答后提出3-5个相关问题。</think>您的 `catkin_make` 构建失败,原因是 CMake 无法找到 `livox_ros_driver2` 依赖包。以下是关键分析和解决方案: ### 错误原因 1. **缺少依赖**:项目 `fast_livo` 需要 `livox_ros_driver2` 包(Livox 激光雷达的 ROS 驱动) 2. **路径未配置**:CMake 在以下路径中搜索失败: - `/opt/ros/noetic/share` - `CMAKE_PREFIX_PATH` 包含的其他路径 3. **配置中断**:因缺失关键组件,配置过程终止 ### 解决方案 #### 方法 1:安装依赖包 ```bash # 进入工作空间 src 目录 cd ~/fast_livo2/src # 克隆官方驱动仓库 git clone https://github.com/Livox-SDK/livox_ros_driver2.git # 返回工作空间根目录 cd .. # 重新构建 catkin_make ``` #### 方法 2:手动配置路径(若已安装) ```bash # 假设驱动安装在 /opt/livox_driver echo "export livox_ros_driver2_DIR=/opt/livox_driver" >> ~/.bashrc source ~/.bashrc catkin_make ``` #### 方法 3:检查依赖声明 检查 `fast-livo2-native/package.xml` 文件,确认包含: ```xml <depend>livox_ros_driver2</depend> ``` ### 验证步骤 1. 安装后检查驱动是否存在: ```bash rospack find livox_ros_driver2 ``` 2. 确认环境变量生效: ```bash echo $CMAKE_PREFIX_PATH | grep livox ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值