17年华南师范机试

本文介绍了Java编程中的基础操作,包括输出平行四边形形状、猴子吃桃问题(递归算法)、计数正负数、学生成绩记录类以及运算符重载。
摘要由CSDN通过智能技术生成

1.输入一个数字,为其高,一个符号*,输出该符号组成的平行四边形形状
 

public static void main(String[] args){
   // 1.输入一个数字,为其高,一个符号*,输出该符号组成的平行四边形形状
    Scanner sc = new Scanner(System.in);
    int high = sc.nextInt();
    int n = 10;
    int c ;
    for(int i = high; i > 0; i--){//高度控制
        for (int j = i;j>0;j--){//打印间隔
            System.out.print(" ");
        }
        if(i != high && i != 1){//非首尾两行打印
            System.out.print("*");
            for (int wei = 0; wei <9; wei++) {
                System.out.print(" ");
                if(wei == 8) System.out.print("*");
            }
        }else {//首尾两行打印
            for (int k = 0;k<n;k++) {
                System.out.print("*");
            }

        }
        System.out.println();
    }
}

 

二、是猴子吃桃问题,每天都吃剩下的一半,再多吃一个,直到第十天剩下一个桃子,问第一天猴子有多少个桃。

public static void main(String[] args) {
    //二、是猴子吃桃问题,每天都吃剩下的一半,再多吃一个,直到第十天剩下一个桃子,问第一天猴子有多少个桃。
   // int n = chiTao(10,1);
    int m = chiTao(1);
    System.out.println(m);
}

private static int chiTao(int time,int i) {
    if(time == 1)   return i;
    System.out.println(time+" "+i);
    return chiTao(time-1,2*(i+1));
}
private static int chiTao(int time) {
    if(time == 10)   return 1;
    return (chiTao(time+1)+1)*2;
}

三、输入一系列的数字,输出他们正负数个数,输入0截止,要用函数实现

 

public static void main(String[] args) {
  //  三、输入一系列的数字,输出他们正负数个数,输入0截止,要用函数实现
    Scanner sc = new Scanner(System.in);
    int[] shuzu = new int[2];
    int c = sc.nextInt();
    shuzu = count(c, sc);
    System.out.println(Arrays.toString(shuzu));
}

private static int[] count(int c, Scanner sc) {
    int[] shuzu = new int[2];
    while(c != 0){
        if(c < 0) {
            shuzu[0]++;
        }else {
            shuzu[1]++;
        }
       c = sc.nextInt();
    }
    return shuzu;
}

 

四.是用一个类来记录学生成绩和他班级,里面有几个函数,静态成员和动态成员都有

    public static void main(String[] args) {
        //四、是用一个类来记录学生成绩和他班级,
        // 里面有几个函数,静态成员和动态成员都有,
        // 因为题目是回忆版的,题目概述不清,根据之前看过的教材大概自己设置几个函数与静态成员
        Student1 student1 = new Student1("zhangsan",99,"5");
        System.out.println(student1.teacher_name);
        System.out.println(student1.toString());
    }

}
class Student1{
    private String name;
    private int score;
    private String stu_class;
    public static final String teacher_name = "梨花";  // 常量
    public static int a = 89; // 静态成员变量
    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", score=" + score +
                ", stu_class='" + stu_class + '\'' +
                '}';
    }

    public static int getA() {
        return a;
    }

    public static void setA(int a) {
        Student1.a = a;
    }

    public Student1(String name, int score, String stu_class) {
        this.name = name;
        this.score = score;
        this.stu_class = stu_class;
    }

    public String getName() {
        return name;
    }

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

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getStu_class() {
        return stu_class;
    }

    public void setStu_class(String stu_class) {
        this.stu_class = stu_class;
    }
}

五.java重载+,++,=等简单运算符,

不会。

  • 11
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值