A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

乔布斯

初级黑马

  • 黑马币:

  • 帖子:

  • 精华:

© 乔布斯 初级黑马   /  2019-5-8 15:12  /  1033 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

说句实话,真的题目比较基础,考的知识点也挺多的,平常听下客还是能够很快的搞出来,四十分钟搞定交卷。javase的内容在开班之前已经差不多自学完了
自认为掌握的不是很好,还是需要加强联系。


附上代码题代码
第一题
/*
1、编写一个Java程序,提示用户输入一串字符串,要求字符串中必须存在字母(需要代码判断)
1.若不符合要求,则提示用户重新输入直至符合要求为止
2.若符合要求,则判断字符串中大写字母出现次数并打印。
*/

import java.util.Scanner;

public class test01 {
    public static void main(String[] args) {
        boolean flag =false;
        int bigcount= 0;
        int smallCount=0;
        //提示用户输入一串字符串
        while (!flag) {
            System.out.println("请输入一串字符串");
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            //判断字符串是否存在字母
            for (int i = 0; i < s.length(); i++) {
                //字符中存在字母
                if (s.charAt(i) >= 'A' && s.charAt(i)<=  'Z') {
                    bigcount++;
                }else if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z'){
                    smallCount++;
                }
            }
            //判断是否出现了字母
            if (bigcount>0||smallCount>0){
                flag=true;
            }else {
                System.out.println("您输入有误请重新输入带字母的字符串");
            }
            //打印出大写字母的次数
            System.out.println("输入的字符串含大写字母个数:" + bigcount);
        }
    }
}第二题
学生类
/*
1. 定义"学生"类,Student,包含以下成员:
成员属性: name (姓名):String类型,chinese(语文):int类型,math(数学):int类型,属性使用private修饰。
        (1)生成所有属性的get/set方法,生成构造方法
        (2)成员方法:void show()方法,打印对象所有属性的值以及对象的语文和数学成绩的总和到控制台;
*/
public class Student {
    private String name ;
    private int  chinese;
    private   int  math;

    public Student() {
    }

    public Student(String name, int chinese, int math) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
    }

    public String getName() {
        return name;
    }

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

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public  void  show (){
        System.out.println(this.name+"的"+"语文成绩:"+this.chinese+"数学成绩:"+this.math+"总成绩:"+(this.chinese+this.math));
    }
}
测试类

import java.util.ArrayList;

/*
定义类:Test,类中定义main()方法,按以下要求编写代码:
    (1)定义如下方法:
            ①定义public static ArrayList<Student>  getSum(ArrayList<Student> list){...}方法:
            ②要求:遍历list集合,将list中语文成绩和数学成绩的总和大于160分的元素存入到另一个ArrayList<Student> 中并返回。
    (2)分别实例化三个Student对象,三个对象分别为:"邓超" ,88,99、"baby" ,85,78、"郑凯" ,86,50;
    (3)创建一个ArrayList集合,这个集合里面存储的是Student类型,分别将上面的三个Student对象添加到集合中,
    调用方法getSum,根据返回的list集合遍历对象并调用show()方法输出所有元素信息。
*/
public class Test {
    public static void main(String[] args) {
        //创建一个ArrayList集合
        ArrayList<Student> arr = new ArrayList<Student>();
        //创建对象
        Student s1 = new Student("邓超" ,88,99);
        Student s2 = new Student("baby" ,85,78);
        Student s3  = new Student("郑凯" ,86,50);
        //对象添加到集合
        arr.add(s1);
        arr.add(s2);
        arr.add(s3);
       //调用getSum方法 遍历集合 调用show
        for (Student s: getSum(arr)){
            s.show();
        }
    }
    //定义一个方法
    public static ArrayList<Student>  getSum(ArrayList<Student> list){
        //定义一个集合用于存放符合要求的数据
        ArrayList<Student> arr1 = new ArrayList<Student>();
        //遍历list集合,将list中语文成绩和数学成绩的总和大于160分的元素存入到
       for (Student s : list){
           int sum =s.getMath()+s.getChinese();
            if (sum>160){
                arr1.add(s);
            }
       }
       return  arr1;
    }
}




0 个回复

您需要登录后才可以回帖 登录 | 加入黑马