java學習之路----java開發實戰經典第12章習題及答案






答案:


1.
public class IOtest {
     public static void main(String[] args) {
          Scanner scanner= new Scanner(System.in);
           int a[]=new int[3];
           int i=0;
           //boolean flage =true;
               while(i!=3){
                   
                   
                   a[i]=scanner.nextInt();
                   i++;
              }
              scanner.close();
              Arrays. sort(a);
              System. out.println("最小" +a[0]+"------" +"最大" +a[2]);
              
     }

}

2.
public class IOtest {
     
     public  static void init(String name,String str)throws Exception {
          File file= new File("f:" +File.separator+name);
           if(!file.exists()){
              file.createNewFile();
              
              DataOutputStream out= new DataOutputStream(new FileOutputStream(file));
              
              out.writeBytes(str);
              
              out.close();
              
          }
          
     }
     
     public static void main(String[] args) throws Exception{
           init("b.txt", "hello world");
          
          
     }

}

3.
public class IOtest {
     
     
     
     public static void main(String[] args) throws Exception{
          
          Scanner scanner= new Scanner(System.in);
          
          
           int i=0;
          java.util.List<String> list= new ArrayList<String>();
           while(i!=5){
              list.add(scanner.next());
              i++;
          }
          scanner.close();
           for(int j=list.size();j>0;j--){
              System. out.print(list.get(j-1)+"\t" );
          }
     }

}
4.
public class IOtest {
     
     
     
      public static void main(String[] args) throws Exception{
          
          Scanner scanner= new Scanner(System. in);
          
          PrintStream print= new PrintStream( new File("f:" +File.separator +"a.txt" ));
          
          String str=scanner.next();
               scanner.close();
          String name= null ;
          String age= null ;
          String s[]=   str.split( "[\\|]" );
          Map<String, String>map= new HashMap<String, String>();
           for (String s1:s){
          String s2[]   =s1.split( "[\\:]" );
           for (String s3:s2){
               if (s3.matches("\\d+" )){
                   age=s3;
              } else {
                   name=s3;
              }
              
              map.put(name, age);
          }
          }
          List<Map.Entry<String, String>>ma= new ArrayList<Map.Entry<String, String>>(map.entrySet());
          
          Collections. sort(ma, new Comparator<Map.Entry<String, String>>() {

               @Override
               public int compare(Entry<String, String> o1,
                        Entry<String, String> o2) {
                   

                    if (o1.getValue()!= null&&o2.getValue()!= null&&(Integer. parseInt(o1.getValue())>=Integer. parseInt(o2.getValue()))){
                         return -1;
              } else {
               return 1; 
              }
              }
          });
          System. out .println(ma);
//        for(Map.Entry<String, String>m:map.entrySet()){
//            
//        }
          
     }

}
5.
public class IOtest {
     
     
     
      public static void main(String[] args) throws Exception{
          
          Scanner scanner= new Scanner(System. in);
          
           //PrintStream print=new PrintStream(new File("f:"+File.separator+"a.txt"));
          
           boolean Y=true ;
          String str= null ;
           while (Y){
          str+=scanner.next();
          System. out .println("是否繼續輸入" );
          String s=scanner.next();
           if (s.equalsIgnoreCase("Y" )){
              Y= true ;
          System. out .println("請繼續輸入" );
          }
           if (s.equalsIgnoreCase("N" )){
              Y= false ;
              System. out .println("退出輸入" );
          }
          }
          String name= null ;
          String age= null ;
          
          DataOutputStream out= new DataOutputStream( new FileOutputStream(new File("f:" +File. separator+ "a.txt")));
          
          out.writeBytes(str);
          out.close();
          scanner.close();
          String s[]=   str.split( "[\\|]" );
          Map<String, String>map= new HashMap<String, String>();
           for (String s1:s){
          String s2[]   =s1.split( "[\\:]" );
           for (String s3:s2){
               if (s3.matches("\\d+" )){
                   age=s3;
              } else {
                   name=s3;
              }
              
              map.put(name, age);
          }
          }
          List<Map.Entry<String, String>>ma= new ArrayList<Map.Entry<String, String>>(map.entrySet());
          
          Collections. sort(ma, new Comparator<Map.Entry<String, String>>() {

               @Override
               public int compare(Entry<String, String> o1,
                        Entry<String, String> o2) {
                   

                    if (o1.getValue()!= null&&o2.getValue()!= null&&(Integer. parseInt(o1.getValue())>=Integer. parseInt(o2.getValue()))){
                         return -1;
              } else {
               return 1; 
              }
              }
          });
          System. out .println(ma);
//        for(Map.Entry<String, String>m:map.entrySet()){
//            
//        }
          
     }

}
6.
public class IOtest {
     
      public static void main(String[] args) throws Exception{
          
          System. out .println("請輸入一段數字字符串" );
          Scanner scanner= new Scanner(System. in);
          String str=scanner.next();
          scanner.close();
           int a=0;//偶數
           int b=0;//奇數
          
           int sum=0;
           for (int i=0;i<str.length();i++){
              sum=str.charAt(i)-48;
          
               if (sum%2==0){
                   a++;
              } else {
                   b++;
              }
          }
          
          System. out .println("總數是:" +str.length()+ "   偶數:"+a+ "   奇數:"+b);
          
          
          
          
     }

}


7.
public class IOtest {
     
      public static void main(String[] args) throws Exception{
          
          System. out .println("請輸入用戶名" );
          Scanner scanner= new Scanner(System. in);
          
          String userName=scanner.next();
          System. out .println("請輸入密碼" );
          String password=scanner.next();
          
          
          
           while (userName== null||password== null){
               if (userName== null){
                   System. out .println("請輸入用戶名" );
                   userName=scanner.next();
              }
               if (password== null){
                   System. out .println("請輸入密碼" );
                    password=scanner.next();
              }
          }
           int count=0;
           while (userName!= null&&password!= null){
                if (userName.equals("mldn" )&&password.equals( "hello")){
                   System. out .println("登陸成功" );
                   scanner.close();
                    return ;
              } else {
                   count++;
                    if (count==3){
                        System. out .println("連續登陸失敗三次,系統推出" );
                        scanner.close();
                         return ;
                   } else {
                        System. out .println("連續登陸三次失敗,則系統退出,您還有" +(3-count)+ "次");
                        System. out .println("用戶名或密碼錯誤,請重新輸入:" );
                        System. out .println("請輸入用戶名" );
                        userName=scanner.next();
                        System. out .println("請輸入密碼" );
                         password=scanner.next();
                   }
              }
          }
          
     }

}

8.
public class IOtest {
     
      public static void main(String[] args) throws Exception{
          
          System. out .println("請輸入源文件" );
          Scanner scanner= new Scanner(System. in);
          String ypath=scanner.next();
          System. out .println("請輸入目標文件" );
          String mbpath=scanner.next();
          scanner.close();
          OutputStream out= new FileOutputStream( new File(mbpath));
          InputStream in= new FileInputStream( new File(ypath));
          
           int temp=0;
          
          
           while ((temp=in.read())!=-1){
          out.write(temp);
          }
          System. out .println("複製完成" );
          out.close();
          in.close();
          
     }

}

10.
class Student{
      private int id ;
     
      private String name;
     
      private int count ;

      public int getId() {
           return id ;
     }

      public void setId(int id) {
           this .id = id;
     }

      public String getName() {
           return name ;
     }

      public void setName(String name) {
           this .name = name;
     }

      public int getCount() {
           return count ;
     }

      public void setCount( int count) {
           this .count = count;
     }

      public Student( int id, String name, int count) {
           this .id = id;
           this .name = name;
           this .count = count;
     }
     
     
     
}


public class IOtest {
     
      public static void main(String[] args) throws Exception{
          Scanner scanner= new Scanner(System. in);
          Student student1= new Student(1,"張三" ,0);
          Student student2= new Student(2,"李四" ,0);
          Student student3= new Student(3,"王五" ,0);
          Student student4= new Student(4,"趙六" ,0);
          Student students[]={student1,student2,student3,student4};
           for (Student student:students){
              System. out .println(student.getId()+": "+student.getName()+ " 【"+student.getCount()+ "】" );
          }
           int a=1;
           int num1=0,num2=0,num3=0,num4=0;
           while (a!=0){
              
          String str=   scanner.next();
           if (str.matches("\\d+" )){
              a=Integer. parseInt(str);
              System. out .println("請輸入班長候選人代號(數字0結束):" +a);
               if (a==0){
                   Map<String,Integer> map= new HashMap<String,Integer>();
                    for (Student student:students){
                        System. out .println(student.getId()+": "+student.getName()+ " 【"+student.getCount()+ "】" );
                        map.put(student.getName(), student.getCount());
                   }
                  List<Map.Entry<String, Integer>>ma= new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
                   
                    Collections. sort(ma, new Comparator<Map.Entry<String, Integer>>() {

                         @Override
                         public int compare(Entry<String, Integer> o1,
                                  Entry<String, Integer> o2) {
                            

                              if (o1.getValue()!= null &&o2.getValue()!= null &&o1.getValue()>=o2.getValue()){
                                   return 1;
                        } else {
                         return -1;
                        }
                        }
                    });
                    System.out .println(ma.get(3).getKey()+"同學,最後以" +ma.get(3).getValue()+ "當選班長");
                    return ;
              }
              
              
               if (a==1){
                   
                   num1++;
                   students[a-1].setCount(num1);
              }
          
               else if (a==2){
              
                   num2++;
                   students[a-1].setCount(num2);
              }
          
               else if (a==3){
              
                   num3++;
                   students[a-1].setCount(num3);
              }
               else if (a==4){
              
                   num4++;
                   students[a-1].setCount(num4);
              } else {
                   System. out .println("此次選票無效,請輸入正確的候選人代號" );
              }
              
          } else {
              System. out .println("此次選票無效,請輸入正確的候選人代號" );
              System. out .println("請輸入班長候選人代號(數字0結束):" +str);
          }
              
              
              
          }
          
          
          
     }
     

     

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章