JavaIO编程练习题

练习1

1.编写java程序,输入3个整数,比较后输出最大值和最小值

package ExampleScanner;

import java.util.Arrays;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
//        键盘输入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入三个整数");
        int datas[] = new int[3];
        scanner.useDelimiter("\n");
//        判断并赋值
        for (int i = 0; i<3;i++){
            if (scanner.hasNextInt()){
                datas[i] = Integer.parseInt(scanner.next());
            }else {
                System.out.println("您输入的并不是整数,自动退出程序");
                break;
            }
        }
        System.out.println(Arrays.toString(datas));
    }
}


练习2

2.编写Java程序,实现键盘输入文件名称创建文件名,并且能够通过键盘输入文件内容

package Test1602;

import java.io.*;
import java.util.Scanner;

public class javaDemo {
    public static void main(String[] args) throws Exception {
        File file = new File("E:" + File.separator + "jiawa" + File.separator);
//        输入文件名称
        System.out.println("请输入文件的名称");
        Scanner scanner = new Scanner(System.in);
        File newFile = new File(file, scanner.next());
        if (!newFile.exists()) {
            newFile.createNewFile();
//            输入文件内容
            Writer write = new FileWriter(newFile);
            System.out.println("输入你想要输入的文件内容");
            Scanner scanData = new Scanner(System.in);
            write.write(scanData.next());
//        注意使用write一定要进行close或者flush,才能将数据导入到文件中
            write.close();
//        读取文件内容
            Reader read = new FileReader(newFile);
            char data[] = new char[1024];
            int len = read.read(data);
            System.out.println("文件名称是" + newFile.getName() + "文件内容是" + new String(data, 0, len));
            read.close();
        } else {
            System.out.println("该文件已经存在,换一个名字吧");
        }
    }
}


练习3

通过键盘输入传入多个字符串,并将字符串进行反转

package Test1603;

import java.util.Scanner;

public class javaDemo {
    public static void main(String[] args) {
        StringBuffer str = new StringBuffer();
        for (int i=0;i<3;i++){
            Scanner scanner =new Scanner(System.in);
            str.append(scanner.next());
            System.out.println("字符串现在为"+str);
        }
        System.out.println("字符串反转后是"+str.reverse());
    }
}


练习4

输入一个数字字符串,统计其中奇书数字有多少个,偶数数字有多少个

package Test1604;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class javaDemo {
    public static void main(String[] args) {
        int countO = 0;
        int countJ = 0;
        System.out.println("请输入一个数字字符串");
        Scanner scanner = new Scanner(System.in);
//        实现正则匹配
        String pattern = "\\d";
        Pattern pat = Pattern.compile(pattern);
        Matcher mat = pat.matcher(scanner.next());
//      提取数字并且进行判断
        
        while (mat.find()){
           int content = Integer.parseInt(mat.group());
            if (content%2==0){
                countO++;
            }else countJ++;
        }
        System.out.println("偶数个数为"+countO+" 奇数个数为"+countJ);
    }
}

 


 练习5

实现一个登陆系统,用户在命令行输入账号和密码,如果没有输入账号和密码则提示用户输入账号和密码,如果只输入了账号,则提示输入密码。只有三次输入机会如果三次机会用完则自动退出登陆系统

package Test1605;

import java.util.Scanner;

public class javaDemo {
    public static void main(String[] args) {
        System.out.println("输入账号和密码");
        int Chance = 3;
        Scanner scanner = new Scanner(System.in);
        while (Chance>0){
//      输入账号密码
            System.out.println("请输入账号");
            String account = scanner.nextLine();
            System.out.println("请输入密码");
            String password = scanner.nextLine();

//      账号密码验证
            if (account.equals("001")&&password.equals("123456")){
                System.out.println("登陆成功");
                break;
            }else {
                Chance--;
                System.out.println("账号或密码错误"+"还剩"+Chance+"次机会");
            }
//      账号密码非空提示
            if (account.isEmpty()||password.isEmpty()){
                Chance--;
                System.out.println("账号或者密码不能为空"+"还剩"+Chance+"次机会");
            }
            if (Chance == 0){
                System.out.println("您的机会用完了");
            }
        }
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alphamilk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值