写程序100道31-40

31.使用do…while循环语句计算正数5的阶乘。

package Exercises.One_Hundred;
​
public class Demo31 {
    public static void main(String[] args) {
        int num=5;
        int factorial=1;
        int i=1;
        do {
            factorial*=i;
            i++;
        }while (i<num+1);
        System.out.println(factorial);
    }
}
​

32.

某公司正进行招聘工作,被招聘人员需要填写个人信息,编写“个人简历”的封装类Resume,并编写测试类进行实现。类图及输出效果如下。

类名:Resume
name : String (private) sex : String (private) age : int (private)
Resume( ) // 没有参数的空构造方法 Resume(Srting name, String sex, int age) // 得到各个属性值的方法getXxx( ) introduce( ) : void // 自我介绍(利用属性)

程序运行结果如下:

我是:李四,性别:男,年龄:20

package Exercises.One_Hundred;
​
public class Resume {
    private String name;
    private String sex;
    private int age;
​
    public Resume() {
​
    }
    public Resume(String name,String sex,int age){
        this.name=name;
        this.age=age;
        this.sex=sex;
    }
​
    public String getName() {
        return name;
    }
​
    public String getSex(){
        return sex;
    }
​
    public int getAge() {
        return age;
    }
​
    public void introduce(){
        System.out.println("我是: "+getName()+","+"性别: "+getSex()+","+"年龄: "+getAge());
    }
}
​

package Exercises.One_Hundred;
​
public class test32 {
    public static void main(String[] args) {
        Resume resume=new Resume("李四","男",20);
        resume.introduce();
    }
}
​

\33. 某公司正进行招聘工作,被招聘人员需要填写个人信息,编写“个人简历”的封装类Resume,并编写测试类进行实现。类图及输出效果如下。

类名:Resume
name : String (private) age : int (private)
Resume( ) // 没有参数的空构造方法 Resume(Srting name, int age) // 得到各个属性值的方法getXxx( ) introduce( ) : void // 自我介绍(利用属性)

程序运行结果如下:

我是:李四,性别:男,年龄:20

\34. 已知函数

y= x + 3 ( x > 0 )

y = 0 ( x = 0 )

y= x2 –1 ( x < 0 )

请设计一个方法实现上面的函数,根据传入的值x的不同,返回对应的y值。

package Exercises.One_Hundred;
​
import java.util.Scanner;
​
public class Demo34 {
    public static void main(String[] args) {
        int y,x;
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入x");
        x=scanner.nextInt();
        if(x>0){
            y=x+3;
        }else if(x==0){
            y=0;
        }else {
            y=x*x-1;
        }
        System.out.println("y="+y);
    }
}
​

\35. 已知函数

y= x- 3 ( x > 0 )

y = 5 ( x = 0 )

y= x ( x < 0 )

请设计一个方法实现上面的函数,根据传入的值x的不同,返回对应的y值。

package Exercises.One_Hundred;
​
import java.util.Scanner;
​
public class Demo35 {
    public static void main(String[] args) {
        int y,x;
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入x");
        x=scanner.nextInt();
        if(x>0){
            y=x-3;
        }else if(x==0){
            y=5;
        }else {
            y=x;
        }
        System.out.println("y="+y);
    }
}
​

36.请按照以下要求设计一个学生类Student,并进行测试。

要求如下:

1)Student类中包含姓名、成绩两个属性

2)分别给这两个属性定义两个方法,一个方法用于设置值,另一个方法用于获取值.

3)Student类中定义一个无参的构造方法和一个接收两个参数的构造方法,两个参数分别为姓名和成绩属性赋值

4)在测试类中创建两个Student对象,一个使用无参的构造方法,然后调用方法给姓名和成绩赋值,一个使用有参的构造方法,在构造方法中给姓名和成绩赋值

package Exercises.One_Hundred;
​
public class Student {
    private String name;
    private int score;
    
    public Student(){
        
    }
    
    public Student(String name,int score){
        this.name=name;
        this.score=score;
    }
​
    public String getName() {
        return name;
    }
​
    public int getScore() {
        return score;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public void setScore(int score) {
        this.score = score;
    }
}
  • 20
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值