最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Java(8題):異常,通過try catch進(jìn)行處理,登錄,商品,使用jdbc進(jìn)行讀取,詳細(xì)圖析

2020-03-01 22:16 作者:詩書畫唱  | 我要投稿

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

package a;


import java.util.Scanner;


public class yichang {

public static void main(String[] args) {


try {

System.out.println("請(qǐng)輸入班級(jí)的人數(shù)");

Scanner s = new Scanner(System.in);

int renshu = s.nextInt();

if (renshu < 0) {

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

}

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

} catch (RuntimeException e) {

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

}

}

}

2.定義一個(gè)數(shù)組長(zhǎng)度為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;//下標(biāo)越界的異常


} catch (ArithmeticException e) {

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

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

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

}catch(ArrayIndexOutOfBoundsException e1){

//下標(biāo)越界操作

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

}

catch(Exception e2){

//其他異常

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

}



}

}

3、創(chuàng)建Student對(duì)象,設(shè)置name和age屬性,在main方法中添加如下的代碼:
Student stu = null;
stu.setName("張三");
System.out.println("學(xué)生的姓名設(shè)置完畢");

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


package a;


public class yichang {

public static void main(String[] args) {



try {

Student stu=null;

stu. setName("張三");

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

} catch (Exception e) {

// TODO: handle exception

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

}

}


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("請(qǐng)輸入年齡");




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




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


if(100<age||age<1){throw new Exception();

}

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

} 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)容長(zhǎng)度是否大于6位數(shù),密碼是否為空,年齡是否滿18(使用異常制作)

package a;




import java.util.Scanner;




public class yichang {




public static void main(String[] args) {




che timegame = new huoche(1, "時(shí)間游戲", 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("---------“詩書畫唱”名品牌游戲購(gòu)買系統(tǒng)-----------");




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




Scanner s = new Scanner(System.in);




int num = s.nextInt();




if (num == 1) {




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




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

String uname = s.next();

try{

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

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

/*拋出異常*/

}



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

}


} catch (StringIndexOutOfBoundsException e) {


// TODO: handle exception


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

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


}








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




String pwd = s.next();

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


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í)行注冊(cè)操作----");




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

String uname = s.next();



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

int age = s.nextInt();


try{

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


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


}

catch (Exception e) {


// TODO: handle exception


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



}




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

String pwd = s.next();

//pwd=null;

try{

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

/*拋出異常*/

}



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


} catch (StringIndexOutOfBoundsException e) {


// TODO: handle exception


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

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


}


catch (NullPointerException e) {


// TODO: handle exception


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


}





System.out.println("因?yàn)閲?guó)家法律,要實(shí)名認(rèn)證,請(qǐng)輸入你的真實(shí)姓名");




String name = s.next();



yh.uname = uname;




yh.pwd = pwd;




yh.name = name;


yh.age = age;



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




} else if (num == 3) {




System.exit(0);




}




}




// 購(gòu)買游戲操作




while (true) {




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




System.out


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




Scanner s1 = new Scanner(System.in);




int num = s1.nextInt();




if (num == 1) {




for (che i : c) {/* 相當(dāng)于for(int i=0;i<c.length;i++) */




i.jieshao();




}




} else if (num == 2) {




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




int num1 = s1.nextInt();




c_zuche = new che[num1];




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




System.out.println("請(qǐng)輸入第" + (i + 1) + "游戲的編號(hào)");




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("購(gòu)買成功!");




} else if (num == 3) {




if (c_zuche == null) {




System.out.println("你還沒有進(jìn)行購(gòu)買呢!");




} 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("總價(jià)格" + 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("編號(hào)" + this.bianhao + "\t名稱:" + this.name +




"\t價(jià)格" + 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("編號(hào)" + this.bianhao + "\t名稱:" +




this.name + "\t價(jià)格" + 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("編號(hào)" + this.bianhao + "\t名稱:" +




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




this.zaiwuliang);




}




}

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

package a;


import java.util.Scanner;


public class yichang {

public static void main(String[] args) {






try {System.out.println("請(qǐng)輸入商品的價(jià)格");




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




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


if(500<jiage||jiage<100){throw new Exception();

}

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

} catch (Exception e) {

// TODO: handle exception

System. out. println("詩書畫唱提醒你,商品的價(jià)格不大于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,包含編號(hào),姓名,年齡,性別,成績(jī)屬性,使用jdbc進(jìn)行讀取數(shù)據(jù)庫中的數(shù)據(jù)


package a;

import java.sql.*;

public class yichang {


public static void main(String[] args) {

// TODO Auto-generated method stub

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

// 1.加載數(shù)據(jù)庫驅(qū)動(dòng)(非運(yùn)行時(shí)的異常)

try {

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

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

//這個(gè)鏈接數(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.獲取結(jié)果(存入到數(shù)據(jù)連接池中)

ResultSet res=sta.executeQuery(sql);

// 5.遍歷結(jié)果

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();

}


}


}












類比替換法。

相關(guān)知識(shí)圖:








、






看結(jié)構(gòu)


Java(8題):異常,通過try catch進(jìn)行處理,登錄,商品,使用jdbc進(jìn)行讀取,詳細(xì)圖析的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
沙雅县| 湛江市| 五华县| 东丰县| 新巴尔虎右旗| 黄石市| 卢湾区| 台前县| 双流县| 莱芜市| 临汾市| 忻州市| 宜丰县| 石台县| 垣曲县| 邯郸市| 元谋县| 鸡泽县| 鹤峰县| 桓仁| 平邑县| 策勒县| 山西省| 鄂尔多斯市| 镇坪县| 梁河县| 盐边县| 若羌县| 汶川县| 阿拉善左旗| 宜章县| 庆元县| 石城县| 松溪县| 牡丹江市| 交口县| 孝感市| 沙坪坝区| 长宁县| 周宁县| 吴堡县|