1.编程
为指定成绩加分,直到分数大于等于 60 为止,输出加分前和加分后的成绩,并统计加分的
次数(提示需要定义两个变量,初始成绩,加分次数)
输出结果:
public class Main {public static void fun(){int score = 55;int count = 0;System.out.println("加分前成绩:"+score);while (score<60){count++;score++;}System.out.println("加分前成绩:"+score);System.out.println("公加了"+count+"次");}public static void main(String[] args) {// write your code herefun();}}
2. 编程
判断一个数(小于 10 位)的位数。
输入 999,则输出 “它是个 3 位的数!
import java.util.Scanner;public class Main {public static void fun(){// int num = 999;System.out.println("请输入不大于十位的数字");Scanner scanner = new Scanner(System.in);int num = scanner.nextInt();int count = 0;while (num!=0){count++;num/=10;}System.out.println("它是个"+count+"位的数");}public static void main(String[] args) {// write your code herefun();}}
3. 编写自定义 Person 类,根据提示以及效果图编写代码。
程序运行参考效果图如下:
提示:
创建 Person 类
属性:名字(name),年龄(age),年级( grade)
方法:
1、无参无返回值的 student 方法,描述为:我是一名学生!
2、带参数(性别 sex)的方法,描述为:我是一个孩!(其中,为传入参数)
3、无参无返回值的 mySelf 方法,介绍自己的姓名、年龄、年级(参数参考效果图)
创建测试类
实例化对象,传入参数,调用无参无返回值的 student 和 mySelf 方法及带参方法 sex
public class Person {private String name;private int age;private String grade;public Person(String name,int age,String grade){this.name=name;this.age=age;this.grade=grade;}public void student(){System.out.println("我是一名学生");}public void toPrint(String sex){System.out.println("我是一个"+sex+"孩");}public void mySelf(){System.out.println("我叫"+name+","+"今年"+age+"岁了,读小学"+grade+"了");}}
public class Main {public static void main(String[] args) {// write your code herePerson person = new Person("李明",12,"六");person.student();person.toPrint("男");person.mySelf();}}
4. 编写自定义猴子类,按照效果图,编写代码。
程序参考运行效果图如下:
提示:
创建 Monkey 类
属性:名称(name)和特征(feature)
方法:
1) 无参构造方法(默认初始化 name 和 feature 的属性值,属性值参考效果图)
2) 带参构造方法,接收外部传入的参数,分别向 name 和 feature 赋值
创建测试类
分别通过无参构造方法和带参构造方法,完成对象实例化实例化对象,并打印输出对
象信息,输出格式如效果图。
public class Monkey {private String name;private String feature;public Monkey(String name, String feature) {this.name = name;this.feature = feature;System.out.println("我是使用带参构造产生的猴子");}public Monkey() {this.name="长尾猴";this.feature="尾巴长";System.out.println("我是使用无参构造产生的猴子");}public void print(){System.out.println("名称:"+name);System.out.println("特征:"+feature);}}
public class Main {public static void main(String[] args) {// write your code hereMonkey monkey1 = new Monkey();monkey1.print();System.out.println("============================");Monkey monkey2 = new Monkey("白头叶猴","头上有白毛,喜欢吃树叶");monkey2.print();}}
5.编程练习
编写自定义类实现图书信息设置,根据效果图编写代码。
运行参考效果如下所示:
提示:
属性:书名、作者、出版社、价格
方法:信息介绍
要求:
1、 设计构造函数实现对属性赋值
2、 设置私有属性,get/set 方法实现对属性的访问
3、 限定图书价格必须大于 10,如果无效需进行提示,并强制赋值为 10
4、 限定作者、书名均为只读属性
5、信息介绍方法描述图书所有信息
public class BookInfo {private String bookName;private String author;private String publishingHouse;private float price;public BookInfo(String bookName, String author, String publishingHouse, float price) {this.bookName = bookName;this.author = author;this.publishingHouse = publishingHouse;//this.price = price;setPrice(price);}public String getBookName() {return bookName;}// public void setBookName(String bookName) {// this.bookName = bookName;// }public String getAuthor() {return author;}// public void setAuthor(String author) {// this.author = author;// }public String getPublishingHouse() {return publishingHouse;}public void setPublishingHouse(String publishingHouse) {this.publishingHouse = publishingHouse;}public float getPrice() {return price;}public void setPrice(float price) {if(price<10.0f){System.out.println("图书价格最低为10元");this.price=10.0f;}else{this.price = price;}}public void print(){System.out.println("书名:"+bookName);System.out.println("作者:"+author);System.out.println("出版社:"+publishingHouse);System.out.println("价格:"+price+"元");}}
public class Main {public static void main(String[] args) {// write your code hereBookInfo book1 = new BookInfo("红楼梦","曹雪芹","人民文学出版社",9.0f);book1.print();System.out.println("==============================");BookInfo book2 = new BookInfo("小李飞刀","古龙","中国长安出版社",55.5f);book2.print();}}
