Java大学实用教程(第4版)课本例题

//1-1
class A{
	void f() {
		System.out.println("I am A");
	}
}
class B{}


public class Hello {
	public static void main(String args[]) {
		System.out.println("你好,很高兴学习Java");
		A a=new A();
		a.f();
	}

}

在这里插入图片描述

//2-1
public class Example2_1 {
	public static void main(String args[]) {
		char c='k';
		System.out.println("字母"+c+"在Unicode表中的顺序:"+(int)c);
		System.out.println("字母表:");
		for(int i=(int)c;i<c+25;i++) {
			System.out.println(" "+(char)i);
		}
	}

}

在这里插入图片描述

//2-2


public class Example2_2 {
	public static void main(String args[]) {//low //byte short int long float double//high
		//low>high auto
		//high>low usethe tool
		byte a=120;
		short b=130;
		int c=2200;
		long d=8000;
		float f;
		double g=0.12345678;
		a=(byte)b;//becaus the byte a lower than the short b, b turn into the a needs a tool
		c=(int )d;//becaus the int c lower than the long d, d turn into the c needs a tool
		f=(float)g;
		System.out.println("a="+a);
		System.out.println("c="+c);
		System.out.println("f="+f);
		System.out.println("g="+g);
	}

}

在这里插入图片描述

//2-3
public class Example2_3 {
	public static void main(String args[]) {
		char c='A';
		float f=123.456789f;
		double d =123456.12345678;
		long x=5678;
		System.out.printf("%c%n%10.3f%n%f,%12d%n%d",c,f,d,x,x=x+2);
	}

}

在这里插入图片描述

//2-4
import java.util.*;
public class Example2_4 {
	public static void main(String args[]) {
		Scanner reader = new Scanner (System.in );
		double sum=0;
		int m=0;
		while (reader.hasNextDouble()) {
			double x=reader.nextDouble();
			m=m+1;
			sum=sum+x;
		}
		System.out.printf("%d个数的和是%f\n",m,sum);
		System.out.printf("%d个数的平均值是%f\n",m,sum/m);
	}

}

在这里插入图片描述

//2-5
public class Example2_5 {
	public static void main(String args[]) {
		int [] a= {1,2,3};
		int [] b= {10,11};
		System.out.println("数组a引用的是:"+a);
		System.out.println("数组b引用的是:"+b);
		System.out.printf("b[0]=%-3db[1]=%-3d\n",b[0],b[1]);
		b=a;
		System.out.println("数组a引用的是:"+a);
		System.out.println("数组b引用的是:"+b);
		b[1]=888;
		b[2]=999;
		System.out.printf("a[0]=%-5da[1]=%-5da[2]=%-5d\n",a[0],a[1],a[2]);
		System.out.printf("b[0]=%-5db[1]=%-5db[2]=%-5d\n",b[0],b[1],b[2]);
	}
}

在这里插入图片描述

//3-1
import java.util.*;
public class Example3_1 {
	public static void main(String args[]) {
		Scanner reader= new Scanner(System.in);
		System.out.println("输入待移位的int型整数:");
		int x=reader.nextInt();
		System.out.println("输入移位量:");
		int n=reader.nextInt();
		System.out.println("左移位的结果:"+(x<<n));
		System.out.println("右移位的结果:"+(x>>n));
		}

}

在这里插入图片描述

//3-2
public class Example3_2 {
	public static void main(String args[]) {
		char a[]= {'金','木','水','火','土'};
		char secret='z';
		for(int i=0;i<a.length;i++) {
			a[i]=(char) (a[i]^secret);
			
		}
		System.out.printf("密文:\n");
		for (int i=0;i<a.length;i++) {
			System.out.printf("%3c",a[i]);
		}
		for(int i=0;i<a.length;i++) {
			a[i]=(char)(a[i]^secret);
		}
		System.out.printf("\n原文:\n");
		for(int i=0;i<a.length;i++) {
			System.out.printf("%3c",a[i]);
		}
	}

}

在这里插入图片描述

//3-3
import java.util.*;
public class Example3_3{
	public static void main(String args[]) {
		Scanner reader=new Scanner(System.in);
		double a=0,b=0,c=0;
		System.out.print("输入边a:");
		a=reader.nextDouble();
		System.out.print("输入边b:");
		b=reader.nextDouble();
		System.out.print("输入边c:");
		c=reader.nextDouble();
		if(a+b>c&&a+c>b&&b+c>a) {
			if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==b*b+a*a) {
				System.out.printf("%-8.3f%-8.3f%-8.3f构成直角三角形",a,b,c);
			}
			else if(a*a<b*b+c*c&&b*b<a*a+c*c&&c*c<b*b+a*a) {
				System.out.printf("%-8.3f%-8.3f%-8.3f构成锐角三角形",a,b,c);
			}
			else {
				System.out.printf("%-8.3f%-8.3f%-8.3f构成钝角三角形",a,b,c);
			}
		}
		else {
			System.out.printf("%f%f%f不构成三角形",a,b,c);
		}
	}
}

在这里插入图片描述

//3-4
import java.util.*;
public class Example3_4 {
	public static void main(String args[]) {
		Scanner reader=new Scanner(System.in);
		System.out.println("输入一个月份:");
		int n=reader.nextInt();
		switch(n) {
		case 1:
		case 2:
		case 3: System.out.printf("%d月属于第一季度",n);
		break;
		case 4:
		case 5:
		case 6: System.out.printf("%d月属于第二季度",n);
		break;
		case 7:
		case 8:
		case 9: System.out.printf("%d月属于第三季度",n);
		break;
		case 10:
		case 11:
		case 12: System.out.printf("%d月属于第四季度",n);
		break;
		default :System.out.printf("%d不代表月份",n);
		}
		}

}

在这里插入图片描述

//3-5

public class Example3_5 {
	public static void main(String args[]) {
		double sum=0, item=1;
		int i=1;
		while (i<=1000) {
			sum=sum+item;
			i++;
			item=item*(1.0/i);
		}
		sum =sum+1;
		System.out.println("e="+sum);
		sum=0;
		i=1;
		item=1;
		do {
			sum=sum+item;
			i++;
			item=item*(1.0/i);
			
		}while(i<=1000);
		sum=sum+1;
		System.out.println("e="+sum);
	}

}

在这里插入图片描述

//3-6

public class Example3_6 {
	public static void main(String args[]) {
		int sum,i,j;
		for(i=1;i<=1000;i++) {
			for (j=1,sum=0;j<=i/2;j++) {
				if(i%j==0) {
					sum=sum+j;
					
				}
			}
			if (sum==i) {
				System.out.printf("%8d是一个完数%n",i);
			}
		}
	}

}

在这里插入图片描述

//3-7

public class Example3_7 {
	public static void main(String args[]) {
		int sum=0, i=0, max=8888, number=7;
		while(true) {
			i++;
			sum=sum+i;
			if (sum>=max)
				break;
		}
		System.out.println("1+2+3..+n<"+max+"的最大整数n是:"+(i-1));
		for(i=1,max=200,sum=0;i<=max;i++) {
			if (i%number!=0) {
				continue;
			}
			sum=sum+i;
			
		}
		System.out.println(max+"内能被"+number+"整除的数字之和:"+sum);
	}

}

在这里插入图片描述

//3-8
import java.util.Scanner;
public class Example3_8 {
	public static void main(String args[]) {
		int n,start,end,middle;
		int a[]= {-2,1,4,5,8,12,17,23,45,56,90,100};
		start=0;
		end =a.length;
		middle=(start+end)/2;
		int count=0;
		Scanner reader=new Scanner(System.in);
		System.out.print("请输入一个整数");
		n=reader.nextInt();
		while (n!=a[middle]) {
			if (n>a[middle]) {
				start=middle;
			}
			else if(n<a[middle]) {
				end = middle;
			}
			middle=(start+end)/2;
			count++;
			if(count>a.length/2)
				break;
			
		}
		if (count>a.length/2)
			System.out.println(n+"不在数组中");
		else
			System.out.println(n+"是数组中的第"+middle+"个元素");
	}

}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值