一.
二.
例2-1
例2-2
例2-3
例2-4
例2-5
三.
例3-1
例3-2
例题3-3
例3-4
例3-5
例3-6
例3-7
例3-8
Class A
{
void f(){
System.out.println("I am A");
}
}
Class B
{
public class Hello
{
public static void main(Strin[] args)
{
System.out.println("你好,很高兴学习Java")
A a=new A();
a.f();
}
}
}
public class Tom{
int leg;
String head;
void cry(String s)
{
System.out.println(s);
}
}
class Example{
public static void main(String[] args)
{
Tom cat;
cat=new Tom();
cat.leg=4;
cat.head="猫头"
System.out.println("腿"+cat.leg+"条");
System.out.println("头"+cat.head);
cat.cry("我今天要和Jerry拼了";)
}
}
二.
例2-1
public class Exemple 2_1{
public static void main(String args[]){
char c='a';
System.out.println("字母"+c+"在unicode表中的顺序位置:"+(int)c);
System.out.println("字母表");
for(int i=(int)c;i<c+25;i++)
{
System.out.print(""+(char)i);
}
}
}
例2-2
public class Example2_2{
public static void main(String args[])
{
byte a=120;
short b=130;
int c=2000;
long d=8000;
float f;
double g=0.1234567812345678;
a=(byte)b;
c=(int)d;
f=(float)g;
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("d="+d);
}
}
例2-3
public class Example2_3{
public static void main()
{
char c='A';
float f=123.456789f;
double d=123456.12345678;
long x=5678;
System.out.printf("%c%n%103f%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.println("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.println("a[0]=%-5da[1]=%-5da[2]=%-5d\n",a[0],a[1],a[2]);
System.out.println("b[0]=%-5db[1]=%-5db[2]=%-5d\n",b[0],b[1],b[2]);
}
}
三.
例3-1
import java.util.*
public class Example3-1
{
public 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(){
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++)
{
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&&v+c>a)
{
if(a*a==b*b+c*c||b*b==a*a+c*c||c*c=a*a+b*b)
{
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<a*a+b*b)
{
System.out.printf("%-8.3f%-8,3f%-8.3f构成锐角三角形",a,b,c);
}
else
}
}
}
例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){
case1 :
case2 :
case3 :System.out.printf("%d月属于第一季度",n);
break;
case4 :
case5 :
case6 :System.out.printf("%d月属于第二季度",n);
break;
case7 :
case8 :
case9 :System.out.printf("%d月属于第三季度",n);
break;
case10 :
case11 :
case12 :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<=j/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+...n<"+max+"最大整数n是:"+(i-1););
for(i=1,max=200,sum=0;i<=max;i++){
if(i%number!=0){
continue;
}
sum=sum+1;
}
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+"个元素");
}
}