|
发表于 2015-1-28 16:10:11
|
显示全部楼层
No1:
- /**
- * 程序设计,第1个人10岁,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
- * @author wwwxinyu1990
- *
- */
- public class Age {
- public static void main(String[] args) {
- System.out.println(getAge(8));
- }
-
- protected static int getAge(int curNum) {
- if (curNum == 1) {
- return 10;
- } else {
- return getAge(curNum - 1) + 2;
- }
-
- }
- }
复制代码 No2:
- /**
- * 程序改错:用一天的微妙数除以一天的毫秒数,输出1000
- * @author wwwxinyu1990
- *
- */
- public class Test {
- public static void main(String[] args) {
- final long PER_DAY_US = 24*60*60*1000*1000L ;//微秒
- final long PER_DAY_MS = 24*60*60*1000L; //毫秒
- System.out.println(PER_DAY_US/PER_DAY_MS);
- }
- }
复制代码
No3:
- /**
- * 程序改错:输出j = 100
- * @author wwwxinyu1990
- *
- */
- public class Test {
- public static void main(String[] args) {
- int j = 0;
- for(int i=0 ;i < 100; i++){
- // 第一种方法:
- // j = ++j;
- // 第二种方法:
- j++;
- }
- System.out.println("j = " + j);
- }
- }
复制代码
No4:
- /**
- * 程序改错:输出 count = 101
- * @author wwwxinyu1990
- *
- */
- public class Test {
- private final static int end = Integer.MAX_VALUE;
- private final static int start = Integer.MAX_VALUE - 100;
-
- public static void main(String[] args) {
- int count = 0;
- for(long i=start; i <= end ; i++){
- count ++;
- }
- System.out.println(" count = "+ count);
- }
- }
复制代码
No5:- import java.util.Calendar;
- /**
- * 程序设计:输出昨天的当前日期
- * @author wwwxinyu1990
- *
- */
- public class Yesterday {
- public static void main(String[] args) {
- Calendar calendar = Calendar.getInstance();
- calendar.add(Calendar.DATE, -1);
- System.out.println(calendar.getTime());
- }
- }
复制代码
No6:
- import java.io.File;
- /**
- * 输出c盘下面的所有目录和文件(就是打开电脑c盘看到的所以东西,不用遍历子目录)
- * @author wwwxinyu1990
- *
- */
- public class ViewFile {
- public static void main(String[] args) {
- File file = new File("C:" + File.separatorChar);
- if (file.exists()) {
- File[] files = file.listFiles();
- for (File fe : files) {
- System.out.println(fe.getName());
- }
- }
- }
- }
复制代码
No7:
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FilenameFilter;
- /**
- * 编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,
- * 并将原来文件的扩展名从.java改为.jad。
- * @author wwwxinyu1990
- *
- */
- public class FileCopy {
- public static void main(String[] args) {
- // 拷贝元路径
- String metaDir = "D:" + File.separator + "java" + File.separator;
- // 目标路径
- String targetDir = "D:" + File.separator + "jad" + File.separator;
- // 拷贝
- copyAllFile(metaDir, targetDir);
- }
-
- /**
- * 将oldPath路径下的java文件拷贝到newPath路径下,并将扩展名java改为jad。
- * @param oldPath 元文件路径
- * @param newPath 目标文件路径
- */
- public static void copyAllFile(String oldPath, String newPath) {
- try {
- (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
- File oldFiles = new File(oldPath);
- // 获取元文件目录下的所有java文件的文件名
- String[] files = oldFiles.list(
- new FilenameFilter() {
- public boolean accept(File dir, String name) {
- if (name.endsWith(".java")) {
- return true;
- }
- return false;
- }
- });
- File temp=null;
- for (int i = 0; i < files.length; i++) {
- if(oldPath.endsWith(File.separator)){
- temp=new File(oldPath+files[i]);
- } else{
- temp=new File(oldPath+File.separator+files[i]);
- }
- if(temp.isFile()){
- // 拷贝文件的输入流
- FileInputStream input = new FileInputStream(temp);
- // 新文件路径
- String newFilePath = null;
- if(newPath.endsWith(File.separator)){
- newFilePath = newPath+temp.getName().replace("java", "jad");
- } else{
- newFilePath = newPath+File.separator+temp.getName().replace("java", "jad");
- }
- // 文件的输出流
- FileOutputStream output = new FileOutputStream(newFilePath);
- byte[] b = new byte[1024 * 5];
- int len;
- while ( (len = input.read(b)) != -1) {
- output.write(b, 0, len);
- }
- output.flush();
- // 关闭输出文件流
- if (output !=null) {
- output.close();
- }
- // 关闭输入文件流
- if (input != null) {
- input.close();
- }
- }
- }
- } catch (Exception e) {
- System.out.println("复制整个文件夹内容操作出错");
- e.printStackTrace();
- }
- }
- }
复制代码
No8:
- import java.util.Random;
- /**
- * 程序改错:输出word = Pain 或 Gain 或 Main
- * @author wwwxinyu1990
- *
- */
- public class TestFishc {
- private static Random rdm = new Random();
- public static void main(String[] args) {
- StringBuffer word = null;
-
- switch (rdm.nextInt(3)) {
- case 1:
- word = new StringBuffer("P");
- break;
- case 2:
- word = new StringBuffer("G");
- break;
- default:
- word = new StringBuffer("M");
- break;
- }
- word.append('a');
- word.append('i');
- word.append('n');
-
- System.out.println("word = " + word);
- }
- }
复制代码
No9:
- /**
- * 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保
- 证汉字不被截取半个,如"我ABC",4,应该截取"我AB",输入"我AB汉CDEF",6,应该输出"我AB",
- 而不是"我ABC+汉的半个"(请说明源文件采用的编码标准,便于判题)。
- 编码 : GBK
- * @author wwwxinyu1990
- */
- public class TruncateString {
- public static void main(String[] args) {
- spltString("我ABC", 4);
- spltString("我AB汉CDEF", 6);
- spltString("DEF加油EFG", 6);
- }
-
- public static void spltString(String src, int length) {
- if (src == null || src.length() < 1) {
- System.out.println("要截取的字符串是空的,请重新指定。");
- return;
- }
-
- if (length > src.length()) {
- length = src.length();
- }
-
- byte[] srcBytes = src.getBytes();
- String subStr = "";
- if (srcBytes[length] < 1) {
- subStr = new String(srcBytes, 0, --length);
- System.out.println("截取的字符串是:" + subStr);
- } else {
- subStr = new String(srcBytes, 0, length);
- System.out.println("截取的字符串是:" + subStr);
- }
-
- }
- }
复制代码
No10:
- public class ThreadTest {
- // 采用 Runnable 接口方式创建的多条线程可以共享实例属性
- private int j;
- // 同步增加方法
- private synchronized void inc() {
- j++;
- System. out .println(Thread.currentThread().getName()+ "--increase--" + j);
- }
-
- // 同步减算方法
- private synchronized void dec() {
- j--;
- System. out .println(Thread.currentThread().getName()+ "--decrease--" + j);
- }
-
- // 增加线程
- class Inc implements Runnable {
- public void run() {
- for (int i = 0; i < 10; i++) {
- inc();
- }
- }
- }
- // 减算线程
- class Dec implements Runnable{
- public void run() {
- for (int i = 0; i < 10; i++) {
- dec();
- }
- }
- }
-
- public static void main(String[] args) {
- ThreadTest t = new ThreadTest();
-
- // 内部类的实例化
- Inc inc = t. new Inc();
- Dec dec = t. new Dec();
-
- // 创建 2*n 个线程 此处 n=2
- for ( int i = 0; i < 2; i++) {
- new Thread(inc).start();
- new Thread(dec).start();
- }
-
- }
- }
复制代码
谢谢!
|
|