pridas 发表于 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;
      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啊。

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


Angel丶L 发表于 2015-2-5 11:56:32

看着就这么复杂啊唉。。

花生 发表于 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);
        }

}

花生 发表于 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);
        }

}

花生 发表于 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);
        }

}

花生 发表于 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);
        }
}

花生 发表于 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()));
        }

}

花生 发表于 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());
                }
        }

}

花生 发表于 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")));
                        }
                }
      }
}

花生 发表于 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);
        }

}

花生 发表于 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();
        }

}

花生 发表于 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");
        }
}

wwwxinyu1990 发表于 2015-2-11 17:55:21

wwwxinyu1990 发表于 2015-1-28 16:10
No1:
No2:



谢谢!那我慢慢去答吧{:1_1:}

月之吟 发表于 2015-2-17 21:44:45

本帖最后由 月之吟 于 2015-2-19 12:57 编辑

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

        static int age(int i) {
                if (i == 1) {
                        return 10;
                } else {
                        return age(i - 1) + 2;
                }
        }
}
No 12
在微秒那行的24后加'L'
public class No12 {
      public static void main(String[] args) {
                final long PER_DAY_US = 24L * 60 * 60 * 1000 * 1000;
                final long PER_DAY_MS = 24 * 60 * 60 * 1000;
                System.out.println(PER_DAY_US / PER_DAY_MS);
      }
}No 13
将 j++ 改为 ++j 。
public class No13 {
      public static void main(String[] args) {
                int j = 0;
                for (int i = 0; i < 100; i++) {
                        j = ++j;
                }
                System.out.println("j = " + j);
      }
}No 14
将 for 循环中添加 i 等于 end 时用 break 跳出的语句,并将for中的i<=end去掉。应该算对吧……
public class No14 {
      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 (int i = start;; i++) {
                        count++;
                        if(i==end){
                              break;
                        }
                }
                System.out.println(" count = " + count);
      }

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

public class No18 {
      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);
      }

}

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





百日维新 发表于 2015-3-1 15:51:01

wwwxinyu1990 发表于 2015-1-28 16:10
No1:
No2:



快点回复你的地址给我,发奖品给你

百日维新 发表于 2015-3-1 15:51:36

曾经的肆无忌惮 发表于 2015-1-30 17:47
学的还是太少了,只做出了这么几题,还有四题的知识点还没自 ...

快点回复你的地址给我,发奖品给你

百日维新 发表于 2015-3-1 15:52:15

pridas 发表于 2015-2-3 23:53
11题:
public class Test11 {



快点回复你的地址给我,发奖品给你

百日维新 发表于 2015-3-1 15:52:33

月之吟 发表于 2015-2-17 21:44
No 11
这个失误了
No 12


快点回复你的地址给我,发奖品给你

月之吟 发表于 2015-3-11 22:10:10

百日维新 发表于 2015-3-1 15:52
快点回复你的地址给我,发奖品给你

等级不够......没法回复消息......再加上之前返校开学......现在是不是已经晚了......{:9_229:}

百日维新 发表于 2015-3-12 11:05:37

月之吟 发表于 2015-3-11 22:10
等级不够......没法回复消息......再加上之前返校开学......现在是不是已经晚了......

不清楚啊,你把地址留下看看小甲鱼怎么说
页: 1 [2] 3
查看完整版本: #鱼C五周年嘉年华# 《JAVA程序设计&改错》# 第二章