Java笔试题

一、基础编程题

1、编程输出一个倒立三角形图。

package Test1;

public class Test1 {
	public static void main(String[] args) {
		for(int i = 0; i < 5; i++){
			for(int j = 5; j >i; j--){
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

2、打印昨天的当前时刻。

package Test1;

import java.util.Calendar;

public class Test2 {
	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		System.out.println(cal.getTime());
	}
}

3、编写程序,取得当前时间的年月日,小时分秒。

package Test1;

import java.util.Calendar;

public class Test3 {
	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		System.out.println(cal.get(Calendar.YEAR));
		System.out.println(cal.get(Calendar.MONTH)+1);
		System.out.println(cal.get(Calendar.DATE));
		System.out.println(cal.get(Calendar.HOUR));
		System.out.println(cal.get(Calendar.MINUTE));
		System.out.println(cal.get(Calendar.SECOND));
	}
}

二、中级编程题

4、编写冒泡排序法。

package Test1;

public class Test4 {
	public static void main(String[] args) {
		int[] nums = new int[]{43,23,12,56,34,78,109};
		display(nums);
		bubbleSort(nums);
		display(nums);
	}
	
	public static void bubbleSort(int[] nums){
		for(int i = 0; i < nums.length; i++) {
			for(int j = 0; j < nums.length - i - 1;j++) {
				if(nums[j] > nums[j+1]) {
					int temp = nums[j];
					nums[j] = nums[j+1];
					nums[j+1] = temp;
				}
			}
		}
	}
	
	public static void display(int[] nums) {
		for(int i = 0; i < nums.length; i++) {
			System.out.print(nums[i] + ",");
		}
		System.out.println();
	}
}

5、用Java代码实现堆栈。

package Test1;

public class Test5 {
	public static void main(String[] args) throws Exception {
		Stack stack=new Stack(5);
		stack.push(1);
		stack.push(2);
		stack.push(3);
		stack.push(4);
		stack.push(5);
		while(stack.top>=0)
		{
			System.out.println(stack.pop());
		}		
	}
}

class Stack{
	int[] data;
	int top;
	int maxSize;
	public Stack(int maxSize){
		this.maxSize = maxSize;
		data = new int[maxSize];
		top = -1;
	}
	
	public boolean push(int data){
		if(top + 1 == maxSize) {
			System.out.println("栈满了");
			return false;
		}
		this.data[++top] = data;
		return true;
	}
	
	public int pop() throws Exception{
		if(top == -1){
			throw new Exception("栈空了");
		}
		return this.data[top--];
	}
}

6、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。

package Test1;

class TestThread{
	private int j;
	public synchronized void inc(){
		j++;
		System.out.println(Thread.currentThread().getName() + "-inc:" + j);
	}
	public synchronized void dec(){
		j--;
		System.out.println(Thread.currentThread().getName() + "-dec:" + j);
	}

}

public class Test6{
	public static void main(String[] args){
		TestThread t=new TestThread();
		for (int i = 0; i < 2; i++){
			Thread inc=new Thread(new Inc(t));
			Thread dec=new Thread(new Dec(t));
			inc.start();
			dec.start();
		}
	}
}


class Inc implements Runnable{
	private TestThread obj;
	public Inc(TestThread obj){
		this.obj=obj;
	}
	public void run(){
		this.obj.inc();
	}
}
class Dec implements Runnable{
	private TestThread obj;
	public Dec(TestThread obj){
		this.obj=obj;
	}
	public void run(){
		this.obj.dec();
	}
}

	

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mind_programmonkey

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值