java学习之路----java开发实战经典第12章习题及答案

本文提供了多个 Java 输入输出操作的示例代码,包括控制台读取数据、文件操作、数据排序展示等,帮助读者理解 Java IO 的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >






答案:


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);
          }
              
              
              
          }
          
          
          
     }
     

     

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值