知ing

Java2实用教程(第4版)

耿祥义、张跃平 / 清华大学出版社

頑張って! 上传

查看本书

习题七(第7章)

一、问答题

1.有效。

2.可以。

3.不可以。

4.一定是。

二、选择题

1C2C

三、阅读程序

1大家好,祝工作顺利!

2p是接口变量。

3你好 fine thanks

4属于上机实习程序,解答略。

四、编程题

import java.util.*;

public class E {

    public static void main (String args[ ]){

      Scanner reader = new Scanner(System.in);

      double sum = 0;

       int m = 0;

       while(reader.hasNextDouble()){

           double x = reader.nextDouble();

           assert x< 100:"数据不合理";

           m = m+1;

           sum = sum+x;

       }

       System.out.printf("%d个数的和为%f\n",m,sum);

       System.out.printf("%d个数的平均值是%f\n",m,sum/m);

    }

}

习题八(第8章)

一、问答题

1.不是。"\\hello"是。

243

3falsetrue

4.负数。

5.是true

63-1

7.会发生NumberFormatException异常。

二、选择题

1A2C3B4D5C

三、阅读程序

1.【代码】:苹果。

2.【代码】:Love:Game

3.【代码1】:15代码2】:abc我们。

4.【代码】:13579

5.【代码】:9javaHello

6属于上机实习程序,解答略。

7属于上机实习程序,解答略。

 

四、编程题

1public class E {

  public static void main (String args[ ]) {

     String s1,s2,t1="ABCDabcd";

     s1=t1.toUpperCase();

     s2=t1.toLowerCase();

     System.out.println(s1);

     System.out.println(s2);

     String s3=s1.concat(s2);

      System.out.println(s3);

   }

}

2.   public class E {

  public static void main (String args[ ]) {

     String s="ABCDabcd";

     char cStart=s.charAt(0);

     char cEnd = s.charAt(s.length()-1);

     System.out.println(cStart);

     System.out.println(cEnd);

   }

}

3.   import java.util.*;

public class E {

  public static void main (String args[ ]) {

    int year1,month1,day1,year2,month2,day2;

      try{ year1=Integer.parseInt(args[0]);

           month1=Integer.parseInt(args[1]);

           day1=Integer.parseInt(args[2]);

           year2=Integer.parseInt(args[3]);

           month2=Integer.parseInt(args[4]);

           day2=Integer.parseInt(args[5]);

       }

       catch(NumberFormatException e)

         { year1=2012;

           month1=0;

           day1=1;

           year2=2018;

           month2=0;

           day2=1;

       }

       Calendar calendar=Calendar.getInstance();

       calendar.set(year1,month1-1,day1);  

       long timeYear1=calendar.getTimeInMillis();

       calendar.set(year2,month2-1,day2);  

       long timeYear2=calendar.getTimeInMillis();

       long 相隔天数=Math.abs((timeYear1-timeYear2)/(1000*60*60*24));

       System.out.println(""+year1+""+month1+""+day1+"日和"+

                            year2+""+month2+""+day2+"日相隔"+相隔天数+"");

   }

}

4.   import java.util.*;

public class E {

  public static void main (String args[ ]) {

   double a=0,b=0,c=0;

      a=12;

      b=24;

      c=Math.asin(0.56);

      System.out.println(c);

      c=Math.cos(3.14);

      System.out.println(c);

      c=Math.exp(1);

      System.out.println(c);

      c=Math.log(8);

      System.out.println(c);

   }

}

5public class E {

      public static void main (String args[ ]) {

        String str = "ab123you你是谁?";

        String regex = "\\D+";

        str = str.replaceAll(regex,"");

        System.out.println(str);

      }

}

6 import java.util.*;

public class E {

   public static void main(String args[]) {

      String cost = "数学87分,物理76分,英语96";

      Scanner scanner = new Scanner(cost);

      scanner.useDelimiter("[^0123456789.]+");

      double sum=0;

      int count =0;

      while(scanner.hasNext()){

         try{  double score = scanner.nextDouble();

               count++;

               sum = sum+score;

               System.out.println(score);

         }

         catch(InputMismatchException exp){

              String t = scanner.next();

         }   

      }

      System.out.println("总分:"+sum+"");

      System.out.println("平均分:"+sum/count+"");

   }

}


查看更多