
1.计算两数相乘的结果,要求乘数不能超过99,超过就抛出异常
class MyException extends Exception{ //自定义异常类,继承Exception类
public MyException(String msg){ //构造方法接收异常信息
super(msg); //调用父类中的构造方法
}
}
public class Throwdemo {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.print("请输入一个整数:");
int a = scan.nextInt();
System.out.print("请输入另外一个整数:");
int b = scan.nextInt();
int sum = a * b;
if( sum>99){
try{
throw new MyException("超出范围了!");
}catch(Exception e){
System.out.println(e);
}
}
else
System.out.print("他们的乘是" + sum);}}
2.车站检查危险品的设备,如果发现危险品会发出警告。编程模拟设备发现危险品。
1.编写一个一个Exception的子类DangerException,
该子类可以创建异常对象,该异常对象调用toShow()方法输出:“属于危险品”。
2.编写一个Machine类,
该类的方法checkBag(Goods goods)当发现参数goods是危险品时(goods的isDanger属性是true)将抛出DangerException异常。
3.程序在主类的main方法中的try~catch语句的try部分让Machine类的实例调用checkBag(Goods goods)方法,
如果发现危险品就在try~catch语句的catch部分处理危险品。
public class Goods {
boolean isDanger;
String name;
public void setIsDanger(boolean boo) {
isDanger = boo;
}
public boolean isDanger() {
return isDanger;
}
public void setName(String s) {
name = s;
}
public String getName() {
return name;}}
public class DangerException extends Exception {
String message;
public DangerException() {
message = "危险品!";
}
public void toShow() {
System.out.print(message+" ");}}
public class Machine {
public void checkBag(Goods goods) throws DangerException {
if(goods.isDanger()) {
DangerException danger=new DangerException();
throw danger; //抛出danger
}
else {
System.out.print(goods.getName()+"不是危险品! ");}}}
public class Check {
public static void main(String args[]) {
Machine machine = new Machine();
String name[] ={"苹果","炸药","西服","硫酸","手表","硫磺"};
Goods [] goods = new Goods[name.length]; //检查6件货物
for(int i= 0;i
java的核心API
1.简历内容如下:
“姓名:张三 出生时间:1989.10.16。个人网站:http://www.zhang.com。身高:185 cm,体重:72 kg”;
编写一个Java应用程序,判断简历中的姓名是否姓“张”,单独输出简历中的出生日期和个人网站,并判断简历中的身高是否大于180充满,体重是否小于75kg.
public class FindMess {
public static void main(String[] args) {
String mess="姓名:张三 出生时间:1989.10.16。个人网站:http://www.zhang.com。身高:185cm,体重:72kg";
int index= mess.indexOf(":"); //mess调用indexOf(String s)方法返回字符串中首次出现冒号的位置
String name=mess.substring(index+1);
if(name.startsWith("张")){
System.out.println("简历中的姓名姓"张" ");
}
index= mess.indexOf(":",index+1); //mess调用indexOf(String s,int start)返回字符串中第2次出现冒号的位置
String date=mess.substring(index+1,index+11);
System.out.println(date);
index=mess.indexOf(":",index+1);
int heightPosition= mess.indexOf("身高"); //mess调用indexOf(String s)返回字符串中首次出现身高的位置
String personNet=mess.substring(index+1,heightPosition-1);
System.out.println(personNet);
index=mess.indexOf(":",index+1); //mess调用indexOf(String s,int start)返回字符串中身高后面的冒号位置
int cmposition=mess.indexOf("cm");
String height=mess.substring(index+1,cmposition);
height=height.trim();
int h=Integer.parseInt(height);
if(h>=180){
System.out.println("简历中的身高"+height+"大于或等于180cm");
}
else {
System.out.println("简历中的身高"+height+"小于180cm");
}
index=mess.indexOf(":",index+1);//mess调用lastIndexOf(String s)返回字符串中最后一个冒号的位置
int kgPosition=mess.indexOf("kg");
String weight=mess.substring(index+1,kgPosition);
weight=weight.trim();
int w=Integer.parseInt(weight);
if(w>=75){
System.out.println("简历中的体重"+weight+"大于或等于75kg");
}
else{
System.out.println("简历中的体重"+weight+"小于75kg");
}
}
}
集合
1.贪吃蛇
public class Node {
private int i;
private int j;
public Node(int i,int j){
this.i=i;
this.j=j;
}
public Node(){}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + i;
result = prime * result + j;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (i != other.i)
return false;
if (j != other.j)
return false;
return true;}}
public class Worm {
linkedList node=new linkedList();
public static final int up=-10;
public static final int down=10;
public static final int right=-1;
public static final int left=1;
private int dir;
public Worm(){
node.add(new Node(4,9));
node.add(new Node(4,10));
node.add(new Node(5,10));
node.add(new Node(6,10));
node.add(new Node(6,11));
node.add(new Node(7,11));
node.add(new Node(8,11));
dir=right;
}
public boolean baohan(int i,int j){
return node.contains(new Node(i,j));
}
public void step(int dir){
if(this.dir+dir==0){
throw new RuntimeException("不能掉头");
}
this.dir=dir;
step();
}
public void step(){
Node n=node.getFirst();
int i=n.getI()+dir/10;
int j=n.getJ()+dir%10;
n=new Node(i,j);
node.addFirst(n);
node.removeLast();
}
}
public class WormPare {
private static Worm worm;
private int rows=15;
private int cols=30;
public WormPare(){
worm=new Worm();
}
public void print(){
for(int i=0;i
2.扫雷游戏的布雷面板可以用二维int数组表示
若某位置为地雷,则该位置用数字-1表示, 若该位置不是地雷,则暂时用数字0表示。 程序读入三个参数:布雷面板的行数(r) 列数(c) 布置的地雷数(n),且要满足要求0
public class Mines_method {
static int[][] bz;
static int[][] minePanel;
public static void layMines(int row, int col, int mines) {
if (mines <= 0 || mines >= row * col * 0.75) {
System.out.println("指定的地雷数无效");
return;
}
if (row < 2 || col < 2) {
System.out.println("指定的行数和列数无效");
return;
}
bz = new int[row][col];
int ms = 0;// 当前已布置 的地雷数
while (ms < mines) {
int r = (int) (Math.random() * row);
int c = (int) (Math.random() * col);
if (bz[r][c] != -1) {
bz[r][c] = -1;
ms++;
}
}
}
public static void accountMines() {
for (int i = 0; i < bz.length; i++) {
for (int j = 0; j < bz[i].length; j++) {
// 当前位置是地雷,则跳过该位置,统计下一位置
if (bz[i][j] == -1) {
continue;
}
// 统计周围的地雷数
int numberOfSurroundingMines = 0;
// 左上角位置
if (i - 1 >= 0 && j - 1 >= 0 && bz[i - 1][j - 1] == -1) {
numberOfSurroundingMines++;
}
// 正上位置
if (i - 1 >= 0 && bz[i - 1][j] == -1) {
numberOfSurroundingMines++;
}
// 右上角位置
if (i - 1 >= 0 && j + 1 <= bz[i].length - 1
&& bz[i - 1][j + 1] == -1) {
numberOfSurroundingMines++;
}
// 正左位置
if (j - 1 >= 0 && bz[i][j - 1] == -1) {
numberOfSurroundingMines++;
}
// 正右位置
if (j + 1 <= bz[i].length - 1 && bz[i][j + 1] == -1) {
numberOfSurroundingMines++;
}
// 左下角位置
if (i + 1 <= bz.length - 1 && j - 1 >= 0
&& bz[i + 1][j - 1] == -1) {
numberOfSurroundingMines++;
}
// 正下位置
if (i + 1 <= bz.length - 1 && bz[i + 1][j] == -1) {
numberOfSurroundingMines++;
}
// 右下角位置
if (i + 1 <= bz.length - 1 && j + 1 <= bz[i].length - 1
&& bz[i + 1][j + 1] == -1) {
numberOfSurroundingMines++;
}
bz[i][j] = numberOfSurroundingMines;
}
}
}
public void print1(){
for (int a = 0; a < bz.length; a++) {
for (int b= 0; b < bz[a].length; b++) {
System.out.print(bz[a][b] + "t");
}
System.out.println();
}
}
public static void dealMines(int i, int j) {
for (int a = i - 1; a <= i + 1; a++)
for (int b = j - 1; b <= j + 1; b++) {
if (a >= 0 && a < bz.length && b >= 0 && b < bz.length
&& !(a == i && b == j) && minePanel[a][b] == 0) {
if (bz[a][b] == 0) {
// 如果当前位置周围没地雷用10表示
minePanel[a][b] = bz[a][b] + 1000;
dealMines(a, b);
} else {
// 如果当前位置周围有地雷,显示地雷的个数
System.out.println("排雷失败");
minePanel[a][b] = bz[a][b];}}}}
public void print2(){
for (int a = 0; a < minePanel.length; a++) {
for (int b= 0; b < minePanel[a].length; b++) {
System.out.print(minePanel[a][b] + "t");
}
System.out.println();}}
public class Mine {
public static void main(String[] args) {
Scanner sca=new Scanner(System.in);
Mines_method m = new Mines_method();
m.layMines(10, 10, 10);
int[][] bz = m.bz;
m.accountMines();
m.print1();
m.minePanel=bz;
System.out.print("请输入行号:");
int h=sca.nextInt();
System.out.print("请输入列号:");
int l=sca.nextInt();
m.dealMines(h,l);
m.print2();
}
}