- 使用while循环和if语句实现计算并输出1-100的偶数和,循环变量名为i,存放和的变量名为sum。
public class Test { public static void main(String[] args) { int sum=0,i=1; while(i<=100) { if(i%2==0) { sum=sum+i; } i++; } System.out.println(sum); } }
- 使用while循环和if语句实现计算并输出1-100的能被3整除的数的和,循环变量名为i,存放和的变量名为sum。
public class Test { public static void main(String[] args) { int sum=0,i=1; while(i<=100) { if(i%3==0) { sum=sum+i; } i++; } System.out.println(sum); } }
- 使用for循环和if语句实现输出1到100中能被7整除或者个位数是7的数字,循环变量名为i。
public class Test { public static void main(String[] args) { int sum=0,i=1; while(i<=100) { if(i%7==0 || i%10==7) { sum=sum+i; } i++; } System.out.println(sum); } }
- 编写一个应用程序,计算整数n的阶乘,n的初始值为8,并将结果输出,循环变量名为i,存放阶乘的变量名为p。
public class test { public static void main(String[] args) { int n=8,i=1,p=1; i=n; while(i>0) { p=p*i; i--; } System.out.println(p); } }
- 使用while循环和if语句实现计算并输出1-100的奇数和,循环变量名为i,存放和的变量名为sum。
public class Test { public static void main(String[] args) { int sum=0,i=1; while(i<=100) { if(i%2==1) { sum=sum+i; } i++; } System.out.println(sum); } }
- 假设某员工今年的年薪(salary)是80000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。循环变量名为i,总收入的变量名为total。
public class Prog1{ public static void main(String[] args) { int i; double salary=80000,total=80000; for(i=1;i<10;i++) { salary=salary*106/100; total=total+salary; } System.out.println("10 years later,salary:"+salary); System.out.println("10 years total:"+total); } }
- 创建Rectangle类与对象。1)创建一个Rectangle类;2)属性:两个double成员变量,width和height;3)不带参数的构造方法:width和height的初始值分别是6和8;4)方法:计算并输出矩形的周长方法名为findPremeter ();5)编写测试类,创建一个Rectangle的对象,对象名为r,调用周长和面积的方法。
public class Rectangle { private double width,height; public Rectangle() { this.width=6; this.height=8; } public double findPremeter() { return 2*(this.width+this.height); } } public class Test { public static void main(String[] args) { Rectangle r = new Rectangle(); System.out.println(r.findPremeter()); } }
- 创建类Student,属性有平时成绩(pingshi),期末成绩(qimo);不带参数的构造方法,方法有计算并输出总成绩的方法calculateScore(),计算方式为:总成绩=平时成绩+期末成绩的1/2;创建测试类,创建Student对象s,然后调用calculateScore()方法来输出总成绩。
public class Student { public int pingshi,qimo; public Student(){} public int calculateScore() { return pingshi+qimo/2; } } public class Test { public static void main(String[] args) { Student s = new Student(); s.pingshi=40; s.qimo=50; System.out.println(s.calculateScore()); } }
- 定义Circle类,包含一个名为radius属性,类型为int,不带参数的构造方法和计算并输出面积方法findArea(面积=3.14*radius*radius),编写测试类,创建Circle类的一个对象c,调用findArea方法。
class Circle { public int radius; public Circle(){} public double findArea() { return radius*radius*3.14; } } public class Test { public static void main(String[] args) { Circle c = new Circle(); c.radius=2; System.out.println(c.findArea()); } }
- 定义一个Book类,表示图书,该类的属性包括name(书名)、author(作者名)、price(书的价格),定义不带参数的构造方法,定义输出图书基本信息的show方法。编写测试类,创建Book类的一个对象b,调用show方法。
public class Book { public String name,author; public double price; public Book(){} public void show() { System.out.println("name:"+this.name); System.out.println("author:"+this.author); System.out.println("price:"+this.price); } } public class Test { public static void main(String[] args) { Book b = new Book(); b.name="Java Program"; b.author="panda"; b.price=3.14; b.show(); } }
- 定义一个商品类Goods,该类有:商品名称(String)、商品编号(String)和价格(double)三个属性,有无参的构造方法和计算折扣价格的方法,方法头为public void computeDiscout (double percent),其中形参代表打折的百分比。编写测试类,创建商品类的对象,调用计算折扣价格的方法。
public class Goods { public String name,number; public double price; public Goods(){} public void computeDiscout(double percent) { System.out.println(price*percent/100); } } public class Test { public static void main(String[] args) { Goods g = new Goods(); g.name="Java Book"; g.number="G001"; g.price=3.14; g.computeDiscout(90); } }
扫描二维码关注公众号,回复: 15294033 查看本文章 - 现有父类Person,结构如下:
class Person { String id; String name; Person(String id, String name) { this.id = id; this.name = name; } void print() { System.out.println("id =" + id + ",name =" + name); } } 在此基础上派生出子类Teacher,子类定义了自己的属性教师编号(teacherID), 有不带参数的构造方法,覆盖了父类的print方法,调用父类被覆盖的print方法, 增加打印自己的属性的语句,请实现Teacher类的编写。 class Teacher extends Person { private String teacherID; Teacher(){ super(null,null); } public void print() { super.print(); System.out.println("teacherID:"+this.teacherID); } }
- 现有父类Good,结构如下:
class Goods { double unitPrice;//单价 double account;//数量 Goods(double unitPrice, double account) { this.unitPrice=unitPrice ; this.account=account ; } double totalPrice() {//计算总价格 return unitPrice * account; } void show() { System.out.println("单价是" + unitPrice); System.out.println("购买数量为" + account); System.out.println("购买商品的总金额是" + this.totalPrice()); } } 在此基础上派生出子类Milk,子类定义了自己的属性会员价格(vipPrice), 有不带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法, 增加打印自己的属性的语句,请实现Milk类的编写。 class Milk extends Goods { private double vipPrice; Milk() { super(0,0); } void show() { super.show(); System.out.println(this.vipPrice); } }
- 编写程序模拟“挑战杯”演讲大赛,共有10个评委打分,分数为1到10之间的随机数,将10个分数存放在int类型数组中,编程利用for循环实现计算歌手的最后得分。
public class Test { public static void main(String[] args) { int[] score = new int[10]; int max=1,min=10,avg=0,sum=0; for(int i=0;i<10;i++) { score[i]=(int)(1+Math.random()*(10-1+1)); //System.out.println(score[i]); } for(int i=0;i<10;i++) { if(score[i]>max) max=score[i]; if(score[i]<min) min=score[i]; sum=sum+score[i]; } avg=sum/10; System.out.println("max= "+max);//最高分 System.out.println("min= "+min);//最低分 System.out.println("avg= "+avg);//平均分 } }
- 已知一个int类型的数组a,数组元素分别是{ 12,11,78,34},编写程序利用for循环将数组逆序输出。
public class Test { public static void main(String[] args) { int[] a = {12,11,78,34}; int i=a.length; for(i=i-1;i>=0;i--) System.out.print(a[i]); } }
- 定义一个长度为100的布尔型数组,数组名为fig,并用for循环语句将数组所有元素赋值为false。
public class Test { public static void main(String[] args) { int[] a = {34,56,78,89,90,100}; for(int i=0;i<a.length;i++) System.out.print(a[i]/10%10); } }
- 某一歌手参加歌曲大奖赛,有20个评委对她的进行打分,存放在一个数组里score[],分数为1到100之间的随机数,编程利用for循环实现输出这位选手的最高分、最低分和最终得分(最终得分=总分数/评委在总人数)
public class Test { public static void main(String[] args) { int[] score = new int[20]; int max=1,min=100,avg=0,sum=0; for(int i=0;i<20;i++) { score[i]=(int)(1+Math.random()*(100-1+1)); //System.out.println(score[i]); } for(int i=0;i<20;i++) { if(score[i]>max) max=score[i]; if(score[i]<min) min=score[i]; sum=sum+score[i]; } avg=sum/20; System.out.println("max= "+max);//最高分 System.out.println("min= "+min);//最低分 System.out.println("avg= "+avg);//平均分 } }
-
下面是Name类、Person类和Test类,请将Name类和Person类中所有private数据域的访问器方法填写完整。
class Name { private String firstName;//姓 private String lastName;//名 Name(String f,String l) { firstName=f; lastName=l; } //填写访问器方法 public String toString() { return firstName + lastName; } } class Person { private Name name;//姓名 Person(Name n) { name=n; } //填写访问器方法 } class Test { public static void main(String[] args) { Name theName=new Name("张","三"); Person p=new Person(theName); System.out.println(p.getName());//输出结果:张三 } } class Name { private String firstName;//姓 private String lastName;//名 Name(String f,String l) { firstName=f; lastName=l; } public String getFirstName() { return firstName; } public void setFirstName(String fn) { firstName=fn; } public String getLastName() { return lastName; } public void setLastName(String ln) { lastName=ln; } public String toString() { return firstName + lastName; } } class Person { private Name name;//姓名 Person(Name n) { name=n; } public Name getName() { return name; } public void setName(Name n) { name=n; } } class Test { public static void main(String[] args) { Name theName=new Name("张","三"); Person p=new Person(theName); System.out.println(p.getName());//输出结果:张三 } }
Java初学者基础编程练习
猜你喜欢
转载自blog.csdn.net/shanhe_yuchuan/article/details/122311891
今日推荐
周排行