您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > Java程序设计基础例题
Java程序设计基础例题1//app2_1.java简单的java应用程序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类库中的所有类4publicclassApp2_2extendsApplet//定义类App2_2,其父类为Applet5{6publicvoidpaint(Graphicsg)7{8g.drawString(HelloJava!,50,50);9}10}App2_2.htmhtmlAPPLETcode=App2_2.classwidth=200height=120alt=很抱歉,您的浏览器不支持Javaapplet。/APPLET/html1//app3_1.java类型自动转换2publicclassapp3_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整数与浮点数的类型转换2publicclassapp3_23{4publicstaticvoidmain(String[]args)5{6inta=155;7intb=9;8floatg,h;9System.out.println(a=+a+,b=+b);//输出a,b的值10g=a/b;//将a除以b的结果放在g中11System.out.println(a/b=+g+\n);//输出g的值12System.out.println(a=+a+,b=+b);//输出a,b的值13h=(float)a/b;//先将a强制转换成float类型后再参加运算14System.out.println(a/b=+h);//输出h的值15}16}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.readLine();//将输入的文字指定给字符串变量str存放12System.out.println(您输入的字符串是:+str);//输出字符串13}14}1//app3_4.java由键盘输入整数2importjava.io.*;3publicclassapp3_44{5publicstaticvoidmain(String[]args)throwsIOException6{7floatnum;8Stringstr;9BufferedReaderbuf;10buf=newBufferedReader(newInputStreamReader(System.in));11System.out.print(请输入一个实数:);12str=buf.readLine();//将输入的文字指定给字符串变量str存放13num=Float.parseFloat(str);//将str转换成float类型后赋给num14System.out.println(您输入的数为:+num);15}16}1//app3_5.java由键盘输入多个数据2importjava.io.*;3publicclassapp3_54{5publicstaticvoidmain(String[]args)throwsIOException6{7intnum1,num2;8Stringstr1,str2;9InputStreamReaderin;10in=newInputStreamReader(System.in);11BufferedReaderbuf;12buf=newBufferedReader(in);13System.out.print(请输入第一个数:);14str1=buf.readLine();//将输入的内容赋值给字符串变量str115num1=Integer.parseInt(str1);//将str1转成int类型后赋给num116System.out.print(请输入第二个数:);17str2=buf.readLine();//将输入的内容赋值给字符串变量str218num2=Integer.parseInt(str2);//将str2转成int类型后赋给num219System.out.println(num1+*+num2+=+(num1*num2));20}21}1//app3_6.java由键盘输入多个数据2importjava.util.*;//加载java.util类库里的所有类3publicclassapp3_64{5publicstaticvoidmain(String[]args)6{7intnum1;8doublenum2;9Scannerreader=newScanner(System.in);10System.out.print(请输入第一个数:);11num1=reader.nextInt();//将输入的内容做int型数据赋值给变量num112System.out.print(请输入第二个数:);13num2=reader.nextDouble();//将输入的内容做double型数据赋值给变量num214System.out.println(num1+*+num2+=+(num1*num2));15}16}1//app3_7.java由键盘输入多个数据2importjava.util.*;//加载java.util类库里的所有类3publicclassapp3_74{5publicstaticvoidmain(String[]args)6{7Strings1,s2;8Scannerreader=newScanner(System.in);9System.out.print(请输入第一个数:);10s1=reader.nextLine();//将输入的内容做为字符型数据赋值给变量s111System.out.print(请输入第二个数:);12s2=reader.next();//按Enter键时next()方法将回车符和换行符13System.out.println(输入的是+s1+和+s2);14}15}1//app3_8.java关系运算符和逻辑运算符的使用2publicclassapp3_83{4publicstaticvoidmain(String[]args)5{6inta=25,b=7;7booleanx=ab;//x=false8System.out.println(ab=+x);9inte=3;10booleany=a/e5;//y=true11System.out.println(x^y=+(x^y));12if(e!=0&b0)System.out.println(b/0=+b/0);13elseSystem.out.println(a%e=+a%e);14intf=0;15if(f!=0&&a/f5)System.out.println(a/f=+a/f);16elseSystem.out.println(f=+f);17}18}1//app4_1.javaif语句的应用2publicclassapp4_13{4publicstaticvoidmain(String[]args)5{6inta=1,b=2,c=3,max,min;7if(ab)8max=a;9else10max=b;11if(cmax)max=c;12System.out.println(Max=+max);13min=ab?a:b;14min=cmin?c:min;15System.out.println(Min=+min);16}17}1//app4_2.java多重条件选择语句的应用2publicclassapp4_23{4publicstaticvoidmain(String[]args)5{6inttestscore=86;7chargrade;8if(testscore=90){9grade='A';10}elseif(testscore=80){11grade='B';12}elseif(testscore=70){13grade='C';14}elseif(testscore=60){15grade='D';16}else{17grade='E';18}19System.out.println(评定成绩为:+grade);20}21}1//app4_3.javaswitch语句的应用2publicclassapp4_33{4publicstaticvoidmain(String[]args)throwsException5{6inta=100,b=6;7charoper;8System.out.print(请输入运算符:);9oper=(char)System.in.read();//从键盘读入一个字符存入变量oper中10switch(oper)11{12case'+'://输出a+b13System.out.println(a+++b+=+(a+b));14break;15case'-'://输出a-b16System.out.println(a+-+b+=+(a-b));17break;18case'*'://输出a*b19System.out.println(a+*+b+=+(a*b));20break;21case'/'://输出a/b22System.out.println(a+/+b+=+((float)a/b));23break;24default://输出字符串25System.out.println(输入的符号不正确!);26}27}28}1//app4_4.javaswitch语句的应用2importjava.util.*;3publicclassapp4_44{5publicstaticvoidmain(String[]args)6{7intmonth,days;8Scannerreader=newScanner(System.in);9System.out.print(请输入月份:);10month=reader.nextInt();11switch(month)12{13case2:days=28;//2月份是28天14break;15case4:16case6:17case9:18case11:days=30;//4、6、9、11月份的天数为3019break;20default:days=31;//其它月份为31天21}22System.out.println(month+月份为【+days+】天);23}24}1//app4_5.javawhile语句的应用2publicclassapp4_53{4publicstaticvoidmain(String[]args)5{6finalintMAX=15;//定义常量MAX=157inti=0,j
本文标题:Java程序设计基础例题
链接地址:https://www.777doc.com/doc-3550515 .html