日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java获取异常的数据_Java(8题):异常,通过try catch进行处理,登录,商品,使用jdbc进行读取,详细图析...

發(fā)布時間:2025/3/15 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java获取异常的数据_Java(8题):异常,通过try catch进行处理,登录,商品,使用jdbc进行读取,详细图析... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

// 1.要求輸入班級人數(shù),人數(shù)必須是正整數(shù)。通過異常機制來處理下,提示輸入的人數(shù)不能為負數(shù)

package a;

import java.util.Scanner;

public class yichang {

public static void main(String[] args) {

try {

System.out.println("請輸入班級的人數(shù)");

Scanner s = new Scanner(System.in);

int renshu = s.nextInt();

if (renshu < 0) {

throw new RuntimeException("人數(shù)必須為正整數(shù)");

}

System.out.println("班級人數(shù)為" + renshu);

} catch (RuntimeException e) {

System.out.println("輸入的人數(shù)不能為負數(shù)");

}

}

}

2.定義一個數(shù)組長度為3的數(shù)組,打印其數(shù)據(jù),要求(使用異常制作)

package a;

public class yichang {

public static void main(String[] args) {

try {

int[] up=new int[3];

int num1=10;

int num2=1;

int num3=num1/num2;//除數(shù)為0的異常

up[0]=11;

up[1]=22;

up[2]=33;

up[3]=44;//下標越界的異常

} catch (ArithmeticException e) {

//出現(xiàn)除數(shù)為0的時候執(zhí)行這塊的內(nèi)容

//除數(shù)為的操作

System. out. println("b站up主詩書畫唱提醒你,你的除數(shù)為0啦,快點贊!");

}catch(ArrayIndexOutOfBoundsException e1){

//下標越界操作

System. out. println("b站up主詩書畫唱提醒你,下標越界啦,快投幣!");

}

catch(Exception e2){

//其他異常

System. out. println( "b站up主詩書畫唱提醒你,出現(xiàn)了其他類型的異常,快三連,關注!");

}

}

}

3、創(chuàng)建Student對象,設置name和age屬性,在main方法中添加如下的代碼:

Student stu = null;

stu.setName("張三");

System.out.println("學生的姓名設置完畢");

4.請分析以上的代碼出現(xiàn)的錯誤,并通過try catch進行處理。

package a;

public class yichang {

public static void main(String[] args) {

try {

Student stu=null;

stu. setName("張三");

System . out. println("詩書畫唱提醒你,學生的姓名設置完畢");

} catch (Exception e) {

// TODO: handle exception

System. out. println("詩書畫唱提醒你,學生的姓名設置有誤");

}

}

class Student{

private String name;

private int age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}}

5.定義人的年齡不能大于100歲并小于1歲(使用異常制作)

package a;

import java.util.Scanner;

public class yichang {

public static void main(String[] args) {

try {System.out.println("請輸入年齡");

Scanner s=new Scanner(System.in);//聲明Scanner接收的類

int age= s.nextInt();//接收用戶輸入整形的數(shù)據(jù)

if(100

}

else{System . out. println("詩書畫唱提醒你,人的年齡設置完畢");}

} catch (Exception e) {

// TODO: handle exception

System. out. println("詩書畫唱提醒你,人的年齡不能大于100歲并不能小于1歲");

}

}

class shangpin{

private String name;

private int age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age =age;

}

}}

//6.制作登錄界面,使用異常判斷用戶輸入是否為空,輸入內(nèi)容長度是否大于6位數(shù),密碼是否為空,年齡是否滿18(使用異常制作)

package a;

import java.util.Scanner;

public class yichang {

public static void main(String[] args) {

che timegame = new huoche(1, "時間游戲", 30, 0, 3);

che mengame = new huoche(2, "爺們游戲", 50, 0, 5);

che? ?woman= new keche(3, "女人游戲", 40, 4, 0);

che run = new keche(4, "跑步游戲", 80, 10, 0);

che swim = new pika(5, "游泳游戲", 60, 2, 4);

che sing = new pika(6, "唱歌游戲", 100, 4, 8);

che[] c = new che[6];

c[0] = timegame;

c[1] = mengame;

c[2] = woman;

c[3] = run;

c[4] = swim;

c[5] = sing;

yonghu yh = new yonghu();

che[] c_zuche = null;

while (true) {

System.out.println("---------“詩書畫唱”名品牌游戲購買系統(tǒng)-----------");

System.out.println("請輸入你的操作:1.登錄? 2.注冊? 3.跳出系統(tǒng)");

Scanner s = new Scanner(System.in);

int num = s.nextInt();

if (num == 1) {

System.out.println("----執(zhí)行登錄操作----");

System.out.println("請輸入用戶名:");

String uname = s.next();

try{

if(uname.length()<6){throw new StringIndexOutOfBoundsException("詩書畫唱提醒你,"

+ "輸入內(nèi)容長度已經(jīng)小于6");

/*拋出異常*/

}

else{System . out. println("輸入內(nèi)容長度已經(jīng)不小于6");

}

} catch (StringIndexOutOfBoundsException e) {

// TODO: handle exception

System. out. println("詩書畫唱提醒你,輸入內(nèi)容長度已經(jīng)小于6,為確保你的安全,"

+ "最好大于6位數(shù)!且密碼不能為空!");

}

System.out.println("請輸入你的密碼");

String pwd = s.next();

System.out.println("請輸入你的年齡");

int age = s.nextInt();

if (uname.equals(yh.uname) && pwd.equals(yh.pwd)&& age ==yh.age) {

System.out.println("登錄成功!");

break;

} else {

System.out.println("登錄失敗");

}

} else if (num == 2) {

System.out.println("----執(zhí)行注冊操作----");

System.out.println("請輸入用戶名:");

String uname = s.next();

System.out.println("請輸入你的年齡");

int age = s.nextInt();

try{

if(age<18){throw new Exception("詩書畫唱提醒你,你未滿18歲,不能玩和購買這款游戲!");

}else{System . out. println("詩書畫唱提醒你,年齡設置成功!");}

}

catch (Exception e) {

// TODO: handle exception

System. out. println("詩書畫唱警告你,你未滿18歲!");

}

System.out.println("請輸入你的密碼(最好輸入內(nèi)容長度大于6位數(shù),不能為空)");

String pwd = s.next();

//pwd=null;

try{

if(pwd.length()<6){throw new StringIndexOutOfBoundsException("詩書畫唱提醒你,密碼不能小于6位數(shù)!");

/*拋出異常*/

}

else{System . out. println("詩書畫唱提醒你,密碼設置成功!");}

} catch (StringIndexOutOfBoundsException e) {

// TODO: handle exception

System. out. println("詩書畫唱提醒你,輸入內(nèi)容長度不大于6位數(shù),為確保你的安全,"

+ "必須大于6位數(shù)!且密碼不能為空!");

}

catch (NullPointerException e) {

// TODO: handle exception

System. out. println("詩書畫唱提醒你,密碼不能為空!");

}

System.out.println("因為國家法律,要實名認證,請輸入你的真實姓名");

String name = s.next();

yh.uname = uname;

yh.pwd = pwd;

yh.name = name;

yh.age = age;

System.out.println("注冊成功!");

} else if (num == 3) {

System.exit(0);

}

}

// 購買游戲操作

while (true) {

System.out.println("歡迎," + yh.name + "請選擇你的操作:");

System.out

.println("1.查看游戲列表? 2.進行購買? 3.查看我的購買總價格,總可在線玩人數(shù),總可下線玩人數(shù) 4.跳出系統(tǒng)");

Scanner s1 = new Scanner(System.in);

int num = s1.nextInt();

if (num == 1) {

for (che i : c) {/* 相當于for(int i=0;i

i.jieshao();

}

} else if (num == 2) {

System.out.println("請輸入你要買的游戲數(shù)");

int num1 = s1.nextInt();

c_zuche = new che[num1];

for (int i = 0; i < num1; i++) {

System.out.println("請輸入第" + (i + 1) + "游戲的編號");

int bianhao = s1.nextInt();

switch (bianhao) {

case 1:

c_zuche[i] = timegame;

break;

case 2:

c_zuche[i] = mengame;

break;

case 3:

c_zuche[i] = woman;

break;

case 4:

c_zuche[i] = run;

break;

case 5:

c_zuche[i] = swim;

break;

case 6:

c_zuche[i] = sing;

break;

}

}

System.out.println("購買成功!");

} else if (num == 3) {

if (c_zuche == null) {

System.out.println("你還沒有進行購買呢!");

} else {

int zongjiage = 0;

int zongkezaixianwanrenshu = 0;

int zongkexiaxianwanrenshu = 0;

for (che i : c_zuche) {

i.jieshao();

zongjiage += i.jiage;

zongkezaixianwanrenshu += i.zaikeliang;

zongkexiaxianwanrenshu += i.zaiwuliang;

}

System.out.println("總價格" + zongjiage + "總可在線玩人數(shù)" +

zongkezaixianwanrenshu + "總可下線玩人數(shù)" + zongkexiaxianwanrenshu);

}

} else {

System.exit(0);/* 終止系統(tǒng),不可為break,break是跳出循環(huán) */

}

}

}

}

class yonghu {

String uname;

String pwd;

String name;

int age;

}

abstract class che {

int bianhao;

String name;

int jiage;

int zaikeliang;

int zaiwuliang;

public che(int bianhao, String name, int jiage, int zaikeliang,int zaiwuliang) {

this.bianhao = bianhao;

this.name = name;

this.jiage = jiage;

this.zaikeliang = zaikeliang;

this.zaiwuliang = zaiwuliang;

}

abstract void jieshao();

}

class huoche extends che {

public huoche(int bianhao, String name, int jiage,

int zaikeliang, int zaiwuliang) {

super(bianhao, name, jiage, zaikeliang, zaiwuliang);

}

void jieshao() {

System.out.println("編號" + this.bianhao + "\t名稱:" + this.name +

"\t價格" + this.jiage + "\t總可在線玩人數(shù)" + this.zaiwuliang);

}

}

class keche extends che {

public keche(int bianhao, String name, int jiage, int zaikeliang,

int zaiwuliang) {

super(bianhao, name, jiage, zaikeliang, zaiwuliang);

}

void jieshao() {

System.out.println("編號" + this.bianhao + "\t名稱:" +

this.name + "\t價格" + this.jiage + "\t總可下線玩人數(shù)" + this.zaikeliang);

}

}

class pika extends che {

public pika(int bianhao, String name, int jiage, int zaikeliang,

int zaiwuliang) {

super(bianhao, name, jiage, zaikeliang, zaiwuliang);

}

void jieshao() {

System.out.println("編號" + this.bianhao + "\t名稱:" +

this.name + "\t價格" + this.jiage + "\t總可在線玩人數(shù)" + this.zaikeliang + "總可下線玩人數(shù)" +

this.zaiwuliang);

}

}

7.定義商品類,判斷商品的價格是否大于100并小于500(使用異常制作)

package a;

import java.util.Scanner;

public class yichang {

public static void main(String[] args) {

try {System.out.println("請輸入商品的價格");

Scanner s=new Scanner(System.in);//聲明Scanner接收的類

int jiage= s.nextInt();//接收用戶輸入整形的數(shù)據(jù)

if(500

}

else{System. out. println("詩書畫唱提醒你,商品的價格大于100并小于500");}

} catch (Exception e) {

// TODO: handle exception

System. out. println("詩書畫唱提醒你,商品的價格不大于100并且不小于500");

}

}

class shangpin{

private String name;

private int jiage;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getJiage() {

return jiage;

}

public void setJiage(int jiage) {

this.jiage =jiage;

}

}}

//JDBC作業(yè):

//1.建立一張數(shù)據(jù)庫表stu,包含編號,姓名,年齡,性別,成績屬性,使用jdbc進行讀取數(shù)據(jù)庫中的數(shù)據(jù)

package a;

import java.sql.*;

public class yichang {

public static void main(String[] args) {

// TODO Auto-generated method stub

//Jar:各個數(shù)據(jù)庫廠商都實現(xiàn)了ODBC規(guī)范,形成了自己的API,就是我們導入的這個jar包

//1.加載數(shù)據(jù)庫驅動(非運行時的異常)

try {

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

//2.建立連接(參數(shù)三個,1用戶名? ?2密碼? ?3數(shù)據(jù)庫名稱)

//這個鏈接數(shù)據(jù)庫也要拋異常,為了方便,我將所有異常一塊拋出

Connection con=DriverManager.getConnection("jdbc:sqlserver://DESKTOP-49FTFSP;"

+ "databaseName=stud","sa","1234abcd");

//3.執(zhí)行SQL語句

String sql="select * from Student";

Statement sta=con.createStatement();

//4.獲取結果(存入到數(shù)據(jù)連接池中)

ResultSet res=sta.executeQuery(sql);

//5.遍歷結果

while(res.next()){

System.out.print(res.getObject("Id")+"? ?");

System.out.print(res.getObject("Name")+"? ?");

System.out.print(res.getObject("Gender")+"? ?");

System.out.print(res.getObject("Height")+"? ?");

System.out.print(res.getObject("Weight")+"? ?");

System.out.print(res.getObject("Age")+"? ?");

System.out.print(res.getObject("Class")+"? ?");

System.out.println();

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

類比替換法。

相關知識圖:

看結構

總結

以上是生活随笔為你收集整理的java获取异常的数据_Java(8题):异常,通过try catch进行处理,登录,商品,使用jdbc进行读取,详细图析...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。