鱼C论坛

 找回密码
 立即注册
查看: 7098|回复: 63

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

[复制链接]
发表于 2015-1-27 20:19:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 百日维新 于 2015-2-25 02:13 编辑

java.jpg

答题注明题号,提交完整的程序

No11:程序设计,第1个人10岁,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大? (10分)


No12:程序改错 (10分)
用一天的微妙数除以一天的毫秒数,输出1000
public class Test {

        public static void main(String[] args) {
                final long PER_DAY_US = 24*60*60*1000*1000 ;//微秒
                final long PER_DAY_MS = 24*60*60*1000; //毫秒
                System.out.println(PER_DAY_US/PER_DAY_MS);
        }
}



No13: 程序改错,输出j = 100 (10分)
public class Test {

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

No14:程序改错,输出 count = 101 (10分)
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(int i=start; i <= end ; i++){
                                count ++;
                        }
                        System.out.println(" count = "+ count);
        }

}






No15: 程序设计,输出昨天的当前日期 (10分)



No16: 输出c盘下面的所有目录和文件(就是打开电脑c盘看到的所以东西,不用遍历子目录)(15分)



No17:编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad。(15分)

No18: 程序改错,输出word = Pain 或 Gain 或 Main (15分)
import java.util.Random;
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');
                        
                case 2:
                        
                        word = new StringBuffer('G');
                        
                default:
                        
                        word = new StringBuffer('M');
                        
                }
                word.append('a');
                word.append('i');
                word.append('n');
               
                System.out.println("word = "+word);
        }
}


No19
: 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保
证汉字不被截取半个,如"我ABC",4,应该截取"我AB",输入"我AB汉CDEF",6,应该输出"我
AB",而不是"我ABC+汉的半个"(请说明源文件采用的编码标准,便于判题)。(20分)




No20:程序设计,设计4个线程,两个线程对j减1,另两个线程对j加1(20分)

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

使用道具 举报

发表于 2015-1-27 21:13:45 | 显示全部楼层
求中奖啊!

点评

肯定中啊,壮士这么厉害  发表于 2015-1-27 21:37
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-27 21:14:40 | 显示全部楼层

回帖奖励 +3 鱼币

呃,呃,呃(⊙o⊙)…
人品这么不行啊。。。。

点评

兄弟,java活动全部更新了  发表于 2015-2-10 09:27
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-1-27 21:15:04 | 显示全部楼层

肯定中啊,壮士这么厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-27 21:32:29 | 显示全部楼层
我来看看

点评

欢迎参加  发表于 2015-1-27 21:36
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-27 21:56:01 | 显示全部楼层
顶顶 很不错 力挺     顶顶 很不错 力挺

点评

欢迎参加活动  发表于 2015-1-27 21:56
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-27 21:57:00 | 显示全部楼层
顶顶 很不错 力挺     顶顶 很不错 力挺
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-28 10:09:10 | 显示全部楼层
No12
  1. public class Test {

  2.     public static void main(String[] args) {
  3.             final long PER_DAY_US = (long)24*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. }
复制代码

点评

鱼同学,java活动全部更新了  发表于 2015-2-10 09:28
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-28 10:26:24 | 显示全部楼层
No13
  1. public class Test {

  2. public static void main(String[] args) {
  3.         int j = 0;
  4.         for(int i=0 ;i < 100;i++){
  5.                 j ++;
  6.         }
  7.         System.out.println("j = "+j);
  8. }
  9. }
复制代码



No11
  1. public class P2_11 {
  2.         public static void main(String[] args) {
  3.         int age1;
  4.         age1=age(8);
  5.         System.out.println("第8个人的年龄是:"+age1);
  6.         }
  7.         static int age(int num){
  8.                 if(num==1){
  9.                         return 10;
  10.                 }else{
  11.                         return age(num-1)+2;
  12.                 }
  13.         }
  14. }
复制代码

No14
  1. public class Test {
  2.         private final static int  end  = Integer.MAX_VALUE;
  3.         private final static int  start  = Integer.MAX_VALUE - 100;
  4.         
  5.         public static void main(String[] args) {
  6.                         int count = 0;
  7.                         for(long i=start; i <= end ; i++){
  8.                                 count ++;
  9.                         }
  10.                         System.out.println(" count = "+ count);
  11.         }

  12. }
复制代码



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

使用道具 举报

发表于 2015-1-28 10:40:44 | 显示全部楼层
No18
  1. import java.util.Random;
  2. public class TestFishc {
  3.         private static Random rdm = new Random();
  4.         public static void main(String[] args) {
  5.                 StringBuffer word = null;
  6.             
  7.              switch (rdm.nextInt(3)) {
  8.      
  9.              case 1:
  10.                      
  11.                      word = new StringBuffer("P");
  12.                      break;
  13.                      
  14.              case 2:
  15.                      
  16.                      word = new StringBuffer("G");
  17.                      break;
  18.                      
  19.              default:
  20.                      
  21.                      word = new StringBuffer("M");
  22.                      break;
  23.                      
  24.              }
  25.              word.append('a');
  26.              word.append('i');
  27.              word.append('n');
  28.             
  29.              System.out.println("word = "+word);
  30.         }
  31. }
复制代码



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

使用道具 举报

发表于 2015-1-28 10:56:15 | 显示全部楼层
No15
  1. import java.text.SimpleDateFormat;
  2. import java.util.*;
  3. public class P2_15 {
  4.         public static void main(String[] args) {
  5.                 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  6.                 Calendar c = Calendar.getInstance();   
  7.                 int d = c.get(Calendar.DAY_OF_MONTH);  
  8.                 --d;                                   
  9.                 c.set(Calendar.DAY_OF_MONTH, d);      
  10.                 System.out.println(df.format(c.getTime()));   
  11.         }
  12. }
复制代码



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

使用道具 举报

发表于 2015-1-28 11:05:55 | 显示全部楼层
本帖最后由 小龙_h 于 2015-1-28 11:09 编辑

第一版写好了,求审判,哪里写的不好我再改。
  1. package com.xl.fichc.fiveAnniversary;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.UnsupportedEncodingException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.Random;
  10. import java.util.Scanner;

  11. public class FiveAnniversaryTest2 {

  12.         /**
  13.          * @param args
  14.          * @author xiaolong
  15.          */
  16.         
  17. //        No11:程序设计,第1个人10岁,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大? (10分)
  18.         
  19.         public static void main(String[] args) {
  20.                 System.out.println("第8个人"+age(8)+"岁啦!");
  21.         }
  22.         public static int age(int n){
  23.                 if(n==1){
  24.                         return 10;
  25.                 }
  26.                 return age(n-1) + 2;
  27.         }
  28.         
  29. //        No12:程序改错 (10分)用一天的微妙数除以一天的毫秒数,输出1000
  30.         
  31.     /*public static void main(String[] args) {
  32.         final long PER_DAY_US = 24*60*60*1000*1000L ;//微秒
  33.         final long PER_DAY_MS = 24*60*60*1000L; //毫秒
  34.         System.out.println(PER_DAY_US/PER_DAY_MS);
  35.     }*/
  36.    
  37. //        No13: 程序改错,输出j = 100 (10分)
  38.     /*public static void main(String[] args) {
  39.         int j = 0;
  40.         for(int i=0 ;i < 100;i++){
  41.                 j++;
  42.         }
  43.         System.out.println("j = "+j);
  44.     }*/

  45. //        No14:程序改错,输出 count = 101 (10分)
  46.    /* private final static int  end  = Integer.MAX_VALUE;
  47.     private final static int  start  = Integer.MAX_VALUE - 100;
  48.     public static void main(String[] args) {
  49.         int count = 0;
  50.         for(long i=start; i <= end ; i++){
  51.                 count ++;
  52.         }
  53.         System.out.println(" count = "+ count);
  54.     }*/

  55. //        No15: 程序设计,输出昨天的当前日期 (10分)
  56.         /*public static void main(String[] args) {
  57.                 Date date = new Date();
  58.                 long time = date.getTime()-24*60*60*1000;
  59.                 Date result = new Date(time);
  60.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  61.                 System.out.println(sdf.format(result));
  62.         }*/


  63. //        No16: 输出c盘下面的所有目录和文件(就是打开电脑c盘看到的所以东西,不用遍历子目录)(15分)
  64.         /*public static void main(String[] args) {
  65.                 File root = new File("c:\");
  66.                 File[] list = root.listFiles();
  67.                 for(File file:list){
  68.                         System.out.println(file.getName());
  69.                 }
  70.         }*/


  71. //        No17:编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad。(15分)
  72.         /*public static void main(String[] args) {
  73.                 File sourRoot = new File("d:\\java\");
  74.                 File destRoot = new File("d:\\jad\");
  75.                 if(!sourRoot.exists())
  76.                         System.out.println("您的目录"+sourRoot.getPath()+"\\不存在");
  77.                 else{
  78.                         if(!destRoot.exists()) destRoot.mkdirs();
  79.                         File[] fileList = sourRoot.listFiles();
  80.                         String filename = "";
  81.                         if(fileList.length==0) System.out.println(sourRoot.getPath()+"\\目录中没有.java文件");
  82.                         for(File file : fileList){
  83.                                 if(file.getName().endsWith(".java")){
  84.                                         filename = file.getName().substring(0, file.getName().lastIndexOf("."));
  85.                                         copy(file.getPath(),destRoot.getPath()+"\"+filename+".jad");
  86.                                 }
  87.                         }
  88.                 }
  89.         }
  90.         public static void copy(String sourPath,String destPath){
  91.                 try {
  92.                         FileInputStream fis = new FileInputStream(sourPath);
  93.                         FileOutputStream fos = new FileOutputStream(destPath);
  94.                         byte[] buf = new byte[1024];
  95.                         int temp = 0;
  96.                         while((temp=fis.read(buf))!=-1){
  97.                                 fos.write(buf, 0, temp);
  98.                         }
  99.                         fos.flush();
  100.                         fis.close();
  101.                         fos.close();
  102.                 } catch (Exception e) {
  103.                         e.printStackTrace();
  104.                 }
  105.                 System.out.println("复制文件"+sourPath+" ---> "+destPath+"成功!");
  106.         }*/
  107.         
  108. //        No18: 程序改错,输出word = Pain 或 Gain 或 Main (15分)
  109.     /*private static Random rdm = new Random();
  110.     public static void main(String[] args) {
  111.         StringBuffer word = null;
  112.         switch (rdm.nextInt(3)) {
  113.         case 1:
  114.             word = new StringBuffer("P");
  115.             break;
  116.         case 2:
  117.             word = new StringBuffer("G");
  118.             break;
  119.         default:
  120.             word = new StringBuffer("M");
  121.         }
  122.         word.append('a');
  123.         word.append('i');
  124.         word.append('n');
  125.         System.out.println("word = "+word);
  126.     }*/

  127. //        No19: 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保
  128. //        证汉字不被截取半个,如"我ABC",4,应该截取"我AB",输入"我AB汉CDEF",6,应该输出"我
  129. //        AB",而不是"我ABC+汉的半个"(请说明源文件采用的编码标准,便于判题)。(20分)
  130.         //        编码格式默认的是Unicode编码么?我木有改过啊。。。
  131.         /*public static void main(String[] args) {
  132.                 System.out.print("请输入字符串:");
  133.                 Scanner scanner = new Scanner(System.in);
  134.                 byte[] str = scanner.nextLine().getBytes();
  135.                 System.out.print("请输入字节数:");
  136.                 byte[] temp = new byte[scanner.nextInt()];
  137.                 for(int i=0;i<temp.length;i++){
  138.                         temp[i]=str[i];
  139.                 }
  140.                 if(temp.length>1){
  141.                         if(temp[temp.length-2]>0&&temp[temp.length-1]<0)
  142.                                 temp[temp.length-1]=0;
  143.                 }else{
  144.                         temp[0]=0;
  145.                 }
  146.                 try {
  147.                         System.out.println(new String(temp,"GBK"));
  148.                 } catch (UnsupportedEncodingException e) {
  149.                         e.printStackTrace();
  150.                 }
  151.         }*/

  152. //        No20:程序设计,设计4个线程,两个线程对j减1,另两个线程对j加1(20分)
  153.         /*private static int j=0;
  154.         public static void main(String[] args) {
  155.                 for(int i=0;i<2;i++){
  156.                         new Thread(){
  157.                                 public void run(){
  158.                                         add();
  159.                                 }
  160.                         }.start();
  161.                         new Thread(){
  162.                                 public void run(){
  163.                                         sub();
  164.                                 }
  165.                         }.start();
  166.                 }
  167.         }
  168.         public synchronized static void add(){
  169.                 j++;
  170.                 System.out.println("线程"+Thread.currentThread().getId()+"调用add()方法,j的值为:"+j);
  171.         }
  172.         public synchronized static void sub(){
  173.                 j--;
  174.                 System.out.println("线程"+Thread.currentThread().getId()+"调用sub()方法,j的值为:"+j);
  175.         }*/

  176. }
复制代码

点评

鱼同学,java活动全部更新了  发表于 2015-2-10 09:28
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-28 16:10:11 | 显示全部楼层
No1:
  1. /**
  2. * 程序设计,第1个人10岁,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
  3. * @author wwwxinyu1990
  4. *
  5. */
  6. public class Age {
  7.         public static void main(String[] args) {
  8.                 System.out.println(getAge(8));
  9.         }
  10.        
  11.         protected static int getAge(int curNum) {
  12.                 if (curNum == 1) {
  13.             return 10;
  14.         } else {
  15.             return getAge(curNum - 1) + 2;
  16.         }
  17.                
  18.         }
  19. }
复制代码
No2:
  1. /**
  2. * 程序改错:用一天的微妙数除以一天的毫秒数,输出1000
  3. * @author wwwxinyu1990
  4. *
  5. */
  6. public class Test {
  7.     public static void main(String[] args) {
  8.         final long PER_DAY_US = 24*60*60*1000*1000L ;//微秒
  9.         final long PER_DAY_MS = 24*60*60*1000L; //毫秒
  10.         System.out.println(PER_DAY_US/PER_DAY_MS);
  11.     }
  12. }
复制代码

No3:
  1. /**
  2. * 程序改错:输出j = 100
  3. * @author wwwxinyu1990
  4. *
  5. */
  6. public class Test {
  7.     public static void main(String[] args) {
  8.         int j = 0;
  9.         for(int i=0 ;i < 100; i++){
  10.                 // 第一种方法:
  11. //                j = ++j;
  12.                 // 第二种方法:
  13.                 j++;
  14.         }
  15.         System.out.println("j = " + j);
  16.     }
  17. }
复制代码

No4:
  1. /**
  2. * 程序改错:输出 count = 101
  3. * @author wwwxinyu1990
  4. *
  5. */
  6. public class Test {
  7.     private final static int  end  = Integer.MAX_VALUE;
  8.     private final static int  start  = Integer.MAX_VALUE - 100;
  9.    
  10.     public static void main(String[] args) {
  11.             int count = 0;
  12.             for(long i=start; i <= end ; i++){
  13.                     count ++;
  14.             }
  15.             System.out.println(" count = "+ count);
  16.     }
  17. }
复制代码

No5:
  1. import java.util.Calendar;

  2. /**
  3. * 程序设计:输出昨天的当前日期
  4. * @author wwwxinyu1990
  5. *
  6. */
  7. public class Yesterday {
  8.         public static void main(String[] args) {
  9.                 Calendar calendar = Calendar.getInstance();
  10.                 calendar.add(Calendar.DATE, -1);
  11.                 System.out.println(calendar.getTime());
  12.         }
  13. }
复制代码


No6:
  1. import java.io.File;

  2. /**
  3. * 输出c盘下面的所有目录和文件(就是打开电脑c盘看到的所以东西,不用遍历子目录)
  4. * @author wwwxinyu1990
  5. *
  6. */
  7. public class ViewFile {
  8.         public static void main(String[] args) {
  9.                 File file = new File("C:" + File.separatorChar);
  10.                 if (file.exists()) {
  11.                         File[] files = file.listFiles();
  12.                         for (File fe : files) {
  13.                                 System.out.println(fe.getName());
  14.                         }
  15.                 }
  16.         }
  17. }
复制代码

No7:
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.FilenameFilter;

  5. /**
  6. * 编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,
  7. *              并将原来文件的扩展名从.java改为.jad。
  8. * @author wwwxinyu1990
  9. *
  10. */
  11. public class FileCopy {
  12.         public static void main(String[] args) {
  13.                 // 拷贝元路径
  14.                 String metaDir = "D:" + File.separator + "java" + File.separator;
  15.                 // 目标路径
  16.                 String targetDir = "D:" + File.separator + "jad" + File.separator;
  17.                 // 拷贝
  18.                 copyAllFile(metaDir, targetDir);
  19.         }
  20.        
  21.         /**
  22.          * 将oldPath路径下的java文件拷贝到newPath路径下,并将扩展名java改为jad。
  23.          * @param oldPath 元文件路径
  24.          * @param newPath 目标文件路径
  25.          */
  26.         public static void copyAllFile(String oldPath, String newPath) {
  27.                 try {
  28.                         (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
  29.                         File oldFiles = new File(oldPath);
  30.                         // 获取元文件目录下的所有java文件的文件名
  31.                         String[] files = oldFiles.list(
  32.                                         new FilenameFilter() {
  33.                                                 public boolean accept(File dir, String name) {
  34.                                                         if (name.endsWith(".java")) {
  35.                                                                 return true;
  36.                                                         }
  37.                                                         return false;
  38.                                                 }
  39.                                         });
  40.                         File temp=null;

  41.                         for (int i = 0; i < files.length; i++) {
  42.                                 if(oldPath.endsWith(File.separator)){
  43.                                         temp=new File(oldPath+files[i]);
  44.                                 } else{
  45.                                         temp=new File(oldPath+File.separator+files[i]);
  46.                                 }

  47.                                 if(temp.isFile()){
  48.                                         // 拷贝文件的输入流
  49.                                         FileInputStream input = new FileInputStream(temp);
  50.                                         // 新文件路径
  51.                                         String newFilePath = null;
  52.                                         if(newPath.endsWith(File.separator)){
  53.                                                 newFilePath = newPath+temp.getName().replace("java", "jad");
  54.                                         } else{
  55.                                                 newFilePath = newPath+File.separator+temp.getName().replace("java", "jad");
  56.                                         }
  57.                                         // 文件的输出流
  58.                                         FileOutputStream output = new FileOutputStream(newFilePath);
  59.                                         byte[] b = new byte[1024 * 5];
  60.                                         int len;
  61.                                         while ( (len = input.read(b)) != -1) {
  62.                                                 output.write(b, 0, len);
  63.                                         }
  64.                                         output.flush();

  65.                                         // 关闭输出文件流
  66.                                         if (output !=null) {
  67.                                                 output.close();
  68.                                         }

  69.                                         // 关闭输入文件流
  70.                                         if (input != null) {
  71.                                                 input.close();
  72.                                         }
  73.                                 }
  74.                         }
  75.                 } catch (Exception e) {
  76.                         System.out.println("复制整个文件夹内容操作出错");
  77.                         e.printStackTrace();
  78.                 }
  79.         }
  80. }
复制代码

No8:
  1. import java.util.Random;

  2. /**
  3. * 程序改错:输出word = Pain 或 Gain 或 Main
  4. * @author wwwxinyu1990
  5. *
  6. */
  7. public class TestFishc {
  8.     private static Random rdm = new Random();
  9.     public static void main(String[] args) {
  10.         StringBuffer word = null;
  11.         
  12.         switch (rdm.nextInt(3)) {
  13.         case 1:
  14.                 word = new StringBuffer("P");
  15.                 break;
  16.         case 2:
  17.                 word = new StringBuffer("G");
  18.                 break;
  19.         default:
  20.                 word = new StringBuffer("M");
  21.                 break;
  22.         }
  23.         word.append('a');
  24.         word.append('i');
  25.         word.append('n');
  26.         
  27.         System.out.println("word = " + word);
  28.     }
  29. }
复制代码

No9:
  1. /**
  2. * 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保
  3.         证汉字不被截取半个,如"我ABC",4,应该截取"我AB",输入"我AB汉CDEF",6,应该输出"我AB",
  4.         而不是"我ABC+汉的半个"(请说明源文件采用的编码标准,便于判题)。
  5.         编码 : GBK
  6. * @author wwwxinyu1990
  7. */
  8. public class TruncateString {

  9.         public static void main(String[] args) {
  10.                 spltString("我ABC", 4);
  11.                 spltString("我AB汉CDEF", 6);
  12.                 spltString("DEF加油EFG", 6);
  13.         }
  14.        
  15.         public static void spltString(String src, int length) {
  16.                 if (src == null || src.length() < 1) {
  17.                         System.out.println("要截取的字符串是空的,请重新指定。");
  18.                         return;
  19.                 }
  20.                
  21.                 if (length > src.length()) {
  22.                         length = src.length();
  23.                 }
  24.                
  25.                 byte[] srcBytes = src.getBytes();
  26.                 String subStr = "";
  27.                 if (srcBytes[length] < 1) {
  28.                         subStr = new String(srcBytes, 0, --length);  
  29.                         System.out.println("截取的字符串是:" + subStr);
  30.                 } else {
  31.                         subStr = new String(srcBytes, 0, length);  
  32.                         System.out.println("截取的字符串是:" + subStr);
  33.                 }
  34.                
  35.         }
  36. }
复制代码

No10:
  1. public class ThreadTest {
  2.         // 采用 Runnable 接口方式创建的多条线程可以共享实例属性
  3.         private int j;

  4.         // 同步增加方法   
  5.         private synchronized void inc() {
  6.                 j++;
  7.                 System. out .println(Thread.currentThread().getName()+ "--increase--" + j);
  8.         }
  9.        
  10.         // 同步减算方法   
  11.         private synchronized void dec() {
  12.                 j--;
  13.                 System. out .println(Thread.currentThread().getName()+ "--decrease--" + j);
  14.         }
  15.        
  16.         // 增加线程   
  17.         class Inc implements Runnable {
  18.                 public void run() {
  19.                         for (int i = 0; i < 10; i++) {
  20.                                 inc();
  21.                         }
  22.                 }
  23.         }

  24.         // 减算线程   
  25.         class Dec implements Runnable{
  26.                 public void run() {
  27.                         for (int i = 0; i < 10; i++) {
  28.                                 dec();
  29.                         }
  30.                 }
  31.         }
  32.        
  33.         public static void main(String[] args) {
  34.                 ThreadTest t = new ThreadTest();
  35.                
  36.                 // 内部类的实例化
  37.                 Inc inc = t. new Inc();
  38.                 Dec dec = t. new Dec();
  39.                
  40.                 // 创建 2*n 个线程 此处 n=2
  41.                 for ( int i = 0; i < 2; i++) {
  42.                         new Thread(inc).start();
  43.                         new Thread(dec).start();
  44.                 }
  45.                
  46.         }
  47. }
复制代码

谢谢!

点评

鱼同学,java活动全部更新了  发表于 2015-2-10 09:28
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-28 19:48:18 | 显示全部楼层
看到上面的题就想到了  当初的C语言考试。。哈哈

点评

少年,参加活动吧,送实物奖品!  发表于 2015-1-28 20:26
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-28 20:05:49 | 显示全部楼层
我也来拿鱼币啦

点评

参加活动啊,送实物奖品!  发表于 2015-1-28 20:25
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-29 09:51:30 | 显示全部楼层
围观

点评

感谢支持!  发表于 2015-1-29 10:22
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-1-29 11:53:27 From FishC Mobile | 显示全部楼层
半个月内估计不会上论坛,电脑准备大修

点评

鱼同学,java活动全部更新了  发表于 2015-2-10 09:29
道友,论坛需要你!  发表于 2015-1-29 12:32
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-30 14:24:39 | 显示全部楼层
1.zip (1.42 KB, 下载次数: 5)

点评

鱼同学,java活动全部更新了  发表于 2015-2-10 09:29
兄弟,麻烦去报个名(http://bbs.fishc.com/thread-57824-1-1.html ),顺便也做了吧  发表于 2015-1-30 17:38
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-30 17:47:29 | 显示全部楼层
本帖最后由 曾经的肆无忌惮 于 2015-1-30 21:15 编辑
  1. /*
  2. No11:程序设计,第1个人10岁,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
  3. */

  4. class YearTest{
  5.         public static void main(String[] args) {
  6.                 System.out.println("第八个人岁数为:"+year(8));
  7.         }

  8.         public static int year(int n){
  9.                 if (n == 1) {
  10.                         return 10;
  11.                 }
  12.                 else {
  13.                         return 2 + year(--n);
  14.                 }
  15.         }
  16. }
复制代码

  1. /*
  2. No12:程序改错
  3. */

  4. public class Test {

  5.          public static void main(String[] args) {
  6.                 final String PER_DAY_US = 24*60*60*1000*1000;//微秒
  7.                 final String PER_DAY_MS = 24*60*60*1000; //毫秒
  8.                 System.out.println(PER_DAY_US);
  9.                 System.out.println(PER_DAY_MS);
  10.                 System.out.println(PER_DAY_US/PER_DAY_MS);
  11.          }
  12. }
复制代码

  1. /*
  2. No13: 程序改错,输出j = 100
  3. */

  4. public class Test {

  5.          public static void main(String[] args) {
  6.                  int j = 0;
  7.                  for(int i=0 ;i < 100;i++){
  8.                          j = ++j ;
  9.                  }
  10.                  System.out.println("j = "+j);
  11.          }
  12. }
复制代码

  1. /*
  2. No14:程序改错,输出 count = 101
  3. */

  4. public class Test {
  5.          private final static int  end  = Integer.MAX_VALUE;
  6.          private final static int  start  = Integer.MAX_VALUE - 100;
  7.          
  8.          public static void main(String[] args) {
  9.                          int count = 0;
  10.                          for(int i=start - 1; i < end ; i++){
  11.                                  count ++;
  12.                          }
  13.                          System.out.println(" count = "+ count);
  14.          }

  15. }
复制代码

  1. /*
  2. No15: 程序设计,输出昨天的当前日期
  3. */

  4. import java.util.*;

  5. class Date{
  6.         public static void main(String[] args) {
  7.                 int year,month,day;
  8.                 Calendar cal=Calendar.getInstance();
  9.                 year=cal.get(Calendar.YEAR);
  10.                 month=cal.get(Calendar.MONTH);
  11.                 day=cal.get(Calendar.DATE);
  12.                 System.out.println("昨天是"+year+"年"+(++month)+"月"+(--day)+"日");
  13.         }
  14. }
复制代码

  1. <p>/*
  2. No18: 程序改错,输出word = Pain 或 Gain 或 Main
  3. */</p><p>import java.util.Random;
  4. public class TestFishc {
  5.          private static Random rdm = new Random();
  6.          public static void main(String[] args) {
  7.                  StringBuffer word = null;
  8.                  
  9.                  switch (rdm.nextInt(3)) {
  10.          
  11.                  case 1:
  12.                           
  13.                          word = new StringBuffer("P");break;
  14.                         
  15.                  case 2:
  16.                           
  17.                          word = new StringBuffer("G");break;
  18.                           
  19.                  default:
  20.                           
  21.                          word = new StringBuffer("M");
  22.                           
  23.                  }
  24.                  word.append('a');
  25.                  word.append('i');
  26.                  word.append('n');
  27.                  
  28.                  System.out.println("word = "+word);
  29.          }
  30. }
  31. </p>
复制代码



学的还是太少了,只做出了这么几题,还有四题的知识点还没自学到、、、

点评

鱼同学,java活动全部更新了  发表于 2015-2-10 09:29
第14题你想复杂了  发表于 2015-1-30 19:37
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

嗯、我想明白了、是因为最后一次循环的时候i++导致的溢出,所以才会陷入死循环,我改好了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 09:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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