第三次Java练习题分解 下载本文

内容发布更新时间 : 2024/6/18 21:23:46星期一 下面是文章的全部内容请认真阅读。

1. 顺序语句练习

6道题.方法1道.数组2道。类2到 建项目Test+学号 每个题一个包

把每个项目压缩 专业班级学号姓名 提交

时间 3小时

数组: 做题号为偶数的 去掉柜子 八皇后 方法 :1 3 11 13 14 16

类: 1 5 4 6 7 8 9 14 15 16 异常:

.循环练习

public class lx5 { public static void main(String args[]){ System.out.println(\以内的素数:\ System.out.print(\ int c=2; for(int i=3;i<=100;i++) for(int j=2;j<=(int)Math.sqrt(i);j++){ if(i%j==0)break; if(j>=(int)Math.sqrt(i)){ System.out.print(i+\ c++; if(c==0) System.out.println(); } } } }

6.(统计正数和负数的个数然后计算这些数的平均值)编写程序,读入未指定个数的整数,分别判断读入的正数和负数的个数,然后计算这些输入值的总和及其平均值(不对0计数),当输入为0时候,表示程序结束。将平均值以浮点数的形式显示。(20分) 下面是一个运行示例:

Enter an int value, the program exits if the input is 0: 1 2 -1 3 0

The number of positives is 3 The number of negatives is 1 The total is 5

The average is 1.25

7.(财务程序) :假设某个大学今年的学费是10000元,学费以每年5%速度增加,编写程序

计算10年后的学费以及现在开始的10年后算起,4年内总学费是多少?(20分)

public static void main(String[] args) { }

}

int a=10000;

double w; double sum=0; double p;

for(int n=1;n<10;n++){ }

w=a*Math.pow(1+0.05,n); sum+=w; if(n==9){ }

if(n==3){ }

p=a+sum;

System.out.println(\+p); System.out.println(\+w);

8.编写程序,提示用户输入学生的个数,每个学生的姓名和分数,最后显示得分最高学生的

姓名和分数和第二高分学生姓名和分数(20分) import java.io.*; import java.util.*;

public class lianxi50 {

public static void main(String[] args){ Scanner ss = new Scanner(System.in); String [][] a = new String[5][6]; for(int i=1; i<6; i++) {

System.out.print(\请输入第\个学生的学号:\ a[i-1][0] = ss.nextLine();

System.out.print(\请输入第\个学生的姓名:\ a[i-1][1] = ss.nextLine(); for(int j=1; j<4; j++) {

System.out.print(\请输入该学生的第\个成绩:\ a[i-1][j+1] = ss.nextLine(); }

System.out.println(\

9.使用while循环找出满足n*n大于12000的最小整数(15分)。

public static void main(String[] args) {

int n=1;

while(n*n <12000){

} } }

System.out.println(\+n); n++;

11. 计算π(25分)

使用下列数列可以近似计算π:

π= 4*(1-1/3 +1/5 -1/7 + 1/9 -1/11+………+1/(2*i-1)-1/2*i+1) 编写程序,显示当i=10000,20000,…..100000的时候π的值。

12.(完全数)如果一个正整数等于除以它本身之外所有其他所有除数之和,就称之为完全数。例如:6是第一个完全数,因为 6 = 1+ 2 + 3, 下一个完全数是28 = 1 + 2 + 4 + 7 + 14;1000以下完全数有4个,编写程序找出这四个完全数。(25分)

public static void main(String[] args) { }

int sum;

for(int i=1;i<1000;i++) { }

sum=0;

for(int j=1;j<=i/2;j++) { }

if(i==sum){ } }

System.out.println(\+i); if(i%j==0) { }

sum+=j;

13.(游戏:石头、剪、子布)对于分支语句练习7的石头-剪子-布程序,修改这个程序,让用户可以连续玩这个游戏,直到用户或者计算机连续赢两次以上。(25分)

14.(最大数的出现次数)编写程序读取整数,找出他们的最大数,然后计算该数出现的次数。键入输入0表示结束。(20分)假定输入是3 5 2 5 5 5 0,程序找出最大数5,而5 出现的次