1、编写一个Java程序在屏幕上输出“你好!”。
public class Helloworld {public static void main(String args[]) {System.out.print ("你好!" );}
}
- 编写一个Java程序,用if-else语句判断某年份是否为闰年。
public class LeapYear{
public static void main(String args[]){
int year=2010;
if(args.length!=0)year=Integer.parseInt(args[0]);
if((year%4==0 && year%100!=0)||(year%400==0))System.out.println(year+" 年是闰年。");
elseSystem.out.println(year+" 年不是闰年。");}}
3.编写一个Java程序在屏幕上输出1!+2!+3!+……+10!的和。
public class ForTest {
public static void main( String args[] ) {int i,j,mul,sum=0;
for(i&#61;1;i<&#61;10;i&#43;&#43;) {
mul&#61;1;
for(j&#61;1,j<&#61;i;j&#43;&#43;) {
mul&#61;mul*j;
}sum&#61;sum&#43;mul;
public class Factorial {
System.out.println(“1&#xff01;&#43;2&#xff01;&#43;3&#xff01;&#43;……&#43;10&#xff01;&#61; ”&#43;sum);
}
}
4.&#xff08;1&#xff09;编写一个圆类Circle&#xff0c;该类拥有&#xff1a;
①一个成员变量
Radius&#xff08;私有&#xff0c;浮点型&#xff09;&#xff1b; // 存放圆的半径&#xff1b;
②两个构造方法
Circle( ) // 将半径设为0
Circle(double r ) //创建Circle对象时将半径初始化为r
③ 三个成员方法
double getArea( ) //获取圆的面积
double getPerimeter( ) //获取圆的周长
void show( ) //将圆的半径、周长、面积输出到屏幕
&#xff08;2&#xff09;编写一个圆柱体类Cylinder&#xff0c;它继承于上面的Circle类。还拥有&#xff1a;
①一个成员变量
double hight&#xff08;私有&#xff0c;浮点型&#xff09;&#xff1b; // 圆柱体的高&#xff1b;
②构造方法
Cylinder (double r, double h ) //创建Circle对象时将半径初始化为r
③ 成员方法
double getVolume( ) //获取圆柱体的体积
void showVolume( ) //将圆柱体的体积输出到屏幕
编写应用程序&#xff0c;创建类的对象&#xff0c;分别设置圆的半径、圆柱体的高&#xff0c;计算并分别显示圆半径、圆面积、圆周长&#xff0c;圆柱体的体积。
class Circle { private double radius; Circle() { radius&#61;0.0;}Circle(double r) { radius&#61;r; }double getPerimeter() { return 2*Math.PI*radius;}double getArea() { return Math.PI*radius*radius;}void disp() { System.out.println("圆半径&#61;"&#43;radius);System.out.println("圆周长&#61;"&#43;getPerimeter());System.out.println("圆面积&#61;"&#43;getArea()); }
}
class Cylinder extends Circle { private double hight; Cylinder(double r,double h) { super(r);hight&#61;h;}public double getVol() { return getArea()*hight;}public void dispVol() { System.out.println("圆柱体积&#61;"&#43;getVol());}
}
public class TestCylinder {
public static void main(String[] args) { Circle Ci&#61;new Circle(10.0); Ci.disp(); Cylinder Cyl&#61;new Cylinder(5.0,10.0); Cyl.disp(); Cyl.dispVol(); }
}
5.编写一个Java应用程序&#xff0c;从键盘读取用户输入两个字符串&#xff0c;并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理&#xff0c;对输入的不符合要求的字符串提示给用户&#xff0c;不能使程序崩溃。
import java.io.*;
public class Strinput
{public static void main(String args[]) {String s1,s2,ss,si,sf;int i1,i2;float f1,f2;BufferedReader strin&#61;new BufferedReader(new InputStreamReader(System.in));try{System.out.print ("输入第一个字符串&#xff1a;" );s1&#61; strin.readLine();System.out.print ("输入第二个字符串&#xff1a;" );s2&#61; strin.readLine();}catch(Exception e){ System.out.println(e.getMessage());}i1 &#61; Integer.parseInt(s1);i2 &#61; Integer.parseInt(s2);f1 &#61; Float.parseFloat(s1);f2 &#61; Float.parseFloat(s2);ss &#61; strAdd(s1,s2);si &#61; strAdd(i1,i2);sf &#61; strAdd(f1,f2);System.out.println ("输入的二个字符串相加结果为&#xff1a;"&#43;ss );System.out.println ("输入字符串转换为整数相加结果为&#xff1a;"&#43;si );System.out.println ("输入字符串转换为浮点数相加结果为&#xff1a;"&#43;sf );}String strAdd(String str1,String str2) {
return str1&#43;str2;
}
String strAdd(int int1,int int2) {
return String.valueOf(int1&#43;int2);
}
String strAdd(float flt1,float flt2) {
return String.valueOf (flt1&#43;flt2);
}}
6.应用FileInputStream类&#xff0c;编写应用程序&#xff0c;从磁盘上读取一个Java程序&#xff0c;并将源程序代码显示在屏幕上。&#xff08;被读取的文件路径为&#xff1a;E:/myjava/Hello.java&#xff09;
import java.io.*;public class FISDemo {public static void main(String args[]) {byte[] buf&#61;new byte[2056];try{FileInputStream fileIn&#61;new FileInputStream("e:/myjava/Hello.java");int bytes&#61;fileIn.read(buf,0,2056);String str&#61;new String(buf,0,bytes);System.out.println(str);
}catch(Exception e){e.printStackTrace( );
}
}
7.定义一个表示学生信息的类Student&#xff0c;要求如下&#xff1a;
&#xff08;1&#xff09;类Student的成员变量&#xff1a;
sNO 表示学号&#xff1b;sName表示姓名&#xff1b;sSex表示性别&#xff1b;sAge表示年龄&#xff1b;sJava&#xff1a;表示Java课程成绩。
&#xff08;2&#xff09;类Student带参数的构造方法&#xff1a;
在构造方法中通过形参完成对成员变量的赋值操作。
&#xff08;3&#xff09;类Student的方法成员&#xff1a;
getNo&#xff08;&#xff09;&#xff1a;获得学号&#xff1b;
getName&#xff08;&#xff09;&#xff1a;获得姓名&#xff1b;
getSex&#xff08;&#xff09;&#xff1a;获得性别&#xff1b;
getAge&#xff08;&#xff09;获得年龄&#xff1b;
getJava&#xff08;&#xff09;&#xff1a;获得Java 课程成绩
&#xff08;4&#xff09;根据类Student的定义&#xff0c;创建五个该类的对象&#xff0c;输出每个学生的信息&#xff0c;计算并输出这五个学生Java语言成绩的平均值&#xff0c;以及计算并输出他们Java语言成绩的最大值和最小值。
public class Student {String sNO,sName,sSex;int sAge,sJava;public Student(String XH,String XM,String XB,int NL,int XF) {super();sNO&#61;XH;sName&#61;XM;sSex&#61;XB;sAge&#61;NL;sJava&#61;XF; }public String getNO() {return sNO;}public String getName() {return sName;}public String getSex() {return sSex;}public int getAge() {return sAge;}public int getJava() {return sJava;}public static void main(String[] args) {Student[] st&#61;new Student[5];st[0]&#61;new Student("09zc01","张三","男",19,94);st[1]&#61;new Student("09zc02","李四","男",20,85);st[2]&#61;new Student("09zc03","王五","女",18,96);st[3]&#61;new Student("09zc04","赵六","男",17,90);st[4]&#61;new Student("09zc05","杨七","女",21,88);int max&#61;0,min&#61;100,sum&#61;0;System.out.println(" 学生信息&#xff1a;");for (int i&#61;0;i<st.length;i&#43;&#43;) {if (st[i].sJava < min)min&#61;st[i].sJava;if (st[i].sJava > max)max&#61;st[i].sJava;sum&#61;sum&#43;st[i].sJava;System.out.println("学生编号&#xff1a;"&#43;st[i].getNO()&#43;"&#xff0c; 姓名&#xff1a;"&#43;st[i].getName()&#43;"&#xff0c; 性别&#xff1a;"&#43;st[i].getSex()&#43;"&#xff0c; 年龄&#xff1a;"&#43;st[i].getAge()&#43;"&#xff0c; Java课学分&#xff1a;"&#43;st[i].getJava());}System.out.println();System.out.println(" 共有学生&#xff1a;"&#43;st.length&#43;"&#xff0c; 平均成绩&#xff1a;"&#43;sum/st.length);System.out.println(" 最小学分&#xff1a;"&#43;min&#43;"&#xff0c; 最大学分&#xff1a;"&#43;max);}
}
8.JDBC数据库操作模块
import java.sql.*;
public class SqlTest {static Connection connect;static Statement st;public static void main(String[] args) {String url&#61;"jdbc:sqlserver://localhost:1433;DatabaseName&#61;JXGL;";try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");connect&#61;DriverManager.getConnection(url,"sa","sa");st&#61;connect.createStatement(); ResultSet rs&#61;st.executeQuery("select * from S"); while(rs.next()){String sno&#61;rs.getString(1);String name&#61;rs.getString(2);int age&#61;rs.getInt(4);System.out.println(sno&#43;" "&#43;name&#43;" "&#43;age);}}catch (SQLException e1) {e1.printStackTrace();System.out.println("连接失败&#xff01;");}finally{try {if(st!&#61;null){st.close();}if(connect!&#61;null){connect.close();}}catch (Exception e2){}}}
}