java学习笔记

位移运算符
public class HelloJava {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int a = 24;
	System.out.println(a+"右移两位的结果是:"+ (a>>2));
	int b = -16;
	System.out.println(b+"左移三位的结果是:"+(b>>3));
	int c = -256;
	System.out.println(c+"无符号右移2位的结果是:"+(c>>>2));
}

}
让byte,short两种类型的变量做无符号右移操作
public class HelloJava {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	byte a = (byte) (-32>>>1);
	System.out.println("byte无符号右移的结果是:"+ a);
	short b = (short) (-128>>>4);
	System.out.println("short无符号右移的结果是:" +b);
}

}

判断输入的电话,如果不是,提示号码不存在

import java.util.Scanner;
public class HelloJava {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner in = new Scanner (System.in);//创建Scanner对象,用于进行输入
	System.out.println("请输入要拨打的电话号码:" );
	int phoneNumber = in.nextInt();//创建变量,保存电话号码
	if (phoneNumber !=123456)
		System.out.println("对不起,您拨打的号码" );
	
}

}

用if…else语句判断变量的值决定输出结果
public class Getifeslse {

public static void main(String[] args) {
	// TODO Auto-generated method stub
			int math = 95;
			int english =56;
			if (math >60) {
			System.out.println("数学及格了" );
			} else {
				System.out.println("数学没有及格");
			}
		if (english >60 ) {
			System.out.println("英语及格了");
		} else {
			System.out.println("英语没有及格");
		}
	}

}

一个技巧:
public class Getifeslse {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int a =10;
	int b;
	if(a > 0 )
		b=a;
	else 
		b= -a;
	//相当于
	b= a > 0 ? a :-a;
}

}

使用if…else if多分语句通过判断x的值决定输出的结果。

public class GetTerm {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int x = 20;
	if (x >30) {
		System.out.println("a的值大于30");
	} else if (x > 10) {
		System.out.println("a的值大于10,但小于30");
	} else if (x >0 ) {
		System.out.println("a的值大于0,但小于10");
	} else {
		System.out.println("a的值小于0");
	}

}

}

规范写法:
if(flag) 表示为真
if(!flag) 表示为假

判断是否为闰年
import java.util.Scanner;

public class JudgeLeapYear {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int iYear;
	Scanner sc = new Scanner (System.in);
	System.out.println("please input number");
	iYear = sc.nextInt();
	if (iYear % 4 == 0) {
		if (iYear % 100 == 0) {
			if (iYear % 400 == 0)
				System.out.println("It is a leap year");
			else
				System.out.println("It is not a leap year");
		} else
			System.out.println("It is a leap year");
	} else 
		System.out.println("It is not a leap year");

}

}
相当于:
import java.util.Scanner;

public class JudgeLeapYear2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int iYear;
	Scanner sc = new Scanner(System.in);
	System.out.println("please input number");
	iYear = sc.nextInt();
	if (iYear %4 ==0 && iYear % 100 !=0 || iYear % 400 == 0)
		System.out.println("It is a leap year");
	else 
		System.out.println("It is not a leap year5");

}

}

使用switch语句判断星期,并打印出对应的英文:
public class GetSwitch {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int week = 2;
	switch (week) {
	case 1:
		System.out.println("Monday");
		break;
	case 2:
		System.out.println("Thusday");
		break;
	case 3:
		System.out.println("Wednesday");
		break;
	case 4:
		System.out.println("Thursday");
		break;
	case 5:
		System.out.println("Friday");
		break;
	case 6:
		System.out.println("Saturday");
		break;
	case 7:
		System.out.println("Sunday");
		break;
	default:
		System.out.println("Sorry ,I don't Know");
	}

}

}

用switch语句判断输入的分数属于哪类成绩:
import java.util.Scanner;

public class Grade {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner sc = new Scanner(System.in);
	System.out.println("请输入成绩");
	int grade = sc.nextInt();
	switch (grade) {
	case 10:
	case 9:System.out.println("成绩为优");break;
	case 8:System.out.println("成绩为良");break;
	case 7:
	case 6:System.out.println("成绩为中");break;
	case 5:
	case 4:
	case 3:
	case 2:
	case 1:
	case 0:System.out.println("成绩为差");break;
	default:System.out.println("成绩无效");
	}

}

}

通过switch语句根据字符串str的值,输出不同的提示信息。

public class SwitchInString {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "明日科技";
	switch (str) {
	case "明日":
		System.out.println("明日图书网wwww.mingribook.com");
	case "明日科技":
		System.out.println("吉林省明日科技有限公司");
		break;
	default:
		System.out.println("以上条件都不是。");
	}

}

}

通过while循环将整数1~10相加。
public class GetSum {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int x = 1;
	int sum = 0;
	while (x <= 10 ) {
		sum = sum + x;
		x++;
		System.out.println("sum=" + sum);
	}
	System.out.println("sum=" + sum);

}

}
while与do…while语句的区别
public class Cycle {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int a = 100;
	while (a == 60) {
		System.out.println("ok1");
		a--;
	}
	int b = 100;
	do {
		System.out.println("ok2");
		b--;
	} while (b == 60);

}

}
public class DoWhileTest {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int i = 0, j = 0;
	System.out.println("before do_while j=" + j);
	do {
		j++;
	} while (i > 1);
	System.out.println("after do_while j=" + j);

}

}
使用while循环输出j的值
public class WhileTest {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int i = 0,j = 0;
	System.out.println("before while j=" + j);
	while (i>1) {
		j++;
	}
	System.out.println("after while j=" + j);

}

}
使用for循环完成1~100的相加运算:

public class AdditiveFor {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int sum = 0;
	int i;
	for (i = 1; i <= 100; i ++) {
		sum +=i;
	}
	System.out.println("the result :" + sum);

}

}

使用foreach语句遍历该数组
public class Repetition {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int arr[] = {7,10,1};
	System.out.println("一组数组中的元素分别为:");
	for (int x: arr) { //foreach语句,int x引用的变量,arr指定要循环遍历的数组,最后将x输出
		System.out.println(x);
	}

}

}

用for循环输出乘法口诀表
public class Multiplication {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int i,j;
	for ( i = 1; i < 10; i++ ) {
		for ( j = 1; j < i+1; j++ ) {
			System.out.print(j + "*" + i + "=" + i * j + "\t");
		}
		System.out.println();
	}
	
}

}

输出1~20的第一个偶数,使用break跳出循环
public class BreakTest {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	for (int i = 1; i < 20; i++)
	{
		if (i % 2 == 0)
		{
			System.out.println(i);
			break;
		}
	}
	System.out.println("---end----");

}

}

在嵌套的循环中使用break,跳出内层循环:
public class BreakInsideNested {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	for (int i = 0; i < 3; i++) {
		for (int j = 0;j < 6;j++) {
			if (j == 4)
			{
				break;
			}
			System.out.println("i="+ i + " j=" + j);
		}
	}

}

}

带有标签的break跳出外层循环
public class BreakOutsideNested {

public static void main(String[] args) {
	Loop: for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 6; j++)
		{
			if (j == 4)
			{
				break Loop;
			}
			System.out.println("i=" + i + " j=" + j);
		}
	}
	
}

}

输出1~20的偶数,使用continue语句跳出循环
public class ContinueTest {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	for (int i = 1; i < 20; i++)
	{
		if (i % 2 !=0)
		{
			continue;
		}
		System.out.println(i);
	}

}

}

使用length属性获取数组长度

public class GetArraylength {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	char a[]= {'A','B','C','D'};
	System.out.println("数组a的长度为" + a.length);
	char b[]= a;
	System.out.println("数组b的长度为" + b.length);

}

}

将各月的天数输出
public class GetDay {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int day[]= new int[] { 31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31};
	for (int i = 0; i < 12; i++)
	{
		System.out.println((i+1) + "月有" + day[i] +"天");
	}

}

}

用三种方法初始化二维数组

public class InitTDArray {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	//第一种方式
	int tdarr1[][] = {{1, 3, 5},{5, 9, 10}};
	//第二种方式
	int tdarr2[][] = new int [][] {{65, 55, 12},{92, 7, 22}};	
	//第三种方式
	int tdarr3 [][] = new int [2][3];
	tdarr3[0] = new int[] {6, 54, 71 };
	tdarr3[1][0] = 63;
	tdarr3[1][1] = 10;
	tdarr3[1][2] = 7;

}

}

创建一个二维数组,横版和竖版两种方式输出古诗:

public class Potry {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	char arr[][] = new char [4][];
	arr[0] = new char[] {'春','眠','不','觉','晓'};
	arr[1] = new char[] {'处','处','闻','啼','鸟'};
	arr[2] = new char[] {'夜','夜','风','雨','声'};
	arr[3] = new char[] {'花','落','知','多','少'};
	System.out.println("----横版----");
	for (int i =0; i <4; i++)
	{
		for (int j = 0;j < 5; j++)
		{
			System.out.print(arr[i][j]);
		}
		if (i % 2 == 0) {
			System.out.println(",");
		} else {
			System.out.println("。");
		}
	}
	System.out.println("\n----竖版----");
	for (int j = 0; j < 5; j++)
	{
		for (int i = 3;i >= 0; i--)
		{
			System.out.print(arr[i][j]);
			
		}
		System.out.println();
	}
	System.out.println("。,。,");	

}

}

创建一个二维数组,输出数组每行的元素个数及各元素的值:
public class IrrengularArray {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int a[][] = new int[3][];
	a[0] = new int[] {52, 64, 85, 12, 2, 64};
	a[1] = new int[] {41, 99, 2};
	a[2] = new int[] {285, 61, 278, 2};
	for (int i = 0; i <a.length; i++)
	{
		System.out.print("a[" +i+ "]中有有" + a[i].length +"个元素,分别是:");
		for (int tmp : a[i])
		{
			System.out.print(tmp + " ");
		}
		System.out.println();
	}

}

}

创建二维数组,二维数组中的元素呈梯形输出

public class Trap {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int b[][] = new int[][] {{1},{2,3},{4,5,6}};
	for (int k = 0; k <b.length;k++)
	{
		for (int c = 0; c <b[k].length;c++)
		{
		System.out.print(b[k][c]);
	}
	System.out.println();
	}

}

}

通过fill()方法填充数组元素,最后将数组中的各个元素输出
import java.util.Arrays;
public class Swap {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int arr[] = new int[5];
	Arrays.fill(arr, 8);
	for (int i = 0;i < arr.length; i++)
	{
		System.out.println("第" + i + "个元素是:" + arr[i]);
	}

}

}

用过fill()方法替换数组元素,最后将数组中的各个元素输出
import java.util.Arrays;
public class Displace {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int arr[] = new int[] {45, 12, 2, 77, 31, 91, 10};
	Arrays.fill(arr, 1, 4, 8);
	for (int i = 0; i < arr.length; i++)
	{
		System.out.println("第" + i + "个元素是:" + arr[i]);
	}

}

}
创建一维数组,将此数组复制得到一个长度为5的新数组,并将新数组输出
import java.util.Arrays;
public class Cope {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int arr[] = new int[] { 23, 42, 12, };
	int newarr[] = Arrays.copyOf(arr, 5);
	for (int i = 0; i < newarr.length; i++)
	{
		System.out.println("第" + i + "个元素:" + newarr[i]);
	}

}

}
创建一维数组,并将数组中索引位置是0~3之间的元素复制到新数组中
import java.util.Arrays;
public class Repeat {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int arr[] = new int[] {23, 42, 12, 84, 10};
	int newarr[] = Arrays.copyOfRange(arr, 0, 3);
	for (int i = 0; i < newarr.length; i++)
	{
		System.out.println(newarr[i]);
	}

}

}
冒泡排序,排序使用的是正排序。
public class Buddllesort {
public void sort (int[] array)
{
for (int i = 1; i < array.length; i++)
{
for (int j = 0; j < array.length - i; j++)
{
if (array[j] > array[j+1])
{
int temp = array[j];
array[j] = array [j+1];
array[j+1] = temp;
}
}
}
showArray(array);
}

public void showArray (int[] array)
{
	System.out.println("冒泡排序的结果:");
	for (int i : array)
	{
		System.out.println( i + " ");
	}
	System.out.println();
}

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int [] array = { 63, 4, 24, 1, 3, 15 };
	Buddllesort sorter = new Buddllesort();
	sorter.sort(array);

}

}

选择排序,正排序

public class Selectsort {

public void sort (int[] array)
{
	int index;
	for (int i = 1; i <array.length; i++)
	{
		index=0;
		for(int j = 1; j <= array.length - i ; j++)
		{
			if (array[j] > array[index])
			{
				index = j;
			}
		}
		int temp = array[array.length - i ];
		array[array.length - i] = array[index];
		array[index] = temp;
	}
	showArray(array);
}

public void showArray (int[] array)
{
	System.out.println("选择排序的结果为: ");
	for (int i : array) 
	{
		System.out.print(i + " ");
	}
	System.out.println();
}

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int [] array = { 63, 4, 24, 1, 3, 15 };
	Selectsort sorter = new Selectsort();
	sorter.sort(array);

}

}
创建一个一维数组,将数组排序后输出

import java.util.Arrays;

public class Taxis {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int arr[] = new int[] { 23, 42, 12, 8 };
	Arrays.sort(arr);
	System.out.println("排序后的结果为");
	for (int i=0; i<arr.length;i++)
	{
		System.out.print(arr[i]+" ");
	}

}

}

声明多个字符串变量,用不同的赋值方法给这些字符串变量赋值并输出。

public class CreateString {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String a = "时间就是金钱,我的朋友。";//直接引用字符串常量
	System.out.println("a = " + a );
	String b = new String ("我爱清汤小肥羊");//利用构造方法实体化
	String c = new String (b);//使用现有字符串变量实例化
	System.out.println("b = " + b);
	System.out.println("c = " + c);
	char [] charArray = {'t', 'i', 'm' ,'e'};
	String d = new String (charArray);//利用字符数组实例化
	System.out.println("d = " + d);
	char[] charArray2 = {'时', '间', '就', '是', '金', '钱' };
	String e = new String (charArray2, 4, 2);
	System.out.println("e = " + e);

}

}
使用“+”和“+=”拼接字符串

public class StringConcatenation {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String a = "abc";
	String b = "123";
	String c = a + b + "!";
	String d = "拼接字符串";
	d += c;
	System.out.println("a = " + a);
	System.out.println("b = " + b);
	System.out.println("c = " + c);
	System.out.println("d = " + d);

}

}
在主方法中创建数值型变量,实现将字符串与整型,浮点型变量相连的结果输出

public class Link {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int booktime = 4;
	float practice = 2.5f;
	System.out.println("我每天花费" + booktime + "小时看书; " + practice + "小时上机练习");

}

}
注意!!!
字符串在计算公式中的先后顺序会影响运算的结果:
String a = “1” +2+3+4 “1234”
String b = 1+2+3+ “4” “64”
String c = “1”+(2+3+4) “19”

创建字符串对象,查看字符串中索引位置是4的字符

public class ChatAtTest {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "床前明月光,疑是地上霜。";
	char chr = str.charAt(4);
	System.out.println("字符串中索引位置为4的字符是:" + chr);
}

}
判断str中是否包含字符串“abc”

public class StringIndexOf {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "123456abcde";
	int charIndex = str.indexOf("abc");
	System.out.println(charIndex);
	if (charIndex != -1)
	{
		System.out.println("str中存在abc字符串");
	} else
	{
		System.out.println("str中没有abc字符串");
	}

}

查找字符串“We are the world”中"r"第一,二,三次出现的索引位置。

public class StringIndexOf2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "We are the world";
	int firstIndex = str.indexOf("r");
	int secondIndex = str.indexOf("r",firstIndex + 1);
	int thirdIndex = str.indexOf("r", secondIndex + 1);
	System.out.println("r第一次出现的索引位置是:" + firstIndex);
	System.out.println("r第二次出现的索引位置是:" + secondIndex);
	System.out.println("r第三次出现的索引位置是:   " + thirdIndex);
	

}

}

查找字符串“Let it go!Let it go!”中单词"go"最后出现的位置

public class StringLastIndexOf {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "Let it go!Let it go!";
	int gIndex = str.lastIndexOf("g");
	int goIndex = str.lastIndexOf("go");
	int oIndex = str.lastIndexOf("o");
	System.out.println("字符串\"Let it go!Let it go! \"中:\n");
	System.out.println("\"g\"最后一次出现的位置是:" + gIndex);
	System.out.println("\"o\"最后一次出现的位置是:" + oIndex);
	System.out.println("\"go\"最后一次出现的位置是:" + goIndex);

}

}

查询字符串“01a3a56a89”中字母“a”的位置

public class StringLastIndexOf2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "01a3a56a89";
	int lastIndex = str.lastIndexOf("a");
	int fiveBeforeIndex = str.lastIndexOf("a",5);
	int threeBeforeIndex = str.lastIndexOf("a",3);
	System.out.println("字符串\"01a3a56a89\"中:\n");
	System.out.println("字母\"a\"最后一次出现的位置是:" + lastIndex);
	System.out.println("从索引位置5开始往回搜索,字母\"a\"最后一次出现的位置: " + fiveBeforeIndex);
	System.out.println("从索引位置3开始往回搜索,字母\"a\"最后一次出现的位置:" +threeBeforeIndexp3p);

}

}

查看一个字符串是否以“我有一个梦想”开始

public class StringStartWith {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String myDream1 = "我有一个梦想,幽谷上升,高山下降;";
	String myDream2 = "坎坷曲折之路成坦途,圣光披露,满照人间。";
	System.out.println(myDream1+myDream2+ "\n\t\t————马丁.路德金《我有一个梦想》\n");
	boolean firstBool = myDream1.startsWith("我有一个梦想");
	boolean secondBool = myDream2.startsWith("我有一个梦想");
	if (firstBool)
	{
		System.out.println("前半句是以\"我有一个梦想\"开始的。");
	}else if (secondBool)
	{
		System.out.println("后半句是以\"我有一个梦想\"开始的。");
	} else
	{
		System.out.println("没有以\"我有一个梦想\"开始的。");
	}

}

}

查询五言绝句《静夜思》的第二行是否以“举”字开头。

public class StringStartWith2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "床前明月光,疑是地上霜。\n举头望明月,低头思故乡。";
	System.out.println("    《静夜诗》\n" + str + "\n");
	int enterIndex = str.indexOf("\n");
	boolean flag = str.startsWith("举",enterIndex + 1);
	if (flag)
	{
		System.out.println("第二行是以\"举\"开始的");
	} else 
	{
		System.out.println("第二行是以\" " + str.charAt(enterIndex  +1 ) + "\"开始的");
	}

}

}
查看一个字符是否以句号结尾

public class StringEndsWith {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str1 = "你说完了吗?";
	String str2 = "我说完了。";
	boolean flag1 = str1.endsWith("。");
	boolean flag2 = str2.endsWith("。");
	System.out.println("字符串str1是以句号结尾的吗?" + flag1);
	System.out.println("字符串str2是以句号结尾的吗?" + flag2);

}

}

创建一个字符串,将此字符串转换成一个字符数组,并分别输出字符数组中的每个元素

public class StringToArray {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "这是一个字符串";
	char[] ch = str.toCharArray();
	for (int i = 0 ; i < ch.length; i++)
	{
		System.out.println("数组第" + i + "个元素为:" + chi[i]);
	}

}

}

创建字符串,输出相声中的《报菜名》,然后用contains()方法查看是否有“腊肉”和“汉堡”这两道菜

public class StringContains {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "今天的菜单有:蒸羊羔,蒸熊掌,蒸鹿尾。烧花鸭,烧雏鸡,烧子鹅,卤煮咸鸭,酱鸡,腊肉,松花小肚。";
	System.out.println(str);
	boolean request1 = str.contains("腊肉");
	System.out.println("今天有腊肉吗?" + request1);
	boolean request2 = str.contains("汉堡");
	System.out.println("今天有汉堡吗?"  + request2);

}

}
输出字符串“为革命保护视力,眼保健操开始!”的最后半句话

public class StringSub {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "为革命保护视力,眼保健操开始!";
	String substr = str.substring(8);
	System.out.println("字符串str的后半句是:"  + substr);

}

}
取字符串“闭门造车,出门合辙。”的前半句话

public class StringSub2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "闭门造车,出门合辙。";
	String substr = str.substring(0,4);
	System.out.println("字符串str的前半句是:" + substr);

}

}
字符串“明月几时有,把酒问青天”中的“月”替换成“日”

public class StringReplace {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "明月几时有,把酒问青天";
	String restr = str.replace("月", "日");
	System.out.println("字符串str替换之后的效果:" + restr);

}

}
分贝使用replace()方法和replaceAll()方法,利用正则表达式将字符串中所有的数字替换成“?”

public class StringReplaceAll {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "0123456789abc\\d";
	String restr = str.replace("\\d", "?");
	String restrAll = str.replaceAll("\\d", "?");
	System.out.println("字符串str: " + str);
	System.out.println("使用replace()替换的结果:" + restr);
	System.out.println("使用resplaceAll()替换的结果: " + restrAll);

}

}

现有字符串“8I want to marry you, so I need you!”,去掉第一个数字,再把第一次出现的"you"替换成“her”。

public class StringReplaceFirst {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "8I want to marry you , so I need you!";
	String noNumber = str.replaceFirst("\\d","");
	String youToHer = noNumber.replaceFirst("you", "her");
	System.out.println("替换之后的结果是: " +youToHer);

}

}
创建一个字符串,用“,”分割

public class StringSplit {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "从前有座山,山里有座庙,庙里有个小松鼠";
	String[] strArray = str.split(",");
	for (int i = 0; i < strArray.length; i++)
	{
		System.out.println("数组第" + i + "索引的元素是:" + strArray[i]);
	}

}

}

同时使用不同的分隔符,分割同一字符串

public class StringSplit2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "alb2,c,d e f|gh";
	String[] a1 = str.split(",");
	String[] a2 = str.split(" ");
	String[] a3 = str.split("\\|");
	String[] a4 = str.split("\\d");
	String[] a5 = str.split(",| |\\||\\d");
	System.out.println("str的原值: [" +str + "]");
	System.out.print("使用\",\"分割:");
	for (String b : a1)
	{
		System.out.print("[" + b + "]");
	}
	System.out.println();
	System.out.print("使用空格分割:");
	for (String b : a2)
	{
		System.out.print("[" + b +"]");
	}
	System.out.println();
	System.out.print("使用\"|\"分割:");
	for (String b : a3)
	{
		System.out.print("[" + b +"]");
	}
	System.out.println();
	System.out.print("使用数字分割:");
	for (String b : a4)
	{
		System.out.print("[" + b + "]");
	}
	System.out.println();
	System.out.print("同时使用所有分隔符:");
	for (String b : a5 )
	{
		System.out.print("[" + b + "]");
	}
	System.out.println();
}

}

将字符串“192.168.0.1”按照“.”拆分两次,第一次全部拆分,第二次拆分两次。

public class StringSplit3 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "192.168.0.1";
	String[] firstArray = str.split("\\.");
	String[] secondArray = str.split("\\.",2);
	System.out.println("str的原值为:[" + str + "]");
	System.out.print("全部分割的结果:");
	for (String a : firstArray)
	{
		System.out.print("[" + a + "]");
	}
	System.out.println();
	System.out.print("分割两次的结果:");
	for ( String a : secondArray)
	{
		System.out.print("[" + a + "]");
	}
	System.out.println();

}

}

将字符串“abc DEF"分别用大写,小写,两种格式输出

public class StringTransform {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "abc DEF";
	System.out.println(str.toLowerCase());
	System.out.println(str.toUpperCase());

}

}
使用trim()方法去掉字符串两边的空白内容

public class StringTrim {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "     abc        ";
	String shortStr = str.trim();
	System.out.println("str的原值是:[" + str + "]");
	System.out.println("去掉首尾空白的值:[" + shortStr +"]");

}

}
利用正则表达式“\s”,将字符串中所有的空白内容替换成""

public class StringRemoveBlank {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "a b cd e      f g        ";
	String shortstr = str.replaceAll("\\s", "");
	System.out.println("str的原值是: [" + str + "]");
	System.out.println("删除空内容之后的值是:[" + shortstr + "]");

}

}

使用字符串运算符比较两个字符串

public class StringCompare {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String tom,jerry;
	tom = "I am a student";
	jerry = "I am a student";
	System.out.println("直接引入字符串常量的比较结果:" + (tom == jerry));
	tom = new String("I am a student");
	jerry = new String("I am a student");
	System.out.println("使用new创建对象的比较结果:" + (tom == jerry));

}

}

创建String变量,分别用"=="和equals()方法判断两个字符串是否相等。

public class StringEquals {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str1 = "Hello";
	String str2 = new String("Hello");
	String str3 = new String("你好");
	String str4 = str2;
	System.out.println("str1 == str2 的结果:" + (str1 == str2));
	System.out.println("str1 == str3 的结果:" + (str1 == str3));
	System.out.println("str1 == str4 的结果:" + (str1 == str4));
	System.out.println("str2 == str4 的结果:" + (str2 == str4));
	System.out.println("str1.equals(str2)的结果:" + str1.equals(str2));
	System.out.println("str1.equals(str3)的结果:" + str1.equals(str3));
	System.out.println("str1.equals(str4)的结果:" + str1.equals(str4));
}

}

使用equals()和equalsIgnoreCase()方法判断两个字符串是否相等。

public class StringEqualsIgnoreCase {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str1 = "abc";
	String str2 = "ABC";
	System.out.println("区分大小写的结果:"+str1.equals(str2));
	System.out.println("不区分大小写的结果:"+str1.equalsIgnoreCase(str2));
}

}

创建类eval,实现将当前日期信息以4位年份,月份全称,2位日期形式输出
import java.util.Date;
public class Eval {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Date date = new Date();
	String year = String.format("%tY", date);
	String month = String.format("%tB", date);
	String day = String.format("%td", date);
	System.out.println("今年是:" + year + "年");
	System.out.println("现在是:" + month );
	System.out.println("今天是:" + day + "号");
	

}

}

实现将当前时间信息以2位小数,2位分钟数,2位秒数形式输出
import java.util.Date;
public class GetDate {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Date date = new Date();
	String hour = String.format("%tH", date);
	String minute = String.format("%tM", date);
	String second = String.format("%tS", date);
	System.out.println("现在是:" + hour + "时" + minute +"分" + second + "秒");

}

}

创建类DateAndTime,在主方法中实现将当前日期时间的全部信息以指定格式和日期输出
import java.util.Date;
public class DateAndTime {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Date date = new Date();
	String time = String.format("%tc", date);
	String form = String.format("%tF", date);
	System.out.println("全部的时间信息是:" +  time);
	System.out.println("年-月-日格式:" + form);
}

}
实现不同类型的格式转化
import javax.swing.plaf.synth.SynthSpinnerUI;

public class StringFormat {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str1 = String.format("%c", 'X');//输出字符
	System.out.println("字母x大写:" + str1);
	String str2 = String.format("%d", 1251+3950);//输出数字
	System.out.println("1251+3950的结果是:" + str2);
	String str3 = String.format("%.2f", Math.PI);//输出小数点后两位
	System.out.println("π取两位小数点:" + str3);
	String str4 = String.format("%b", 2<3);//输出布尔值
	System.out.println("2<3的结果是:" + str4);
	String str5 = String.format("%h", 3510);//输出哈希散列码
	System.out.println("3510的hashCode值:" + str5);
	String str6 = String.format("%o", 33);//输出8进制
	System.out.println("33的8进制的结果是:" + str6);
	String str7 = String.format("%x", 33);//输出16进制
	System.out.println("33的16进制的结果是:" + str7);
	String str8 = String.format("%e", 120000.1);//输出科学计数法
	System.out.println("120000.1用科学计数法表示:" + str8);
	String str9 = String.format("%a", 40.0);//输出带有效位数和指数的16进制浮点值
	System.out.println("40.0的16进制浮点值:" + str9);
	System.out.println(String.format("天才是由%d%%的灵感, %d%%的汗水。",1,99));
	//输出百分号和数字

}

}

使用标识控制字符串的输出格式

public class StringFormat2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str1 = String.format("%5d", 123);//让字符串输出的最大长度为5,不足长度在前段补空格
	System.out.println("输出长度为5的字符串|" + str1 + "|");
	
	String str2 = String.format("%-5d", 123);//让字符串左对齐
	System.out.println("左对齐|" + str2 + "|");
	
	String str3 = String.format("%#o", 33);//在8进制前加一个0
	System.out.println("33的8进制的结果是:" + str3);
	
	String str4 = String.format("%#x", 33);//在16进制前加一个0x
	System.out.println("33的16进制的结果是:" + str4);
	
	String str5 = String.format("%+d", 1);//显示数字正负号
	System.out.println("我是正式:" + str5);
	
	String str6 = String.format("%+d", -1);//显示数字的正负号
	System.out.println("我是负数:" + str6);
	
	String str7 = String.format("% d", 1);//在正数前面补一个空格
	System.out.println("我是正数,前面有空格:" + str7);
	
	String str8 = String.format("% d", -1);//在负数前面补一个空格
	System.out.println("我是负数,前面有负号:" + str8);
	
	String str9 = String.format("%05d",12);//在字符串输出的最大长度为5,不足长度在前段补0
	System.out.println("前面不够的数用0填充:" + str9);
	
	String str10 = String.format("%,d", 123456789);//用逗号分隔数字
	System.out.println("用逗号分隔:" + str10);
	
	String str11 = String.format("%(d",13);//正数无影响
	System.out.println("我是正数,我没有括号:" + str11);
	
	String str12 = String.format("%(d", -13);//让负数用括号括起来
	System.out.println("我是负数,我又括号的:" + str12);

}

}

创建StringBuffer对象,使用append()追加字符序列

public class StringBufferAppend {

public static void main(String[] args) {
	StringBuffer sbf = new StringBuffer("门前大桥下,");
	sbf.append("游过一只鸭,");
	StringBuffer tmp = new StringBuffer("快来快来数一数,");
	sbf.append(tmp);
	int x = 24678;
	sbf.append(x);
	System.out.println(sbf.toString());
}

}

创建一个StringBuffer对象,将索引为3的字符修改成’A’。

public class StringBufferSerCharAt {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	StringBuffer sbf = new StringBuffer("0123456");
	System.out.println("sbf的原值是:" + sbf);
	sbf.setCharAt(3, 'A');
	System.out.println("修改后的值是:" + sbf);

}

}

创建一个StringBuffer对象,在索引为5的位置插入字符串“F”

public class StringBufferInsert {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	StringBuffer sbf = new StringBuffer("0123456");
	System.out.println("sbf的原值为:" +sbf);
	sbf = sbf.insert(5, "F");
	System.out.println("修改之后的值为:"+sbf);

}

}

创建一个StringBuffer对象,将其字符序列反序输出

public class StringBufferReverse {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	StringBuffer sbf = new StringBuffer("同一个世界,同一个梦想");
	System.out.println("sbf的原值为:" + sbf);
	sbf = sbf.reverse();
	System.out.println("修改之后的值为:" + sbf);

}

}

创建一个StringBuffer对象,删除从索引4开始至索引7之前的内容

public class StringBufferDelete {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	StringBuffer sbf = new StringBuffer("天行健,君子以自强不息");
	System.out.println("sbf的原值为:" + sbf);
	sbf = sbf.delete(4, 7);
	System.out.println("删除之后的值为:" + sbf);

}

}

StringBuffer类中类似String类的方法

public class StringBufferTest {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	StringBuffer sbf = new StringBuffer("ABCDEFG");
	int length = sbf.length();
	char chr = sbf.charAt(5);//获取索引为5的字符
	int index = sbf.indexOf("DEF");//获取DEF字符串所在的索引位置
	String substr = sbf.substring(0,2);
	StringBuffer tmp = sbf.replace(2, 5, "1234");//将索引2开始至索引5之间的字符串序列替换成“1234”
	
	
	System.out.println("sbf的原值为:" + sbf);
	System.out.println("sbf的长度为:" + length);
	System.out.println("索引为5的字符为:" + chr);
	System.out.println("DEF字符串的索引位置为:" + index);
	System.out.println("索引0开始至索引2之间的字符串:" + substr);
	System.out.println("替换后的字符串:" + tmp);
	

}

}
创建StringBuilder字符序列对象,对其做追加,插入,删除和反序列输出操作

public class StringBuilderTest {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	StringBuilder sbd = new StringBuilder();
	System.out.println("sbd的原值为空");
	sbd.append("我是StringBuilder类");
	System.out.println("sbd追加字符串:" + sbd);
	int length = sbd.length();
	System.out.println("sbd的长度为:" + length);
	sbd = sbd.insert(length-1,"123456");
	System.out.println("插入字符串:" + sbd);
	sbd = sbd.delete(sbd.length()-1, sbd.length());
	System.out.println("删除最后一个字:" + sbd);
	sbd = sbd.reverse();
	System.out.println("反序输出:" + sbd);

}

}
创建StringBuffer对象,StringBuilder对象,String对象,并将三者的内容互相转换。

public class StringInterchange {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "String";
	StringBuffer sbf = new StringBuffer(str);//String转换成StringBuffer
	StringBuilder sbd = new StringBuilder(str);
	str = sbf.toString();//StringBuffer转换成String
	str = sbd.toString();//StringBuilder转换成String
	StringBuilder bufferToBuilder = new StringBuilder(sbf.toString());
	          //StringBuffer转换成StringBuilder
	StringBuffer builderToBuffer = new StringBuffer(sbd.toString());
	         //String Builder转换成StringBuffer

}

}

在项目中创建类Jerque,在主方法中编写如下代码,验证字符串操作和字符串生成器操作的效率。

public class Jerque {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String str = "";
	long startTime = System.currentTimeMillis();//定义对字符串执行的起始时间
	for (int i = 0; i < 10000 ; i++)
	{
		str = str + i;
	}
	long endTime = System.currentTimeMillis();
	long time = endTime - startTime;
	System.out.println("String循环1万次消耗时间:" + time);
	
	StringBuilder builder = new StringBuilder("");
	startTime = System.currentTimeMillis();
	for (int j = 0; j < 10000; j++)
	{
		builder.append(j);
	}
	endTime = System.currentTimeMillis();
	time = endTime - startTime;
	System.out.println("StringBuiiler循环1万次消耗的时间:" + time);
	
	StringBuilder builder2 = new StringBuilder("");
	startTime = System.currentTimeMillis();
	for (int j = 0; j < 50000; j++)
	{
		builder2.append(j);
	}
	endTime = System.currentTimeMillis();
	time = endTime - startTime;
	System.out.println("StringBuilder循环5万次消耗的时间:" + time);
}

}

定义一个add方法,用来计算两个数的和,该方法中有两个形参,但在方法体中,对其中的一个形参x执行y操作,并返回x;在main方法中调用该方法,为该方法传入定义好的实参;最后分别显示调用add方法计算之后的x值和实参x的值。
public class Book {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Book book =new Book();
	int x = 30;
	int y = 40;
	System.out.println("运算结果:" + book.add(x,y));
	System.out.println("实参x的值:" + x);
}
private int add(int x,int y)
{
	x = x + y;
	return x;
}

}

定义一个change方法,该方法中有一个形参,类型为数组类型,在方法体中,改变数组的索引0,1,2这3处的值,在main方法中定义一个一维数组并初始化,然后将该数组作为参数传递给change方法,最后输出一维数组的元素。

public class RefTest
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
RefTest refTest = new RefTest();
int [] i = { 0 , 1 , 2 };
System.out.print(“原始数据:”);
for (int j=0;j<i.length;j++)
{
System.out.print(i[j]+" “);
}
refTest.change(i);
System.out.print(”\n修改后的数据: “);
for (int j = 0; j < i.length; j++)
{
System.out.print(i[j]+” ");
}
}
public void change(int [] i)
{
i[0]=100;
i1=200;
i2=300;
}

}

定义一个add方法,用来计算多个int类型数据和,在具体定义时,将参数定义为int类型的不定长参数;在main方法中调用该方法,为该方法传入多个int类型的数据,并输出计算结果。
public class MultiTest {

public static void main(String[] args)
{
	// TODO Auto-generated method stub
	MultiTest multi = new MultiTest();
	int result = 100;
	System.out.println("运算结果:" + multi.add(20,30,40,50,60));
}
int add(int... x)
{
	int result = 0;
	for(int i = 0; i < x.length; i++)
	{
		result +=x[i];
	}
	return result;
}

}

创建猎豹类,用成员方法实现猎豹的行为
public class Leopard {
public void gaze(String target)//凝视。目标函数是taget
{
System.out.println("猎豹凝视: " + target);
}
public void run()
{
System.out.println(“猎豹开始奔跑”);
}
public boolean catchPrey(String prey)
{//捕捉猎物,返回捕捉是否成功
System.out.println(“猎豹开始捕猎”+ prey);
return true;//返回成功
}
public void eat(String meat)
{//吃肉,参数是肉
System.out.println(“猎豹吃” + meat);
}
public void sleep()
{
System.out.println(“猎豹睡觉”);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Leopard liebao = new Leopard();
liebao.gaze(“羚羊”);
liebao.run();
liebao.catchPrey(“羚羊”);
liebao.eat(“羚羊肉”);
liebao.sleep();
}

}

创建一个图书类,将构造方法设为私有,这时如果需要创建图书类的对象,只能通过定义一个static方法,并调用该静态方法生成图书类的对象
public class BookTest {
private BookTest()
{
//私有静态构造方法
}//静态公开方法,向图书馆借书
static public BookTest libraryBorrow()
{
//创建静态方法,返回本类实例对象
System.out.println(“通过调用静态方法创建对象”);
return new BookTest();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个书的对象,不是new实例化的,而是通过方法从图书馆借来的
BookTest book = BookTest.libraryBorrow();

}

}

在项目中创建CreateObject类,在该类中创建对象并在主方法中创建对象。

public class CreateObject
{
public CreateObject()
{
System.out.println(“创建对象”);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new CreateObject();

}

}

在项目中创建TransferProperty类,在该类中说明对象是如何调用类成员的

public class TransferProperty
{
int i = 47; //定义成员变量
public void call() //定义成员方法
{
System.out.println(“调用call()方法”);
for (i = 0; i < 3; i++)
{
System.out.print(i + " “);
if (i ==2 )
{
System.out.println(”\n");
}
}
}
public TransferProperty()
{
//定义构造方法
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TransferProperty t1 = new TransferProperty();
TransferProperty t2 = new TransferProperty();
t2.i= 60;
//使用第一个对象调用类成员变量
System.out.println(“第一个实例对象调用变量i的结果:” + t1.i);
t1.call();
//使用第二个对象调用类成员变量
System.out.println(“第二个实例对象调用变量i的结果:” + t2.i);
t2.call();
}

}

创建Book2类,定义一个成员变量name并赋初值,再定义一个成员方法showName(String name),输出方法中name的值

public class Book2 {
String name = “abc”;
public void showName(String name)
{
System.out.println(name);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Book2 book = new Book2();
book.showName(“123”);

}

}

在Book3类的showName()方法中,使用this关键字。

public class Book3 {
String name = “abc”;
public void showName(String name)
{
System.out.println(this.name);
}

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Book3 book = new Book3();
	book.showName("123");

}

}

我去买鸡蛋灌饼,我要求加几个蛋时,烙饼大妈就给饼加几个蛋,不要求的时候就只加一个蛋。创建鸡蛋灌饼EggCake类,创建有参数和无参数构造方法,无参数构造方法调用有参数实现初始化。

public class EggCake {
int eggCount;//鸡蛋灌饼里有几个蛋
//有参数构造方法,参数是给饼加鸡蛋的个数
public EggCake (int eggCount)
{
this.eggCount = eggCount;
System.out.println(“这个鸡蛋灌饼里有” + eggCount + “个蛋。”);
}
//无参数构造方法,默认给饼一个鸡蛋
public EggCake()
{
this(1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
EggCake cake1 = new EggCake();
EggCake cake2 = new EggCake(5);

}

}

创建一个水池类,创建注水方法和放水方法,同时控制水池中的水量

public class Pool {
static public int water = 0;
public void outlet()
// 防水,一次放出2个单位
{
if (water >= 2)
{
water = water - 2;
}
else
{
water = 0;
}
}
public void inler()
//注水,一次注入3个单位
{
water = water + 3;
}

public static void main(String[] args)
{
	// TODO Auto-generated method stub
	Pool out = new Pool();
	Pool in = new Pool();
	System.out.println("水池的水量:" + Pool.water);
	System.out.println("水池注水两次。");
	in.inler();
	in.inler();
	System.out.println("水池的水量:" + Pool.water);
	System.out.println("水池放水一次。");
	out.outlet();
	System.out.println("水池的水量:" + Pool.water);

}

}

建StaticVariable类,包含一个静态成员变量和普通成员变量,在构造方法中给两个变量赋初值,然后分别实例化两个不同的对象

public class StaticVariable
{
static int x;
int y;
public StaticVariable(int x, int y)
{
this.x=x;
this.y=y;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
StaticVariable a = new StaticVariable(1,2);
StaticVariable b = new StaticVariable(13,17);
System.out.println("a.x的值是 = " + a.x);
System.out.println("a.y的值是 = " + a.y);
System.out.println("b.x的值是 = " + b.x);
System.out.println("b.y的值是 = " + b.y);
}

}

将π的值赋给静态常量PI,使用PI计算圆类的面积和球类的体积

public class Graphical {
final static double PI = 3.1415926;
public static void main(String[] args)
{
// TODO Auto-generated method stub
double radius = 3.0;
double area = Graphical.PI * radius * radius;
double volume = 4/3 * Graphical.PI * radius * radius * radius;
Circular yuan = new Circular (radius,area);
Spherical qiu = new Spherical (radius,volume);
}
}
class Circular
{
double radius;
double area;
public Circular(double radius,double area)
{
this.radius = radius;
this.area = area;
System.out.println(“圆的半径是:” + radius + “,圆的面积是:” + area);
}
}
class Spherical
{
double radius;
double volume;
public Spherical (double radius, double volume)
{
this.radius = radius;
this.volume = volume;
System.out.println(“球的半径是:” + radius + ",球的体积是: " + volume);
}
}

创建静态代码块,非静态代码块,构造方法,成员方法,查看这几处代码的调用顺序。

public class StaticTest
{
static String name;
//静态代码块
static
{
System.out.println(name + “静态代码块”);
}
//非静态代码块
{
System.out.println(name + “非静态代码块”);
}
public StaticTest(String a)
{
name = a;
System.out.println(name + “构造方法”);
}
public void method()
{
System.out.println(name + “成员方法”);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StaticTest s1;//声明的时候就已经运行代码块了
StaticTest s2 = new StaticTest(“s2”);//new的时候才会运行构造方法
StaticTest s3 = new StaticTest(“s3”);
s3.method();//只有调用的时候才会运行

}

在项目中创建TestMain类,在主方法中编写一下代码,并在Eclipse中设置程序参数
public class TestMain {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	for (int i = 0; i < args.length; i ++)
	{
		System.out.println(args[i]);
	}

}

}

创建Restaurant1这个类,实现餐馆点菜的场景。

public class Restaurant1
{
public static void main(String[] args) {
// TODO Auto-generated method stub
String cookName = “Tom Cruise”;//厨师名字叫Tom Cruise
System.out.println(“请让厨师为我做一份香辣肉丝。*”);
System.out.println(cookName + “切葱花”);
System.out.println(cookName + “洗蔬菜”);
System.out.println(cookName + “开始烹饪” + “香辣肉丝”);
System.out.println(“请问厨师叫什么名字?*”);
System.out.println(“cookName”);
System.out.println(“请让厨师给我切一点葱花。*”);
System.out.println(cookName + “切葱花”);
}

}

将厨师封装成Cook,实现餐馆点菜的场景。
import javax.swing.plaf.synth.SynthSpinnerUI;

public class Restaurant2
{

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Cook1 cook = new Cook1();
	System.out.println("**请让厨师为我做一份香辣肉丝。***");
	cook.cooking("香辣肉丝");
	System.out.println("**你们的厨师叫什么名字?***");
	System.out.println(cook.name);//厨师回答自己的名字
	System.out.println("**请让厨师给我切一点葱花。***");
	cook.cutOnion();

}

}
class Cook1
{
String name;//厨师的名字
public Cook1()
{
this.name = “Tom Cruise”;
}
void cutOnion()
{
System.out.println(name + “洗蔬菜”);
}
void washVagetables()
{
System.out.println(name + “洗蔬菜”);
}
void cooking(String dish)
{
washVagetables();
cutOnion();
System.out.println(name + “开始烹饪” + dish);
}
}

将厨师的属性和方法用private修饰

public class Restaurant3 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Cook2 cook = new Cook2();
	System.out.println("**请让厨师为我做一份香辣肉丝。***");
	cook.cooking("香辣肉丝");
	System.out.println("**你们的厨师叫什么名字?***");
	System.out.println("cook.name");
	System.out.println("**请让厨师给我切一点葱花。***");
	cook.cutOnion();
}

}
class Cook2
{
private String name;
public Cook2()
{
this.name = “Tom Cruise”;
}
private void cutOnion()
{
System.out.println(name + “切葱花”);
}
private void washVegetables()
{
System.out.println(name + “洗蔬菜”);
}
void cooking(String dish)
{
washVegetables();
cutOnion();
System.out.println(name + “开始烹饪” + dish);
}
}

将厨师对象封装在餐馆类中,顾客无法解除到厨师的任何信息

public class Restaurant4 {
private Cook2 cook = new Cook2();
public void takeOrder (String dish)
{
cook.cooking(dish);
System.out.println(“您的菜好了,请慢用。”);
}
public String saySorry()
{
return “抱歉,餐厅不提供此项服务。”;
}

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Restaurant4 water = new Restaurant4();
	System.out.println("**请让厨师为我做一份香辣肉丝。***");
	water.takeOrder("香辣肉丝");
	System.out.println("**你们厨师叫什么名字?***");
	System.out.println(water.saySorry());
	System.out.println("**请让厨师给我切一点葱花。***");
	System.out.println(water.saySorry());

}

}

创建Pad类,继承Computer类
class Computer
{
String screen = “液晶显示屏”;
void startup()
{
System.out.println(“电脑正在开机,请等待…”);
}
}
public class Pad extends Computer
{
String battery = “5000毫安电池”;//子类独有的属性
public static void main(String[] args)
{
// TODO Auto-generated method stub
Computer pc = new Computer();
System.out.println(“computer的屏幕是:” + pc.screen);
pc.startup();
Pad ipad = new Pad();
System.out.println(“Pad的屏幕是:” + ipad.screen);
System.out.println(“pad的电池是:” + ipad.battery);
ipad.startup();

}

}

创建Pad2类,继承Computer2类,并重写父类的showPicture()方法
class Computer2
{
void showPicture()
{
System.out.println(“鼠标单击”);
}
}
public class Pad2 extends Computer2
{
void showPicture()
{
System.out.println(“手指点击触摸屏”);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Computer2 pc = new Computer2();
System.out.print(“pc打开图片:”);
pc.showPicture();//调用方法
Pad2 ipad = new Pad2();
System.out.print(“ipad打开图片:”);
ipad.showPicture();//重写父类方法
Computer2 computerpad = new Pad2();//父类声明,子类实现
System.out.println(“Computerpad打开图片”);
computerpad.showPicture();//调用父类方法,实现子类重写的逻辑

}

}

欢迎使用Markdo

wn编辑器

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markd的基本语法知识。

新的改变

我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

  1. 全新的界面设计 ,将会带来全新的写作体验;
  2. 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示;
  3. 增加了 图片拖拽 功能,你可以将本地的图片直接拖拽到编辑区域直接展示;
  4. 全新的 KaTeX数学公式 语法;
  5. 增加了支持甘特图的mermaid语法1 功能;
  6. 增加了 多屏幕编辑 Markdown文章功能;
  7. 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间;
  8. 增加了 检查列表 功能。

功能快捷键

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + Shift + B
斜体:Ctrl/Command + Shift + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G

合理的创建标题,有助于目录的生成

直接输入1次#,并按下space后,将生成1级标题。
输入2次#,并按下space后,将生成2级标题。
以此类推,我们支持6级标题。有助于使用TOC语法后生成一个完美的目录。

如何改变文本的样式

强调文本 强调文本

加粗文本 加粗文本

标记文本

删除文本

引用文本

H2O is是液体。

210 运算结果是 1024.

插入链接与图片

链接: link.

图片: Alt

带尺寸的图片: Alt

当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。

如何插入一段漂亮的代码片

博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

// An highlighted block
var foo = 'bar';

生成一个适合你的列表

  • 项目
    • 项目
      • 项目
  1. 项目1
  2. 项目2
  3. 项目3
  • 计划任务
  • 完成任务

创建一个表格

一个简单的表格是这么创建的:

项目Value
电脑$1600
手机$12
导管$1

设定内容居中、居左、居右

使用:---------:居中
使用:----------居左
使用----------:居右

第一列第二列第三列
第一列文本居中第二列文本居右第三列文本居左

SmartyPants

SmartyPants将ASCII标点字符转换为“智能”印刷标点HTML实体。例如:

TYPEASCIIHTML
Single backticks'Isn't this fun?'‘Isn’t this fun?’
Quotes"Isn't this fun?"“Isn’t this fun?”
Dashes-- is en-dash, --- is em-dash– is en-dash, — is em-dash

创建一个自定义列表

Markdown
Text-to- HTML conversion tool
Authors
John
Luke

如何创建一个注脚

一个具有注脚的文本。2

注释也是必不可少的

Markdown将文本转换为 HTML

KaTeX数学公式

您可以使用渲染LaTeX数学表达式 KaTeX:

Gamma公式展示 Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ(n)=(n1)!nN 是通过欧拉积分

Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t &ThinSpace; . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=0tz1etdt.

你可以找到更多关于的信息 LaTeX 数学表达式here.

新的甘特图功能,丰富你的文章

Mon 06 Mon 13 Mon 20 已完成 进行中 计划一 计划二 现有任务 Adding GANTT diagram functionality to mermaid
  • 关于 甘特图 语法,参考 这儿,

UML 图表

可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图::

张三 李四 王五 你好!李四, 最近怎么样? 你最近怎么样,王五? 我很好,谢谢! 我很好,谢谢! 李四想了很长时间, 文字太长了 不适合放在一行. 打量着王五... 很好... 王五, 你怎么样? 张三 李四 王五

这将产生一个流程图。:

链接
长方形
圆角长方形
菱形
  • 关于 Mermaid 语法,参考 这儿,

FLowchart流程图

我们依旧会支持flowchart的流程图:

Created with Raphaël 2.2.0 开始 我的操作 确认? 结束 yes no
  • 关于 Flowchart流程图 语法,参考 这儿.

导出与导入

导出

如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。

导入

如果你想加载一篇你写过的.md文件或者.html文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。


  1. mermaid语法说明 ↩︎

  2. 注脚的解释 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值