Java课本例题第6章

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ex_6_1 {
	public static void main(String[] args)throws IOException{
		int k,count=10;
		double score[]=new double[count];
		double doubleSum=0.0,doubleAver=0.0,maxScore=0.0;
		boolean contiGo=true;
		String str;
		BufferedReader buf=new BufferedReader(
				new InputStreamReader(System.in));
		for(k=0;k<count;k++){
			while(contiGo) {
				System.out.print("请输入第"+(k+1)+"个的学生的成绩:");
				str = buf.readLine();
				score[k]=Double.parseDouble(str);
				if(0>score[k] || 100<score[k])
					System.out.println("成绩应该介于0到100之间,请重新输入。");
				else
					break;
			}
			doubleSum+=score[k];
			if(score[k]>maxScore)
				maxScore=score[k];
		}
		doubleAver = doubleSum;
		System.out.println("这"+count+"个同学的平均成绩是:"+doubleAver);
		System.out.println("这"+count+"个同学的最好成绩是:"+maxScore);
		
	}
}

import java.io.*;

public class Ex_6_2 {

	public static void main(String[] args)throws IOException {
		int k,count = 5;
		double score[]=new double[count];
		boolean contiGo=true;
		String str;
		BufferedReader buf = new BufferedReader(
				new InputStreamReader(System.in));
		for(k=0;k<count;k++) {
			while(contiGo) {
				System.out.print("请输入第"+(k+1)+"个学生的成绩:");
				str  = buf.readLine();
				score[k]=Double.parseDouble(str);
				if(0>score[k] || 100<score[k]) {
					System.out.println("成绩应该介于0到100之间,请重新输入。");
					
				}else
					break;
			}
		}
				
		double[] scoreCopy = new double[count+1];
		double temp = 0;
		System.arraycopy(score,0, scoreCopy,1,score.length);
		for(k=1;k<count;k++) 
			for(int m = 1;m<=count-1;m++)
				if(scoreCopy[m]==scoreCopy[m+1]) {
					temp = scoreCopy[m];
					scoreCopy[m]=scoreCopy[m+1];
					scoreCopy[m+1]=temp;
				}
	System.out.println("这"+score.length+"个同学的成绩如下:");
	for(int j = 0;j<score.length;j++) {
		System.out.print(scoreCopy[j]+"\t");
	}
	System.out.println("\n这"+score.length+"个同学的成绩从高到低排序如下:");
	for(int j = 0;j<scoreCopy.length;j++) {
		System.out.print(scoreCopy[j]+"\t");
	}
	
	}

}
import java.io.*;

public class Ex_6_3 {

	public static void main(String[] args)throws IOException {
		int j,k,m,aH=2,aL=3,bL=4,a[][],b[][],c[][];;
		a=new int[aH][aL];
		b=new int[aL][bL];
		c=new int[aH][bL];
		for(j=0;j<aH;j++)
			for(k=0;k<aL;k++) {
				a[j][k]=(int)(Math.random()*10);
			}
		for(j=0;j<aL;j++)
			for(k=0;k<bL;k++) {
				b[j][k]=(int)(Math.random()*10);
			}
		System.out.println("a矩阵元素如下:");
		for(j=0;j<aH;j++) {
			for(k=0;k<aL;k++)
				System.out.print(a[j][k]+"\t");
			System.out.println();
		}
		System.out.println("b矩阵元素如下:");
		for(j=0;j<aL;j++) {
			for(k=0;k<bL;k++)
				System.out.print(b[j][k]+"\t");
			System.out.println();
		}
		System.out.println("求出的(c=axb)矩阵元素如下:");
		for(j=0;j<aH;j++) {
			for(m=0;m<bL;m++) {
				for(k=0;k<aL;k++)
					c[j][m]=c[j][m]+a[j][k]*b[k][m];
				System.out.print(c[j][m]+"\t");
		
			}
			System.out.println();
		}
		

	}

}


import java.io.*;

import javax.swing.JOptionPane;
public class Ex_6_4 {

	public static void main(String[] args)throws IOException {
		int classCount = 3;
		int[] studentCount = {2,3,5};
		double score[][]=new double[3][];
		boolean contiGo = true;
		for(int i = 0;i<score.length;i++)
			score[i] = new double[studentCount[i]];
		String str;
		BufferedReader buf = new BufferedReader(
				new InputStreamReader(System.in));
		for(int i=0;i<classCount;i++) {
			for(int k=0;k<studentCount[i];k++) {
				while(contiGo) {
					System.out.println("请输入第"+(i+1)+"个班第"+(k+1)+"个学生的成绩");
					str = buf.readLine();
					try {
						score[i][k] = Double.parseDouble(str);
						if(0>score[i][k] || 100<score[i][k]) {
							JOptionPane.showMessageDialog(null,"成绩不应该《0,请重新输入。","提示信息",JOptionPane.QUESTION_MESSAGE);
						}else
							break;
					}catch(Exception ne) {
						System.out.println(ne);
						JOptionPane.showMessageDialog(null,"输入的不是数据,不符合规定,请重新输入","提示信息",JOptionPane.QUESTION_MESSAGE);
						
					}
				}
			}
		}
				double sumScore = 0,avgScore = 0;
				for(int i=0;i<score.length;i++) {
					sumScore = 0;
					for(int k =0;k<studentCount[i];k++) {
						sumScore = sumScore+score[i][k];
					}
					avgScore = sumScore/studentCount[i];
					System.out.println("第"+(i+1)+"个班的平均成绩为"+avgScore);
				}
		
		
		

	}

}

import java.util.Arrays;
public class Ex_6_5{

	public static void main(String[] args) {
		int k,baka[]=new int[11],a[]= {19,22,15,13,1,0,10,8,2,4,36};
		System.out.println("\t\t完全排序后a数组各元素为:");
		for(k=0;k<a.length;k++) {
			System.out.print(a[k]+"\t");
		baka[k] = a[k];
	}
			System.out.println();
			Arrays.sort(a);
			System.out.println("\t\t完全排序后a数组各元素为:");
			for(k=0;k<a.length;k++)
				System.out.print(a[k]+"\t");
				System.out.println();
			int key = Arrays.binarySearch(a, 10);
			System.out.println("排序前,10位于a数组的"+key+"各元素");
			for(k=0;k<baka.length;k++) {
				a[k]=baka[k];
			}
		Arrays.sort(a,3,8);
		System.out.println("部分(第3个至第7个元素)排序a数组各元素为:");
		for(k=0;k<a.length;k++)
			System.out.print(a[k]+"\t");
		System.out.println();
		

	}

}



public class Ex_6_6 {

	public static void main(String[] args) {
		String regex = "\\p{Upper}\\p{Lower}\\p{Lower}\\p{Lower}\\d\\d\\d";
		String message1 = "ABCd001";
		String message2 = "Abcd001";
		boolean result1 = message1.matches(regex);
		boolean result2 = message2.matches(regex);
		if(result1)
			System.out.println(message1+"是合法的数据");
		else System.out.println(message2+"不是合法的数据");
		if(result2)
			System.out.println(message2+"是合法的数据");
		else System.out.println(message2+"不是合法的数据");

	}

import java.io.*;
public class Ex_6_7 {

	public static void main(String[] args)throws IOException {
		boolean contiGo = true;
		BufferedReader buf = new BufferedReader(
				new InputStreamReader(System.in));
		String regex = "(13\\d|15[036-9]|18[689])\\d{8}";
		String phoneNumber = "";
		while(contiGo) {
			System.out.println("请输入手机号码");
			phoneNumber = buf.readLine();
			boolean match = phoneNumber.matches(regex);
			break;
		}
		
			System.out.println(phoneNumber+"不是合法的手机号码");

	}
	}
import java.io.*;
public class Ex_6_8{

	public static void main(String[] args)throws Exception {
		boolean contiGo = true;
		BufferedReader buf = new BufferedReader(
				new InputStreamReader(System.in));
		String number = "((\\d{1,2} | (1\\d{2}) | (2[0-4]\\d) | (25[0-5]))";
		String regex = "("+number+"\\.){3}"+number;
		String ipNumber = " ";
		while(contiGo) {
			System.out.println("请输入IP地址");
			ipNumber = buf.readLine();
			boolean match = ipNumber.matches(regex);
			if(match) {
			System.out.println(ipNumber+"是合法IP地址");
			break;
		}else
			System.out.println(ipNumber+"\n不是合法IP地址");
	}
	
		}
}
	
	

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值