資源描述:
《java程序代碼框架所有代碼集合.doc》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、Java程序設(shè)計(jì)基礎(chǔ)例題1//app2_1.java簡單的java應(yīng)用程序2publicclassapp2_1//定義app2_1類3{4publicstaticvoidmain(String[]args)//定義主方法5{6System.out.println("HelloJava!");7}8}1//App2_2.javaJava小程序2importjava.awt.*;//加載java.awt類庫中的所有類3importjava.applet.*;//加載java.applet類庫中的所有類4publiccl
2、assApp2_2extendsApplet//定義類App2_2,其父類為Applet5{6publicvoidpaint(Graphicsg)7{8g.drawString("HelloJava!",50,50);9}10}1//app3_1.java類型自動轉(zhuǎn)換2publicclassa
3、pp3_1//定義類app3_13{4publicstaticvoidmain(String[]args)5{6inta=155;7floatb=21.0f;8System.out.println("a="+a+",b="+b);//輸出a,b的值9System.out.println("a/b="+(a/b));//輸出a/b的值10}11}1//app3_2.java整數(shù)與浮點(diǎn)數(shù)的類型轉(zhuǎn)換2publicclassapp3_23{4publicstaticvoidmain(String[]args)5{6inta
4、=155;7intb=9;8floatg,h;9System.out.println("a="+a+",b="+b);//輸出a,b的值10g=a/b;//將a除以b的結(jié)果放在g中11System.out.println("a/b="+g+"");//輸出g的值12System.out.println("a="+a+",b="+b);//輸出a,b的值13h=(float)a/b;//先將a強(qiáng)制轉(zhuǎn)換成float類型后再參加運(yùn)算14System.out.println("a/b="+h);//輸出h的值15}16
5、}1//app3_3.java由鍵盤輸入字符串2importjava.io.*;//加載java.io類庫里的所有類3publicclassapp3_34{5publicstaticvoidmain(String[]args)throwsIOException6{7BufferedReaderbuf;8Stringstr;9buf=newBufferedReader(newInputStreamReader(System.in));10System.out.print("請輸入字符串;");11str=buf.re
6、adLine();//將輸入的文字指定給字符串變量str存放12System.out.println("您輸入的字符串是:"+str);//輸出字符串13}14}1//app3_4.java由鍵盤輸入整數(shù)2importjava.io.*;3publicclassapp3_44{5publicstaticvoidmain(String[]args)throwsIOException6{7floatnum;8Stringstr;9BufferedReaderbuf;10buf=newBufferedReader(new
7、InputStreamReader(System.in));11System.out.print("請輸入一個(gè)實(shí)數(shù):");1str=buf.readLine();//將輸入的文字指定給字符串變量str存放2num=Float.parseFloat(str);//將str轉(zhuǎn)換成float類型后賦給num3System.out.println("您輸入的數(shù)為:"+num);4}5}1//app3_5.java由鍵盤輸入多個(gè)數(shù)據(jù)2importjava.io.*;3publicclassapp3_54{5publicsta
8、ticvoidmain(String[]args)throwsIOException6{7intnum1,num2;8Stringstr1,str2;9InputStreamReaderin;10in=newInputStreamReader(System.in);11BufferedReaderbuf;12buf=newBufferedReader(in)