Introduction to Java Programming编程题12.3<ArrayIndexOutBoundsException>
2015-09-28 19:58
686 查看
运行结果:
ArrayIndexOutBounds.java
Enter the array's index less than 100: -5 q 111 9 Out of bounds. Enter the array's index less than 100: qq Index must be digital. Enter the array's index less than 100: 5 Index 5: 144
ArrayIndexOutBounds.java
import java.util.InputMismatchException; import java.util.Scanner; public class ArrayIndexOutBounds { public static void main(String[] args) { int[] a = new int[100]; for (int i = 0; i < 100; i++) { a[i] = (int) (Math.random() * 200); } Scanner input = new Scanner(System.in); boolean n = true; do { System.out.print("Enter the array's index less than 100: "); try { int index = input.nextInt(); if (index >= 0 || index < 100) { System.out.println("Index " + index + ": " + a[index]); n = false; } } catch (InputMismatchException e) { System.out.println("Index must be digital."); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Out of bounds."); } input.nextLine(); } while (n); } }
相关文章推荐
- SpringMVC+Hbernate+Spring简单使用
- java 反射方式访问私有属性和方法
- java知识复习提纲(考试需要而写)
- 基于struts2的登录系统
- java自带线程池和队列详细讲解
- 图结构 java实现
- Spring @Pointcut的用法
- JAVA中的数据存储(堆及堆栈)
- 9.28 Java基本数据类型作业
- JAVA NIO 真正做到处理一个事件
- springmvc url参数和调用方法参数对应原理
- java IO学习
- Java垃圾回收工作原理
- Introduction to Java Programming编程题12.2<InputMismatchException>
- Java默认构造函数的一些发现
- Java反射机制 和 泛型函数设计
- java多线程学习
- 【JavaWeb探究】自定义JSP标签(一)
- Java机制
- Java学习笔记_003_方法和数组