欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

有趣的题

程序员文章站 2022-04-19 16:05:39
...

1.实现一个方法将一个字符串的奇数位取出,如传的是ahbhchd,就返回abcd

 

public class Test {

	public static void main(String args[]) {
		Test t = new Test();
		String result = t.getJiShuChuan("ahbhchdhehf");
		System.out.println(result);
	}
	
	public String getJiShuChuan(String src) {
		StringBuffer buffer = new StringBuffer(src);
		int len = buffer.length();
		if (len > 1) {
			for (int i=1;i<len;i++,len--) {
				buffer.deleteCharAt(i);
				
			}
		}
		return buffer.toString();
	}
}

 

2.输出一个菱形

 

public class Test2 {
	
	public static void main(String args[]) {
		Test2 t = new Test2();
		t.print(25);
	}

	public void print(int n) {
		for (int i = 0; i < n / 2; i++) {
			int num1 = (i + 1) * 2 - 1;
			int num2 = (n - num1) / 2;
			printSpace(num2);
			printNum(num1);
			printSpace(num2);
			System.out.println();
		}
		printNum(n);
		System.out.println();
		for (int i=n/2;i>0;i--) {
			int num1 = 2*i - 1;
			int num2 = (n-num1)/2; 
			printSpace(num2);
			printNum(num1);
			printSpace(num2);
			System.out.println();
		}
	}

	private void printSpace(int n) {
		for (int i = 0; i < n; i++) {
			System.out.print("  ");
		}
	}

	private void printNum(int n) {
		for (int i = 0; i < n; i++)
			System.out.print(" *");
	}
}
相关标签: Java