鱼C论坛

 找回密码
 立即注册
楼主: 百日维新

[技术交流] #鱼C五周年嘉年华# 《JAVA程序设计&改错》# 第二章

[复制链接]
发表于 2015-2-3 23:53:37 | 显示全部楼层
11题:
public class Test11 {

public static void main(String[] args) {
  System.out.println(calcAge(1, 10, 8));
}

static int calcAge(int n, int age, int end){
  if (n >= end)
   return age;
  return calcAge(n + 1, age + 2, end);
}
}


12题:
public class Test12 {
public static void main(String[] args) {
  final long PER_DAY_US = 24L * 60 * 60 * 1000 * 1000;// ΢��
  final long PER_DAY_MS = 24L * 60 * 60 * 1000; // ����
  System.out.println(PER_DAY_US / PER_DAY_MS);
}
}

13题:
public class Test13 {
public static void main(String[] args) {
  int j = 0;
  for (int i = 0; i < 100; i++) {
   j++;
  }
  System.out.println("j = " + j);
}
}

14:
public class Test14 {
private final static int end = Integer.MAX_VALUE - 1;
private final static int start = Integer.MAX_VALUE - 101;
public static void main(String[] args) {
  int count = 0;
  for (int i = start; i <= end; i++) {
   count++;
  }
  System.out.println(" count = " + count);
}
}


15:
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test15 {
public static void main(String[] args) {
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  System.out.println(format.format(new Date(new Date().getTime()-1000*24*3600)));
}
}

16:
import java.io.File;

public class Test16 {
public static void main(String[] args) {
  File dir = new File("c:\\");
//  System.out.println(dir.isDirectory());
  for(String s : dir.list())
   System.out.println(s);
}
}


17:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Test17 {
public static void main(String[] args) {
  
  File dirSrc = new File("d:\\java\\");
  File dirDest = new File("d:\\jad\\");
  if(!dirDest.exists()){
   dirDest.mkdirs();
  }
  if(!dirDest.isDirectory()){
   System.out.println("there exists a file named jad");
   return;
  }
  FileOutputStream fos = null;
  FileInputStream fis = null;
  
  for(File f : dirSrc.listFiles()){
   if (f.isFile() && f.getName().endsWith(".java")) {
    String path = dirDest.getAbsolutePath() + File.separator
      + f.getName().replaceAll("\\.java", "\\.jad");
    File destFile = new File(path);
    try {
     if (!destFile.createNewFile())
      System.out.println(path+" has been exist. nothing was done.");
     else
      System.out.println(path+" has been copied.");
      
     fos = new FileOutputStream(destFile);
     fis = new FileInputStream(f);
     byte[] content = read(fis);
     fos.write(content);
     fos.flush();
    } catch (IOException e) {
     e.printStackTrace();
    } finally {
     if(fos != null){
      try {
       fos.close();
      } catch (IOException e) {
       e.printStackTrace();
      }
     } if(fis != null){
      try {
       fis.close();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }
    }
   }
  }
  System.out.println("over");
}

    public static byte[] read(InputStream in) throws IOException {
        byte[] buf = new byte[1024];
        int length = 0;
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        while ((length = in.read(buf, 0, buf.length)) > 0) {
            bout.write(buf, 0, length);
        }
        bout.flush();
        return bout.toByteArray();
    }
   
   
}

18:
import java.util.Random;
public class Test18 {
        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");
                }
                word.append('a');
                word.append('i');
                word.append('n');
               
                System.out.println("word = "+word);
        }
}


19:
import java.io.UnsupportedEncodingException;

public class Test19 {
public static void main(String[] args) {
  try {
   System.out.println(subStr("我ABCDEF", 4, "utf8"));
   System.out.println(subStr("我AB汉CDEF", 6, "utf8"));
   System.out.println(subStr("我AB汉CDEF", 7, "utf8"));
   System.out.println(subStr("我AB汉CDEF", 8, "utf8"));
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
}

static String subStr(String orig, int num, String charset) throws UnsupportedEncodingException{
  int count = 0;
  int i=0;
  for (; i<orig.length(); i++){
   Character c = orig.charAt(i);
   count = count + (c.toString().getBytes(charset).length);
   if (count > num) break;
  }
  return orig.substring(0, i);
}
}


20:
不会(‘;ω;&acute;)。
容我翻课本复习一下。
能不能直接AtomicInteger啊。

老师你好,我是来拿阳光普照奖的。


点评

鱼同学,java活动全部更新了  发表于 2015-2-10 09:29
兄弟叫我小新就行了,随便把第一章也做了吧!  发表于 2015-2-4 00:27
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-5 11:56:32 | 显示全部楼层
看着就这么复杂啊唉。。

点评

你只是没有学过java而已,不难的  发表于 2015-2-5 12:16
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-9 20:32:17 | 显示全部楼层
No11:程序设计,第1个人10岁,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
package 小甲鱼活动;

public class _2_11
{
        // 第1个人10岁,第2个比第1个人大2岁,第3个人比第2个人大3岁,依次递推
        // 10 12 15 19 24 30 37 45
        public static void main(String[] args)
        {
                System.out.println("第八个人是: " + getYearsOld(2, 10, 8) + "岁");
        }

        public static int getYearsOld(int i, int yearsOld, int size)
        {
                if (i > size)
                        return yearsOld;
                return getYearsOld(i + 1, yearsOld + i, 8);
        }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-9 20:42:21 | 显示全部楼层
No12:程序改错 (10分)
用一天的微妙数除以一天的毫秒数,输出1000
package 小甲鱼活动;

public class _2_12
{

        public static void main(String[] args)
        {
                //分别加上L转换成long型
                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);
        }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-9 20:43:16 | 显示全部楼层
No13: 程序改错,输出j = 100
package 小甲鱼活动;

public class _2_13
{

        public static void main(String[] args)
        {
                int j = 0;
                for (int i = 0; i < 100; i++)
                {
                        //改正
                        j++;
                }
                System.out.println("j = " + j);
        }

}

点评

鱼同学,java活动全部更新了  发表于 2015-2-10 09:29
第三第四章已经更新了,去看看吧,这个待会给你打分!  发表于 2015-2-9 20:45
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-9 20:43:56 | 显示全部楼层
No14:程序改错,输出 count = 101
package 小甲鱼活动;

public class _2_14
{
        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;
                //将i改为long
                for (long i = start; i <= end; i++)
                {
                        count++;
                }
                System.out.println(" count = " + count);
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-9 20:45:55 | 显示全部楼层
No15: 程序设计,输出昨天的当前日期
package 小甲鱼活动;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class _2_15
{

        public static void main(String[] args)
        {
                Calendar cal = Calendar.getInstance();
                // cal.set(2008,2,1);
                //cal.set(2015, 0, 1);
                cal.add(Calendar.DAY_OF_YEAR, -1);
                SimpleDateFormat sfd = new SimpleDateFormat("昨天是:yyyy年MM月d日");
                System.out.println(sfd.format(cal.getTime()));
        }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-9 20:46:59 | 显示全部楼层
No16: 输出c盘下面的所有目录和文件(就是打开电脑c盘看到的所以东西,不用遍历子目录)
package 小甲鱼活动;

import java.io.File;

public class _2_16
{

        public static void main(String[] args)
        {
                File cFile = new File("c:\\");
                for (File file : cFile.listFiles())
                {
                        if (!file.isHidden())
                                System.out.println(file.getName());
                }
        }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-9 20:48:19 | 显示全部楼层
本帖最后由 花生 于 2015-2-9 21:27 编辑

No17:编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad。
package 小甲鱼活动;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class _2_17
{

        public static void main(String[] args) throws IOException
        {
                //需保证D盘下确实有java和jad这两个文件夹
                File sourceDir = new File("d:/java");
                Path newdir = Paths.get("d:", "jad");
                for (File file : sourceDir.listFiles())
                {
                        if (file.getName().endsWith(".java"))
                        {
                                Files.copy(
                                                file.toPath(),
                                                newdir.resolve(file.getName().replaceAll(".java",
                                                                ".jad")));
                        }
                }
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-9 20:48:56 | 显示全部楼层
No18: 程序改错,输出word = Pain 或 Gain 或 Main
package 小甲鱼活动;

import java.util.Random;

public class _2_18
{
        private static Random rdm = new Random();

        public static void main(String[] args)
        {
                StringBuffer word = null;

                //将StringBuffer中的char改成String,并加上break
                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);
        }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-9 21:21:42 | 显示全部楼层
No20:程序设计,设计4个线程,两个线程对j减1,另两个线程对j加1
Resource类
package 小甲鱼活动;

public class _2_20_Resource
{
        private int j = 0;

        public _2_20_Resource(int j)
        {
                this.j = j;
        }

        public int getJ()
        {
                return j;
        }

        public void setJ(int j)
        {
                this.j = j;
        }

        public synchronized void add()
        {
                j++;
                System.out.println("增加1 j=" + j);
        }

        public synchronized void sub()
        {
                j--;
                System.out.println("减少1 j=" + j);
        }
}
用于增加的类
package 小甲鱼活动;

public class _2_20_Adder extends Thread
{
        private _2_20_Resource resource;

        public _2_20_Adder(_2_20_Resource resource)
        {
                this.resource = resource;
        }

        public _2_20_Resource getResource()
        {
                return resource;
        }

        public void setResource(_2_20_Resource resource)
        {
                this.resource = resource;
        }

        @Override
        public void run()
        {
                while (true)
                {
                        resource.add();
                        try
                        {
                                Thread.sleep(500);
                        }
                        catch (InterruptedException e)
                        {
                                e.printStackTrace();
                        }
                }
        }
}
用于减少的类
package 小甲鱼活动;

public class _2_20_Suber extends Thread
{
        private _2_20_Resource resource;

        public _2_20_Suber(_2_20_Resource resource)
        {
                this.resource = resource;
        }

        public _2_20_Resource getResource()
        {
                return resource;
        }

        public void setResource(_2_20_Resource resource)
        {
                this.resource = resource;
        }

        @Override
        public void run()
        {
                while (true)
                {
                        resource.sub();
                        try
                        {
                                Thread.sleep(500);
                        }
                        catch (InterruptedException e)
                        {
                                e.printStackTrace();
                        }
                }
        }
}
Main类
package 小甲鱼活动;

public class _2_20
{

        public static void main(String[] args)
        {
                _2_20_Resource resource = new _2_20_Resource(1);
                _2_20_Adder adder1 = new _2_20_Adder(resource);
                _2_20_Adder adder2 = new _2_20_Adder(resource);
                _2_20_Suber suber1 = new _2_20_Suber(resource);
                _2_20_Suber suber2 = new _2_20_Suber(resource);

                adder1.start();
                suber1.start();
                adder2.start();
                suber2.start();
        }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-9 22:01:30 | 显示全部楼层
No19: 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保
证汉字不被截取半个,如"我ABC",4,应该截取"我AB",输入"我AB汉CDEF",6,应该输出"我
AB",而不是"我ABC+汉的半个"(请说明源文件采用的编码标准,便于判题)。

package 小甲鱼活动;

import java.io.UnsupportedEncodingException;

public class _2_19
{
        // GBK编码
        public static void main(String[] args) throws UnsupportedEncodingException
        {
                String str1 = "我ABC你";
                System.out.println(str1 + " 截取为: " + subString(str1, 6));
                String str2 = "我A汉bcDEF";
                System.out.println(str2 + " 截取为: " + subString(str2, 4));
                String str3 = "我";
                System.out.println(str3 + " 截取为: " + subString(str3, 1));
                String str4 = "我你";
                System.out.println(str4 + " 截取为: " + subString(str4, 3));
        }

        public static String subString(String target, int size)
                        throws UnsupportedEncodingException
        {
                // 得到GBK编码的字符数组,一个汉字占两个字节
                byte[] bytes = target.getBytes("GBK");
                if (0 >= size || size > bytes.length)
                        return target;
                int amount = 0;
                int i = size;
                //向左边走,从本位开始到开头或第一个非汉字间有多少位是存汉字的
                while (0 < i && bytes[--i] < 0)
                {
                        amount++;
                }
                if (0 != (amount & 1))
                {
                        size--;
                }
                return new String(bytes,0,size,"GBK");
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-11 17:55:21 | 显示全部楼层

  谢谢!那我慢慢去答吧{:1_1:}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-2-17 21:44:45 | 显示全部楼层
本帖最后由 月之吟 于 2015-2-19 12:57 编辑

No 11
这个失误了:lol:
  1. public class No11 {
  2.         public static void main(String[] aegs) {
  3.                 System.out.println("第8个人的年龄为" + age(8));
  4.         }

  5.         static int age(int i) {
  6.                 if (i == 1) {
  7.                         return 10;
  8.                 } else {
  9.                         return age(i - 1) + 2;
  10.                 }
  11.         }
  12. }
复制代码

No 12
在微秒那行的24后加'L'
  1. public class No12 {
  2.         public static void main(String[] args) {
  3.                 final long PER_DAY_US = 24L * 60 * 60 * 1000 * 1000;
  4.                 final long PER_DAY_MS = 24 * 60 * 60 * 1000;
  5.                 System.out.println(PER_DAY_US / PER_DAY_MS);
  6.         }
  7. }
复制代码
No 13
将 j++ 改为 ++j 。
  1. public class No13 {
  2.         public static void main(String[] args) {
  3.                 int j = 0;
  4.                 for (int i = 0; i < 100; i++) {
  5.                         j = ++j;
  6.                 }
  7.                 System.out.println("j = " + j);
  8.         }
  9. }
复制代码
No 14
将 for 循环中添加 i 等于 end 时用 break 跳出的语句,并将for中的i<=end去掉。应该算对吧……
  1. public class No14 {
  2.         private final static int end = Integer.MAX_VALUE;
  3.         private final static int start = Integer.MAX_VALUE - 100;

  4.         public static void main(String[] args) {
  5.                 int count = 0;
  6.                 for (int i = start;; i++) {
  7.                         count++;
  8.                         if(i==end){
  9.                                 break;
  10.                         }
  11.                 }
  12.                 System.out.println(" count = " + count);
  13.         }

  14. }
复制代码

No 18
将所有case最后都加上break,将StringBuffer()中的单引号都改成双引号。
  1. import java.util.Random;

  2. public class No18 {
  3.         private static Random rdm = new Random();

  4.         public static void main(String[] args) {
  5.                 StringBuffer word = null;
  6.                 switch (rdm.nextInt(3)) {
  7.                 case 1:
  8.                         word = new StringBuffer("P");
  9.                         break;
  10.                 case 2:
  11.                         word = new StringBuffer("G");
  12.                         break;
  13.                 default:
  14.                         word = new StringBuffer("M");
  15.                 }
  16.                 word.append('a');
  17.                 word.append('i');
  18.                 word.append('n');
  19.                 System.out.println("word = " + word);
  20.         }

  21. }
复制代码


目前我的水平就只会这些了……
重在参与,继续学习进步





点评

加油,你努力一下你就可以拿u盘了!  发表于 2015-2-18 00:47
+0 + 10 + 10 + 10 + 15,No10必须用递归,你这个不是递归  发表于 2015-2-17 23:38
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-1 15:51:01 | 显示全部楼层

快点回复你的地址给我,发奖品给你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-1 15:51:36 | 显示全部楼层
曾经的肆无忌惮 发表于 2015-1-30 17:47
学的还是太少了,只做出了这么几题,还有四题的知识点还没自 ...

快点回复你的地址给我,发奖品给你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-1 15:52:15 | 显示全部楼层
pridas 发表于 2015-2-3 23:53
11题:
public class Test11 {

快点回复你的地址给我,发奖品给你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-1 15:52:33 | 显示全部楼层
月之吟 发表于 2015-2-17 21:44
No 11
这个失误了
No 12

快点回复你的地址给我,发奖品给你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-3-11 22:10:10 | 显示全部楼层
百日维新 发表于 2015-3-1 15:52
快点回复你的地址给我,发奖品给你

等级不够......没法回复消息......再加上之前返校开学......现在是不是已经晚了......
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-12 11:05:37 From FishC Mobile | 显示全部楼层
月之吟 发表于 2015-3-11 22:10
等级不够......没法回复消息......再加上之前返校开学......现在是不是已经晚了......

不清楚啊,你把地址留下看看小甲鱼怎么说
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 06:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表