栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java练习题(API)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java练习题(API)

1.将一个字符串逆序输出 例如: abcd --> dcba

package com.ffyc.javaoop.day08.HM;

public class Demo1 {
    public static void main(String[] args) {
        String s = "abcd";
        char[] c = s.toCharArray();
        String s1 = "";
        for (int i = c.length-1; i >=0 ; i--) {
            s1+=c[i];
        }
        System.out.println(s1);
    }
}

2.统计一个字符串中出现的大写字母,小写字母各自的个数

package com.ffyc.javaoop.day08.HM;

public class Demo2 {
    public static void main(String[] args) {
        String s = "ABUghgGUnks";
        byte[] b = s.getBytes();
        int dx = 0;
        int xx = 0;
        for (int i = 0; i < b.length; i++) {
            if(b[i]>=65&&b[i]<=90){
                dx++;
            }else if(b[i]>=97&&b[i]<=122){
                xx++;
            }
        }
        System.out.println("大写字母个数:"+dx+"n小写字母个数"+xx);
    }
}

3.判断一个字符串是否对称,例如"abccba"

package com.ffyc.javaoop.day08.HM;


public class Demo3 {
    public static void main(String[] args) {
        String s = "abccba";
        char[] c = s.toCharArray();
        String s1 = "";
        String s2 = "";
        for (int i = 0; i < c.length/2; i++) {
            s1 += c[i];
        }
        for (int i = c.length-1; i >=c.length/2; i--) {
            s2 += c[i];
        }
        
        if(s1.equals(s2)){
            System.out.println("对称");
        }else {
            System.out.println("不对称");
        }
    }
}

4.定义一个用于截取文件后缀名的方法, 传入一个字符串的文件名( hello.java),返回一个字符串.

package com.ffyc.javaoop.day08.HM;

public class Demo4 {
    public static void main(String[] args) {
        String s = "hello.java";
        System.out.println(s.substring((s.indexOf("."))));
    }
}

5.给定一段字符串,将里面的信息进行提取,(注意:需要考虑信息是可变的)
String s = “张三:上机成绩=90,笔试成绩=78;”
+ “李四:上机成绩=68,笔试成绩=98;”
+ “王五:上机成绩=48,笔试成绩=58;”
+ “赵六:上机成绩=55,笔试成绩=11;”
+ “jim:上机成绩=44,笔试成绩=55;”
+ “tom:上机成绩=22,笔试成绩=55;”
要求: 按照总分进行降序显示输出 (使用面向对象思想实现)
学生类

package com.ffyc.javaoop.day08.HM;

public class StudentDemo5 implements Comparable{
    private String name;
    private int num1;
    private int num2;
    private int sum;

    @Override
    public int compareTo(StudentDemo5 o) {
        return o.sum-this.sum;
    }

    @Override
    public String toString() {
        return "StudentDemo5{" +
                "name='" + name + ''' +
                ", num1=" + num1 +
                ", num2=" + num2 +
                ", sum=" + sum +
                '}';
    }

    public StudentDemo5(String name, int num1, int num2, int sum) {
        this.name = name;
        this.num1 = num1;
        this.num2 = num2;
        this.sum = sum;
    }

    public StudentDemo5() {
    }

    public String getName() {
        return name;
    }

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

    public int getNum1() {
        return num1;
    }

    public void setNum1(int num1) {
        this.num1 = num1;
    }

    public int getNum2() {
        return num2;
    }

    public void setNum2(int num2) {
        this.num2 = num2;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }
}

实现类

package com.ffyc.javaoop.day08.HM;

import java.util.Arrays;


public class Demo5 {
    public static void main(String[] args) {
        String s =  "张三:上机成绩=90,笔试成绩=78;"
                + "李四:上机成绩=68,笔试成绩=98;"
                + "王五:上机成绩=48,笔试成绩=58;"
                + "赵六:上机成绩=55,笔试成绩=11;"
                + "jim:上机成绩=44,笔试成绩=55;"
                + "tom:上机成绩=22,笔试成绩=55;";
        //System.out.println(s);
       String[] s1 = s.split(";");
       StudentDemo5[] stu = new StudentDemo5[s1.length];
        for (int i = 0; i < s1.length; i++) {
            String s2 = s1[i];
            String name = s2.substring(0,s2.indexOf(":"));
           int num1 = Integer.parseInt(s2.substring((s2.indexOf("=")+1), s2.indexOf(",")));
           int num2 = Integer.parseInt(s2.substring((s2.lastIndexOf("=")+1)));
           int sum = num1+num2;
           StudentDemo5 student = new StudentDemo5(name,num1,num2,sum);
           stu[i] = student;
        }
        Arrays.sort(stu);
        System.out.println(Arrays.toString(stu));
    }

}

6.使用面向对象编程思想设计实现用户注册,登录功能.
启动程序选择操作功能:1.注册,2.登录,3退出
输入1进入注册流程,提示用户输入手机号,密码.
输入符合规则后将用户信息存储(存储到用户对象,将用户对象存储到数组,假设数组长度为100),回到功能选择.

输入2进入用户登录流程,提示用户输入手机号,密码
输入符合规则后 与之前保存的用户信息比较,手机号和密码是否存在.
判断登录能否成功.
输入3 退出操作

package com.ffyc.javaoop.day08.HM.Demo6;

public class User implements Comparable{
    private String phone;
    private String password;
    @Override
    public int compareTo(User o) {
        return 0;
    }

    public User(String phone, String password) {
        this.phone = phone;
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

package com.ffyc.javaoop.day08.HM.Demo6;

public class UserOpera {


    User[] users = new User[100];
    int index = 0;

    public boolean register(){
        User user = new User("1522222222","1111");
        users[index] = user;
        index++;
        return true;
    }

    public boolean login(){
        String phone = "1522222222";
        String password = "1111";
        int num=0;
        for (int i = 0; i < users.length; i++) {
            User user = users[i];
            if(phone.equals(user.getPhone())&&password.equals(user.getPassword())){
                num++;
            }
        }
        //循环结束
        if(num!=0){
            return true;
        }

        return false;
    }
}

package com.ffyc.javaoop.day08.HM.Demo6;

import java.util.Scanner;


public class Test {




        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            UserOpera userOper = new UserOpera();//只new1次
            // User[] users = new User[100];
            while(true){
                System.out.println("请输入操作项1-注册,2-登录,3-退出");
                int item = scanner.nextInt();
                switch (item){
                    case 1: userOper.register();
                    case 2: userOper.login();
                    case 3: break;
                }
            }
        }
    }






转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/716602.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号