package EveryDay;
import java.util.Arrays;
public class Demo03 {
public static void main(String[] args) {
// 静态初始化数组保存学生成绩
int[] scores = {67, 90, 78, 96, 80, 79, 94};
System.out.println("考试的前三名是:");
// 调用方法
demo(scores);
}
// 定义方法完成排序输出前三名的成绩
public static void demo(int[] scores){
Arrays.sort(scores); // 排序
int count = 0; // 保存有效成绩的数量
for (int i = scores.length - 1; i >= 0; --i) { // 倒序遍历数组中的每一个元素
if (scores[i] > 100 || scores[i] < 0){ // 判断成绩的有效性
continue; // 如果成绩无效,则跳出循环,忽略此成绩
}
count++; // 成绩有效加1
if (count > 3){ // 判断有效成绩的数量
break; // 如果有效成绩大于3,则结束循环,只输出成绩的前三名
}
System.out.println(scores[i]);
}
}
}
2、统计一个班级的所有学生成绩后五名的平均值
package EveryDay;
public class Demo04 {
public static void main(String[] args) {
int[] scores = {67, 90, 78, 96, 80, 79, 94, 100, 85, 91};
for (int i = 0; i < scores.length - 1; i++) {
for (int j = 0; j < scores.length - i - 1; j++) {
if (scores[j] < scores[j + 1]) {
int temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
int avg = 0;
int sum = 0;
for (int i = scores.length - 5; i < scores.length; i++) {
sum += scores[i];
}
avg = sum / 5;
System.out.println("考试后五名成绩的平均值是:" + avg);
}
}
3、一个类A有两个成员变量v、num,v有一个初值100。 定义一个方法guess,对A类的成员变量v,用num进行猜。 如果大了则提示大了,小了则提示小了。等于则提示猜测成功。在main方法中测试。
package EveryDay;
public class A {
// 被猜测的数字
public double v = 0;
// 猜测方法
public void guess(double num) {
if (num > v) {
System.out.println("猜大了");
} else if (num < v) {
System.out.println("猜小了");
} else {
System.out.println("猜对了");
}
}
}
package EveryDay;
// 测试类
public class ATest {
public static void main(String[] args) {
A a = new A();
a.guess(1);
a.guess(2);
a.guess(3);
}
}
4、创建一个圆Circle类。为该类提供一个变量r表示半径,一个常量PI表示圆周率;同时为该类提供两个方法:方法一用于求圆的面积,方法二用于求圆的周长;为该类提供一个无参的构造方法,用于初始化r的值为4。为该类提供一个有参的构造方法,用于初始化r的值。在main方法中测试。
package EveryDay;
public class Circle {
public double r; // 半径
// 加final后就不可修改
public final double PI = 3.14; // 圆周率
// 求周长的方法
public double perimeter() {
return PI * r * r;
}
// 求面积的方法
public double area() {
return 2 * PI * r;
}
// 无参构造
public Circle() {
r = 4;
}
// 有参构造
public Circle(double r) {
this.r = r;
}
}
package EveryDay;
public class CircleTest {
public static void main(String[] args) {
Circle c1 = new Circle();
System.out.println(c1.r);
System.out.println("圆的面积是:" + c1.area());
System.out.println("圆的周长是:" + c1.perimeter());
Circle c2 = new Circle();
System.out.println(c2.r);
System.out.println("圆的面积是:" + c2.area());
System.out.println("圆的周长是:" + c2.perimeter());
}
}
5、定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(int x0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动,创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。
package EveryDay;
public class Point {
public double x;
public double y;
// 无参构造器
public Point() {
}
// 有参构造器
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public void movePoint(int dx, int dy) {
x += dx;
y += dy;
}
}
package EveryDay;
public class PointTest {
public static void main(String[] args) {
Point p = new Point();
p.movePoint(4,4);
System.out.println(p.x);
System.out.println(p.y);
Point p2 = new Point();
p2.movePoint(6,6);
System.out.println(p2.x);
System.out.println(p2.y);
}
}
6、定义一个矩形类Rectangle:(1) 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。(2) 有2个属性:长length、宽width(3) 通过构造方法Rectangle(int width, int length),分别给两个属性赋值(4) 创建一个Rectangle对象,并输出相关信息
package EveryDay;
public class Rectangle {
// 长
public double length;
// 宽
public double width;
// 求面积
public double getArea() {
return length * width;
}
// 求周长
public double getPer() {
return (length + width) * 2;
}
// 输出长、宽、面积、周长
public void showAll() {
System.out.println(length);
System.out.println(width);
System.out.println(getArea());
System.out.println(getPer());
}
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
}
package EveryDay;
public class RectangleTest {
public static void main(String[] args) {
Rectangle r = new Rectangle(10, 20);
r.showAll();
}
}
7、为“无名的粉”写一个类:class WuMingFen
要求:
(1).有三个属性:面码:String theMa 粉的分量(两):int quantity
是否带汤:boolean likeSoup
(2).写一个普通方法:check(),用于查看粉是否符合要求。即:将对象的三个属性打印在控制台上。
(3).写一个构造方法,以便于简化初始化过程,
如,叫一碗3两牛肉带汤的粉:
WuMingFen f1 = new WuMingFen(“牛肉”,3,true);
(4).重载构造方法,使得初始化过程可以多样化,
比如叫一碗2两鸡丝粉,则默认是带汤的:
WuMingFen f2 = new WuMingFen(“鸡丝”,2);
(5).如何顾客这样说:老板,一碗粉。则默认是酸辣面码、2两、带汤的?
WuMingFen f3 = new WuMingFen();
(6).调用check()方法,将上面每碗粉的属性都打印在控制台上。
package EveryDay;
public class WuMingFen {
// 面码
public String theMa;
// 分量
public int quantity;
// 是否带汤
public boolean likeSoup;
//构造方法
public void check() {
String str = likeSoup ? "带汤" : "不带汤";
System.out.println("叫一碗" + quantity + "两" + theMa + str + "的粉");
}
public WuMingFen(String theMa, int quantity, boolean likeSoup) {
// this.theMa = theMa;
// this.quantity = quantity;
this(theMa,quantity);
this.likeSoup = likeSoup;
}
public WuMingFen(String theMa, int quantity) {
this.theMa = theMa;
this.quantity = quantity;
// this.likeSoup = true;
}
public WuMingFen() {
// this.theMa = "酸辣";
// this.quantity = 2;
// this.likeSoup = true;
this("酸辣粉",2,true);
}
}
package EveryDay;
public class WuMingFenTest {
public static void main(String[] args) {
WuMingFen wm = new WuMingFen();
wm.check();
WuMingFen wm1 = new WuMingFen("牛肉",3,true);
wm1.check();
WuMingFen wm2 = new WuMingFen("鸡丝",2);
wm2.check();
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)