1. 输出char的Unicode值

    public class E {
    public static void main(String args[]) {
    System.out.println((int)'你');
    System.out.println((int)'我');
    System.out.println((int)'他');
    }
    }
  2. 输出α到ω的字符

public class E {
public static void main (String args[ ]) {
char cStart='α',cEnd='ω';
for(char c=cStart;c<=cEnd;c++)//char值++
System.out.print(" "+c);//转为String

//方法2
char cStart0='α';
for(int i=0;i<=24;i++)
System.out.println(""+(char)(cStart0+i));
//同理
for(int i=0;i<=24;i++)
System.out.println(""+(char)(945+i));
}
}
}
}

  1. 求1!+2!+…+20!
public class Xiti1 {
public static void main(String args[]) {
long sum=0,a=1;
int i=1;
while(i<=20) {
sum=sum+a;//主要目的
i++;
a=a*i;//一次循环一次自乘,求n!
}
System.out.println("sum="+sum);

//推荐
long sum1=0,a1=1;
for(int j=1;j<=10;j++,a1*=j)
sum1+=a1;

System.out.println("sum="+sum1);


long sum1=0;//配合函数计算,更加灵活但是效率降低
for(int j=1;j<=10;j++)
sum1+=fun(j);
System.out.println("sum1="+sum1);

}

static int fun(int i) {
return i>=2?fun(i-1)*i:1;
}
}
  1. 求100内的素数
public class Xiti2 {
public static boolean sushu(int i) {
if(i<=2) return i==2:true?false;
else {
for(int j=3;j<=i;j+=2) {
if(i%j==0)
break;
else if(i==j)
return true;
}
return false;
}
}
public static void main(String args[]) {
for(j=2;j<=100;j++)
if(sushu(j))//是否是素数
System.out.println(j);
}
}
  1. 分别用do-while,for求1+1/2!+1/3!+…1/20!
class Xiti3 {
public static void main(String args[]) {
double sum=0,a=1,i=1;
do {
sum=sum+a;
i++;
a=(1.0/i)*a;
}
while(i<=20);
System.out.println("使用do-while循环计算的sum="+sum);
//
for(sum=0,i=1,a=1;i<=20;i++) {
a=a*(1.0/i);
sum=sum+a;
}
System.out.println("使用for循环计算的sum="+sum);
}
}
  1. 1000内的完数
public class Xiti4 {
public static void main(String args[]) {
int sum=0,i,j;
for(i=1;i<=1000;i++) {
for(j=1,sum=0;j<i;j++) {
if(i%j==0)
sum+=j;
}
if(sum==i)
System.out.println("完数:"+i);
}
}
}
  1. for求8+88+888+…前10项
public class Xiti5 {
public static void main(String args[]) {
int m=8,item=m,i=1;
long sum=0;
for(i=1,sum=0,item=m;i<=10;i++) {
sum=sum+item;
item=item*10+m;
}
System.out.println(sum);
}
}
  1. 求1+2+3+…+n<8888的最大正整数n
public class Xiti6 {
public static void main(String args[]) {
int n=1;
long sum=0;
while(true) {
sum=sum+n;
n++;
if(sum>=8888)
break;
}
System.out.println("满足条件的最大整数:"+(n-1));
}
}

  • 类与对象
//CPU.java

public class CPU {
int speed;
int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}

//HardDisk.java

public class HardDisk {
int amount;
int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}

//PC.java

public class PC {
CPU cpu;
HardDisk HD;
void setCPU(CPU cpu) {
this.cpu = cpu;
}
void setHardDisk(HardDisk HD) {
this.HD = HD;
}
void show(){
System.out.println("CPU速度:"+cpu.getSpeed());
System.out.println("硬盘容量:"+HD.getAmount());
}
}

//Test.java

public class Test {
public static void main(String args[]) {
CPU cpu = new CPU();
HardDisk HD=new HardDisk();
cpu.setSpeed(2200);
HD.setAmount(200);
PC pc =new PC();
pc.setCPU(cpu);
pc.setHardDisk(HD);
pc.show();
}
}

  • 继承/接口
//Animal.java

public abstract class Animal {
public abstract void cry();
public abstract String getAnimalName();
}

//Simulator.java

public class Simulator {
public void playSound(Animal animal) {
System.out.print("现在播放"+animal.getAnimalName()+"类的声音:");
animal.cry();
}
}

//Dog.java

public class Dog extends Animal {
public void cry() {
System.out.println("汪汪...汪汪");
}
public String getAnimalName() {
return "狗";
}
}

//Cat.java

public class Cat extends Animal {
public void cry() {
System.out.println("喵喵...喵喵");
}
public String getAnimalName() {
return "猫";
}
}

//Application.java

public class Example5_13 {
public static void main(String args[]) {
Simulator simulator = new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
}

  • //Animal.java

    public interface Animal {
    public abstract void cry();
    public abstract String getAnimalName();
    }

    //Simulator.java

    public class Simulator {
    public void playSound(Animal animal) {
    System.out.print("现在播放"+animal.getAnimalName()+"类的声音:");
    animal.cry();
    }
    }

    //Dog.java

    public class Dog implements Animal {
    public void cry() {
    System.out.println("汪汪...汪汪");
    }
    public String getAnimalName() {
    return "狗";
    }
    }

    //Cat.java

    public class Cat implements Animal {
    public void cry() {
    System.out.println("喵喵...喵喵");
    }
    public String getAnimalName() {
    return "猫";
    }
    }

    //Application.java

    public class Example5_13 {
    public static void main(String args[]) {
    Simulator simulator = new Simulator();
    simulator.playSound(new Dog());
    simulator.playSound(new Cat());
    }
    }

  • import java.util.*;
    public class E {
    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();
    assert x< 100:"数据不合理";
    m = m+1;
    sum = sum+x;
    }
    System.out.printf("%d个数的和为%f\n",m,sum);
    System.out.printf("%d个数的平均值是%f\n",m,sum/m);
    }
    }

  1. public class E {
    public static void main (String args[ ]) {
    String s1,s2,t1="ABCDabcd";
    s1=t1.toUpperCase();
    s2=t1.toLowerCase();
    System.out.println(s1);
    System.out.println(s2);
    String s3=s1.concat(s2);
    System.out.println(s3);
    }
    }
public class E {
public static void main (String args[ ]) {
String s="ABCDabcd";
char cStart=s.charAt(0);
char cEnd = s.charAt(s.length()-1);
System.out.println(cStart);
System.out.println(cEnd);
}
}
  1. import java.util.*;
    public class E {
    public static void main (String args[ ]) {
    int year1,month1,day1,year2,month2,day2;
    try{ year1=Integer.parseInt(args[0]);
    month1=Integer.parseInt(args[1]);
    day1=Integer.parseInt(args[2]);
    year2=Integer.parseInt(args[3]);
    month2=Integer.parseInt(args[4]);
    day2=Integer.parseInt(args[5]);
    }
    catch(NumberFormatException e)
    { year1=2012;
    month1=0;
    day1=1;
    year2=2018;
    month2=0;
    day2=1;
    }
    Calendar calendar=Calendar.getInstance();
    calendar.set(year1,month1-1,day1);
    long timeYear1=calendar.getTimeInMillis();
    calendar.set(year2,month2-1,day2);
    long timeYear2=calendar.getTimeInMillis();
    long 相隔天数=Math.abs((timeYear1-timeYear2)/(1000*60*60*24));
    System.out.println(""+year1+"年"+month1+"月"+day1+"日和"+
    year2+"年"+month2+"月"+day2+"日相隔"+相隔天数+"天");
    }
    }
import java.util.*;
public class E {
public static void main (String args[ ]) {
double a=0,b=0,c=0;
a=12;
b=24;
c=Math.asin(0.56);
System.out.println(c);
c=Math.cos(3.14);
System.out.println(c);
c=Math.exp(1);
System.out.println(c);
c=Math.log(8);
System.out.println(c);
}
}
  1. public class E {
    public static void main (String args[ ]) {
    String str = "ab123you你是谁?";
    str = str.replaceAll(regex,"\\D+");//匹配一次或多次非数字
    System.out.println(str);
    }
    }
import java.util.*;
public class E {
public static void main(String args[]) {
String cost = "数学87分,物理76分,英语96分";
Scanner scanner = new Scanner(cost);
scanner.useDelimiter("[^0123456789.]+");
double sum=0;
int count =0;
while(scanner.hasNext()){
try{
double score = scanner.nextDouble();
count++;
sum = sum+score;
System.out.println(score);
}
catch(InputMismatchException exp){
String t = scanner.next();
}
}
System.out.println("总分:"+sum+"分");
System.out.println("平均分:"+sum/count+"分");
}
}