1、编写一个成绩统计程序,该程序可以找出一个班JAVA课程考试的最高分、最低分和全班平均分。课程成绩用一个数组表示。
import java.util.*;
public class Score {
public static void main(String[] args) {
// Scanner input = new Scanner(System.in);
int[] score = {10,11,12};
int max;
int min;
int avrege;
// for (int i = 0; i < 3; i ) {// 输入3个数的循环
// score[i] = input.nextInt();
// }
max = score[0];
min = score[0];
avrege = 0;
for (int index = 1; index < 3; index ) {
if (score[index] > max) {
max = score[index];
}
if (score[index] < min) {
min = score[index];
}
avrege = score[index];
}
System.out
.println("最高分为:" max "最低分为:" min "平均分为:" avrege / 3);
}
}
2、编写一个成绩统计程序,该程序可以统计一个班JAVA课程考试优秀(85分以上)、及格(60分到85分)和不及格(60分以下)的人数,并计算全部的平均分。课程成绩用一个数组表示。
import java.util.*;
public class Score {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] score = new int[3];
int excellen;
int pass;
int uppass;
int avrege;
System.out.println("请输入成绩:");
for (int i = 0; i < 3; i ) {// 输入3个数的循环
score[i] = input.nextInt();
}
excellen = 0;
pass = 0;
uppass=0;
avrege=0;
for (int index = 0; index < 3; index ) {
if (score[index] > 85) {
excellen ;
}
else if (score[index] <= 85 && score[index]>=60) {
pass ;
}else{
uppass ;
}
avrege =score[index];
}
System.out.println("85分以上人数为:" excellen "及格人数为:" pass "不及格人数为:" uppass "平均分为:" avrege/30);
}
}
3. 编写一个数组倒置程序,该程序将原数组中各元素倒置,第一个元素移到最后一位,最后一个移到第一个元素,第二个元素移到倒数第二位,倒数第二个元素移到第二位,依次类推。
import java.util.*;
public class Score {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] value = new String[30];
String temp = "";
System.out.println("请输入元素值:");
for (int i = 0; i < 30; i ) {// 输入30个数的循环
value[i] = input.next();
}
for (int i = 0; i < 30; i ) {// 输入30个数的循环
System.out.println("元素内容为:" value[i]);
}
System.out.println("" 30 / 2);
for (int index = 0; index <= 30 / 2; index ) {
temp = value[index];
value[index] = value[29 - index];
value[29 - index] = temp;
}
for (int i = 0; i < 30; i ) {// 输入30个数的循环
System.out.println("元素倒置后为:" value[i]);
}
}
}
4. 定义一个类——人(Person),该类有两个私有成员:name和address,四个公有成员函数:
void setName(Strng n)设置name值、void setaddress(String a)设置address值、String getAddress获取address值和String getName获取name值,一个无参构造函数:Person()。
public class Person {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5. 定义一个类——圆(Circle),该类有一个私有成员:r,一个公有成员函数:float area(float r)计算圆的面积。(∏为:3.14),一个有参构造函数Circle(float r)。用该类生成一个r为2的圆的对象,并计算和显示该圆的面积。
public class Circle {
float r;
final double PI = 3.14159;
public double area() // 计算面积
{
return PI * r * r;
}
public void setR(float x) // 设置半径
{
r = x;
}
public double perimeter() // 计算周长
{
return 2 * PI * r;
}
public static void main(String args[]) {
double x, y;
Circle cir = new Circle(); // 创建Circle1类的对象cir
cir.setR(12.35f); // 引用cir对象的setR()方法
x = cir.area(); // 引用cir对象的area()方法
y = cir.perimeter(); // 引用cir对象的perimeter()方法
System.out.println("圆面积=" x "\n圆周长=" y);
}
}
6. 有一个类——人(Person),该类的定义如下:
Public class Person {
Public String id , name;
Person(String id, String name){this.id=id; this.name=name;}
}
要求定义一个类——学生(Student),学生类继承人类,并增加一个公有成员:school,增加一个公有成员函数:void show(Student s )显示学生的id,name,school,一个有参构造函数
Student(String id, String name, String school)。
public class Person {
public String id;
public String name;
public Person(String id, String name){
this.id=id;
this.name=name;
}
}
public class Student extends Person {
public String school;
public Student(String id, String name,String school) {
super(id, name);
this.school =school;
// TODO Auto-generated constructor stub
}
public void show(Student s) {
System.out.print("id=" id "," "name=" name "," "school " school);
}
public static void main(String[] args) {
Student s = new Student("1","ABc","学校");
s.show(s);
}
}
7、编写两个JSP文件:First.jsp和Second.jsp,在First.jsp文件中通过表单向Second.jsp提交了name和password,要求在Second.jsp中获取name和password,并在页面中显示name和password。
First.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>提交显示页面</title>
</head>
<body>
<P>获取name信息:
<% String name1=request.getParameter("name");
%>
<%=name1%>
<P>获取address信息:
<% String address1=request.getParameter("address");
%>
<%=address1%>
</body>
</html>
Second.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>提交页面</title>
</head>
<body>
<form action="show.jsp">
<center>
<table border="1">
<tr>
<td>NAME</td>
<td> <input> </td>
</tr>
<tr>
<td>ADDRESS</td>
<td> <input> </td>
</tr>
<tr>
<td > <input value="SUBMIT"> </td>
<td> <input value="RESET"> </td>
</tr>
</table>
</center>
</body>
</html>
8、编写JSP页面,利用application对象实现页面计数。
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>JavaBean应用示例</title>
</head>
<body>
<jsp:useBean id="bean1" scope="application" class="ch7.Counter" />
<%
out.println("当前的计数为:" bean1.getCount() "<br>");
%>
</body>
</html>
package ch7;
public class Counter
{
private int count;
public Counter()
{
count = 0;
}
public int getCount()
{
count ;
return count;
}
public void setCount(int value)
{
count = value;
}
}
9、利用第4题中编写的Person类,采用JavaBean技术,编写两个JSP页面:Third.jsp和Fourth.jsp,在Third.jsp页面中通过表单向Fourth.jsp页面提交name和address信息,在Fourth.jsp中将获取的信息赋值给一个JavaBean实例,并在页面中显示该信息。
10、编写一个JSP页面,在页面中访问SQL SERVER 2000中的student数据库中的info表,将表中数据显示出来。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
Connection con=DriverManager.getConnection(url);
Statement stmt=con.createStatement();
String queryStr="SELECT * FROM info";
ResultSet rs=stmt.executeQuery(queryStr)
while(rs.next())
{System.out.println(rs.getString("*"));
rs.close();
stmt.close();
con.close();
}
%>
</body>
</html>
11、编写一个JSP页面,在页面中向SQL SERVER 2000中的student数据库中的info表中插入一条记录,该记录包含id,name和address字段信息。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
Connection con=DriverManager.getConnection(url);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(queryStr)
stmt.executeUpdate(“insert into info(id,name,address)”)
stmt.close();
con.close();
%>
</body>
</html>
12、编写一个JSP页面,在页面中根据给定的id值,将SQL SERVER 2000中的student数据库中的info表中该学生信息删除。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
Connection con=DriverManager.getConnection(url,user,password);
Statement stmt=con.createStatement();
String queryStr="SELECT * FROM info";
ResultSet rs=stmt.executeQuery(queryStr)
stmt.executeUpdate(“delete from info where. getString("id"))”)
stmt.close();
con.close()
%>
</body>
</html>


