3-1
public class Task1 {
public static void main(String[] args) {
int[] monthList = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//十二月份
int sum = 0;
Scanner input = new Scanner(System.in);
System.out.println("请输入某年某月某日:");
System.out.print("年:");
int year = input.nextInt();
System.out.print("月:");
int m = input.nextInt();
System.out.print("日:");
int d = input.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
for (int i = m - 1; i > 0; i--) {
sum = sum + monthList[i - 1];
}
if (m > 2) {
sum += 1;//闰年2月多一天
}
} else {
for (int i = m - 1; i > 0; i--) {
sum = sum + monthList[i - 1];
}
}
sum += d;//月份的天数加上日子的天数
System.out.println(year + "年" + m + "月" + d + "日" + "是这一年的第" + sum + "天");
}
}
public class Task2 {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + "*" + j + "=" + i * j+" ");
}
System.out.println();
}
}
}
public class Task3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一个三位树的整数:");
int n = input.nextInt();
boolean flag = isShuiXianHua(n);
System.out.println(n + (flag ? "是" : "不是") + "水仙花数");
}
public static boolean isShuiXianHua(int n) {
//个位数
int ge = n % 10;
//十位数
int shi = n / 10 % 10;
//百位数
int bai = n / 10 / 10 % 10;
int i = ge * ge * ge + shi * shi * shi + bai * bai * bai;
return i==n;
}
}
3-2
public class Task1 {
public static void main(String[] args) {
int[][] a = new int[30][5];//存放30个学生的5门成绩
int[] b = new int[30]; //存放每个学生5门课程的平均成绩
//输入每个学生的5门成绩
for (int i = 0; i < a.length; i++) {
System.out.print("第" + (i + 1) + "个学生的5门成绩:\t");
int count = 0; //局部变量,学生的成绩
for (int j = 0; j < a[i].length; j++) {
a[i][j] = new Random().nextInt(101);//随机生成一个学生一门成绩分数
count += a[i][j]; //学生5门成绩的总分
System.out.print(a[i][j] + "\t");
}
count = count / 5; //学生5门成绩的平均分
b[i] = count; //存放到b数组中
System.out.println("");
System.out.println("第" + (i + 1) + "个学生的5门成绩的平均分为:\t" + b[i]);
System.out.println();
}
for (int i = 0; i < b.length; i++) {
System.out.println("第" + (i + 1) + "个学生的5门成绩的平均分为:\t" + b[i]);
}
}
}
public class Task2 {
public static void main(String[] args) {
int factorial = getFactorial(5);
System.out.println("5的阶乘:" + factorial);
}
public static int getFactorial(int i) {
int sum = 1;
if (i < 0)
throw new IllegalArgumentException("必须为正整数!");
if (i == 1) {
return 1;
} else {
return i * getFactorial(i - 1);
}
}
}
3-3
public class Task1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int count = 0;
int sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 7 == 0) {
count++;
sum += i;
}
}
System.out.println("1~" + n + "之间能被 7 整除的整数的个数:" + count);
System.out.println("被 7 整 除的数的和:" +sum);
}
}
public class Task2 {
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
for (int i = 2; i <= n; i++) {
boolean flag = true;
for (int j = 2; j < i; j++) {
if (0 == i % j) {
flag = false;
break;
}
}
if (flag) {
System.out.print(i + " ");
}
}
}
}
public class Task3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入一个整数:");
int number = in.nextInt();
for (int i = 1; i <= number * 2 - 1; i++) {
//上半部分
if (i <= number) {
for (int j = number; j > i; j--) {
System.out.print(" ");
}
for (int x = 1; x <= i * 2 - 1; x++) {
System.out.print("*");
}
//下半部分
} else {
for (int j = 1; j <= i - number; j++) {
System.out.print(" ");
}
for (int x = number * 2 - 2; x > (i - number) * 2 - 1; x--) {
System.out.print("*");
}
}
System.out.println();
}
}
}
3-4
public class Task1 {
public static void main(String[] args) {
int count = 4 * 2;
for (int j = 0; j <= count; j += 2) {
//i=列的最大个数
for (int i = 7; i > j; i--) {
System.out.print("*");
}
System.out.println("");
}
}
}
public class Task2 {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner scanner = new Scanner(System.in);
System.out.println("输入十个数:");
for (int i = 0; i < 10; i++) {
arr[i]=scanner.nextInt();
}
int max=0;
for (int i = 0; i < 10; i++) {
if (max<arr[i]){
max=arr[i];
}
}
System.out.println("最大值为:"+max);
}
}
public class Task3 {
public static void main(String[] args) {
int n =new Scanner(System.in).nextInt();
int result = getResult(n);
System.out.println("结果:"+result);
}
public static int getResult(int n) {
int length = Integer.toString(n).length();
int p = 1;//获取的位数
int m = 10;//取余
int num = 1;//存储数字各个位数的乘积
for (int i = 1; i <= length; i++) {
int g = n / p % m;
p *= 10;
num *= g;
}
return num;
}
}
3-5
public class Task2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入定价:");
float price = scanner.nextFloat();
System.out.println("输入您要购买的数量:");
int n = scanner.nextInt();
double discount;
if (n > 100) {
discount = 0.8;
} else if (n > 10) {
discount = 0.85;
} else {
discount = 0.9;
}
System.out.println("您购买" + n + "本价格" + price + "的总价:" + n * discount * price);
}
}
public class Task2 {
public static void main(String[] args) {
String str = " ";
int sum = 0;
for (int i = 100; i <= 200; i++) {
if (i % 10 == i / 100) {
sum += i;
System.out.print(i + " ");
}
}
System.out.println("\n100—200 的范围内所有回文数的和为:" + sum);
}
}
3-9
public class Task1 {
public static void main(String[] args) {
int i = new Scanner(System.in).nextInt();
if (i > 100) {
System.out.println("Score is error!");
} else if (i >= 90) {
System.out.println("A");
} else if (i >= 80) {
System.out.println("B");
} else if (i >= 70) {
System.out.println("C");
} else if (i >= 60) {
System.out.println("D");
} else if (i >= 0) {
System.out.println("E");
} else {
System.out.println("Score is error!");
}
}
}
public class Task2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
double n = sc.nextInt();
double m = sc.nextInt();
double sum = 0;
for (int i = 0; i < m; i++) {
sum += n;
n = Math.sqrt(n);
}
System.out.printf("%.2f\n", sum);
}
}
}
public class Task3 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int t;
double sum;
t = reader.nextInt();
while(t > 0) {
int n = reader.nextInt();
int flag = 1;
sum = 0;
if(n >= 300)
n = 300;
for(int i = 1; i <= n; i++) {
sum += (flag * 1.0)/ i;
flag = -flag;
}
t--;
System.out.printf("%.2f\n", sum);
}
reader.close();
}
}
3-10
public class Task1 {
public static void main(String[] args) {
double arr[] = {9.8, 12, 45, 67, 23, 1.98, 2.55, 45};
double max = 0;
double min = 100;
double sum = 0;
for (double v : arr) {
if (v > max) {
max = v;
}
if (v < min) {
min = v;
}
sum+=v;
}
System.out.println("{9.8, 12, 45, 67, 23, 1.98, 2.55, 45}中最大值为:"+max);
System.out.println("{9.8, 12, 45, 67, 23, 1.98, 2.55, 45}中最小值为:"+min);
System.out.println("{9.8, 12, 45, 67, 23, 1.98, 2.55, 45}的平均值为:"+sum/arr.length);
}
}
public class Task2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一个2~10的正整数:");
int n = input.nextInt();
int sum = 1;
for (int i = 1; i <= n; i++) {
sum += Integer.parseInt("2" + i);
}
System.out.println(sum);
}
}
public class Task3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入你的工资:");
double i = sc.nextDouble();
System.out.println("你要交的税为:" + IsMoney(i));
}
public static double IsMoney(double money) {
double i = money;
double s = i - 3000;
double j = 0;
double M = 1500 * 0.05;// 一级税额
if (s > 0) {
if (s <= 1500) {
j = s * 0.05;
} else if (s <= 4500) {
j = M + (s - 1500) * 0.1;
} else if (s <= 9000) {
j = M + 3000 * 0.1 + (s - 4500) * 0.2;
} else if (s <= 35000) {
j = M + 3000 * 0.1 + 4500 * 0.2 + (s - 9000) * 0.25;
} else if (s <= 55000) {
j = M + 3000 * 0.1 + 4500 * 0.2 + 26000 * 0.25 + (s - 35000)
* 0.3;
} else if (s <= 80000) {
j = M + 3000 * 0.1 + 4500 * 0.2 + 26000 * 0.25 + 20000 * 0.3
+ (s - 55000) * 0.35;
} else {
j = M + 3000 * 0.1 + 4500 * 0.2 + 26000 * 0.25 + 20000 * 0.3
+ 25000 * 0.35 + (s - 80000) * 0.45;
}
return j;
} else {
System.out.println("不交税!");
}
return 0;
}
}
3-15
public class Task1 {
public static void main(String[] args) {
int num = 0;//计算累加
for (int i = 1; i <= 10000; i++) { //一一尝试10000以内的数字
for (int j = 1; j < i; j++) { //验证对应的i是不是完全数
if (i % j == 0) {
num += j;//如果j是这个i的因子就把j加到num里面
}
}
if (i == num) {
System.out.println("数字 " + i + " 是完全数..");
}
num = 0;//清零
}
}
}
public class Task2 {
public static void main(String[] args) {
int total = 0;
for (int i = 0; i <= 20; i++) {
for (int j = 0; j <= 50; j++) {
for (int k = 0; k <= 100; k++) {
if (5 * i + 2 * j + k == 100)
total++;
}
}
}
System.out.println("将一元钱换成5分,2分和1分的硬币,兑换的方法数一共有" + total + "种");
}
}
public class Task3 {
public static void main(String[] args) {
Square square = new Square();
Circle circle = new Circle();
double area = square.area(2);
double area1 = circle.area(3);
System.out.println("边长为2的正方形面积" + area);
System.out.println("半径为3的圆形面积为:" + area1);
}
}
interface Shape {
abstract double area(double n);
}
class Square implements Shape {
@Override
public double area(double n) {
return n * n;
}
}
class Circle implements Shape {
@Override
public double area(double n) {
return 3.14 * n * n;
}
}
3-16
public class Task1 {
public static void main(String[] args) {
int total = 0;
for (int i = 0; i <= 20; i++) {
for (int j = 0; j <= 50; j++) {
for (int k = 0; k <= 100; k++) {
if (5 * i + 2 * j + k == 100)
total++;
}
}
}
System.out.println("将一元钱换成5分,2分和1分的硬币,兑换的方法数一共有" + total + "种");
}
}
public class Task2 {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner scanner = new Scanner(System.in);
System.out.println("输入十个数:");
for (int i = 0; i < 10; i++) {
arr[i] = scanner.nextInt();
}
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
public class Task3 {
public static void main(String[] args) {
//向上转型,把Bird类型转换为Animal类型
Animal animal = new Bird();
animal.eat();
Bird bird = (Bird) animal;
// 向下转型,把Animal类型转换为Bird类型
bird.fly();
}
}
class Animal {
public void eat() {
System.out.println("吃东西");
}
}
interface A {
abstract void fly();
}
class Bird extends Animal implements A {
@Override
public void eat() {
System.out.println("鸟儿吃虫");
}
@Override
public void fly() {
System.out.println("鸟儿飞翔");
}
}
3-17
//输出斐波那契数列
public class Task1 {
public static void main(String[] args) {
int a = 1;
int b = 1;
for(int i = 1;i <= 20;i++) {
System.out.print(a + "\t" + b + "\t");
a = a + b;
b = a + b;
}
}
}
//有一条长阶梯,若每步跨2阶,则最后剩余1阶,若每步跨3阶,则最后剩2阶,若每步跨5阶,则最后剩4阶,若每步跨6阶则最后剩5阶,若每步跨7阶,最后才正好一阶不剩。请问,这条阶梯共有多少阶?
public class Task2 {
public static void main(String[] args) {
for (int i = 1; ; i++) {
if (i % 2 == 1 &&
i % 3 == 2 &&
i % 5 == 4 &&
i % 6 == 5 &&
i % 7 == 0) {
System.out.println(i);
break;
}
}
}
}
public class Task3 {
public static void main(String[] args) {
Student student = new Student("阿红",10,2002222);
Worker worker = new Worker("阿清",30,1988888);
System.out.println("学号:"+student.getStuId()+",姓名:"+student.getName()+",年龄:"+student.getAge());
student.study();
student.eat();
System.out.println("工号:"+worker.getWorkId()+",姓名:"+worker.getName()+",年龄:"+worker.getAge());
worker.work();
worker.eat();
}
}
abstract class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
abstract public void eat();
}
class Student extends Person {
private int stuId;
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public Student(String name, int age, int stuId) {
super(name, age);
this.stuId = stuId;
}
@Override
public void eat() {
System.out.println("学生在食堂吃饭");
}
public void study() {
System.out.println("我爱学习");
}
}
class Worker extends Person {
private int workId;
public int getWorkId() {
return workId;
}
public void setWorkId(int workId) {
this.workId = workId;
}
public Worker(String name, int age, int workId) {
super(name, age);
this.workId = workId;
}
@Override
public void eat() {
System.out.println("工人在工厂食堂吃饭");
}
public void work() {
System.out.println("我爱工作");
}
}
--------Before--------
3_11
public class Task1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个月份:");
int month = scan.nextInt();
switch (month) {
case 12:
case 1:
case 2:
System.out.print("您输入的月份属于冬季。");
break;
case 3:
case 4:
case 5:
System.out.print("您输入的月份属于春季");
break;
case 6:
case 7:
case 8:
System.out.print("您输入的月份属于夏季");
break;
case 9:
case 10:
case 11:
System.out.print("您输入的月份属于秋季");
break;
default:
System.out.print("您输入的月份:" + month + " 未在1~12范围内!");
}
}
}
public class Task2 {
public static void main(String[] args) {
int year = 2017;
for (double i = 0.85; i < 10; year++) {
i = i * (1 + 0.32);
}
System.out.println("店铺盈利到10万元需要:" + year + " 年");
}
}
public class Task3 {
public static void main(String[] args) {
Circle circle = new Circle(10);
System.out.println("半径10cm的圆面积为:" + circle.mianJI() + " 周长:" + circle.zhouChang());
Rect rect = new Rect(10, 6);
System.out.println("长10cm、宽6cm的矩形面积为:" + rect.mianJI() + " 周长:" + rect.zhouChang());
}
}
abstract class Shape {
abstract double mianJI();
abstract double zhouChang();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
double mianJI() {
return Math.PI * radius * radius;
}
@Override
double zhouChang() {
return 2 * Math.PI * radius;
}
}
class Rect extends Shape {
private double length;
private double height;
public Rect(double length, double height) {
this.length = length;
this.height = height;
}
@Override
double mianJI() {
return length * height;
}
@Override
double zhouChang() {
return 2 * (length + height);
}
}
3_12
public class Task1 {
public static void main(String[] args) {
int n = 10;
int arr[] = new int[n];
Scanner scanner = new Scanner(System.in);
System.out.println("输入十个数,空格隔开");
for (int i = 0; i < 10; i++) {
arr[i] = scanner.nextInt();
}
System.out.println("待排序列:" + Arrays.toString(arr));
for (int i = 0; i < n - 1; i++) {
boolean flag = false;
for (int j = n - 1; j > i; j--) {
if (arr[j - 1] > arr[j]) {
int tmp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = tmp;
flag = true;
}
}
}
System.out.println("已排序列:" + Arrays.toString(arr));
}
}
public class Task2 {
public static void main(String[] args) {
int sum = 0, i = 1;
while (i <= 200) {
if (i % 3 == 0) {
sum += i;
}
i++;
}
System.out.println("1~200之间3倍数之和:"+sum);
}
}
public class Task3 {
public static void main(String[] args) {
Person person = new Person("zhangsan", 18);
person.show();
Student student = new Student("lisi", 20, "qinghai");
student.show();
}
}
class Person {
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void show() {
System.out.println("姓名:" + this.name + " 年龄:" + this.age);
}
}
class Student extends Person {
private String school;
public Student(String name, int age, String school) {
super(name, age);
this.school = school;
}
public void show() {
System.out.println("姓名:" + this.name + " 年龄:" + this.age + " 学校:" + this.school);
}
}
3_13
public class Task1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入消费金额:");
int money = scanner.nextInt();
double discount;
int c = (money < 200 ? 1 : 0)
+ (money < 400 ? 1 : 0)
+ (money < 600 ? 1 : 0)
+ (money < 800 ? 1 : 0)
+ (money < 1000 ? 1 : 0);
switch (c) {
case 0:
discount = 0.5;
System.out.println("消费在大于1000,享受5折优惠,折后价:" + discount * money);
break;
case 1:
discount = 0.6;
System.out.println("消费在800~999之间,享受6折优惠,折后价:" + discount * money);
break;
case 2:
discount = 0.7;
System.out.println("消费在600~799之间,享受7折优惠,折后价:" + discount * money);
break;
case 3:
discount = 0.8;
System.out.println("消费在400~599之间,享受折8优惠,折后价:" + discount * money);
break;
case 4:
discount = 0.9;
System.out.println("消费在200~399之间,享受9折优惠,折后价:" + discount * money);
break;
case 5:
System.out.println("消费小于200,无折扣");
break;
}
}
}
public class Task2 {
public static void main(String[] args) {
final int max = 1000;
double item = 0;
double sum = 0;
int flag = -1;
int n = 0;
for (n = 0; n <= max; n++) {
flag *= -1;
item = flag * 1.0 / (2 * n + 1);
sum += item;
}
System.out.println(4 * sum);
}
}
public class Task3 {
public static void main(String[] args) {
Flower flower = new Flower("白色", 10);
flower.showInfo();
Rose rose = new Rose("紫色",30,"大理");
rose.showInfo();
rose.warn();
}
}
class Flower {
private String color;
private double price;
public Flower() {
}
public Flower(String color, double price) {
this.color = color;
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void showInfo() {
System.out.println("花的颜色是" + this.color+",价格是 " + this.price +"元");
}
}
class Rose extends Flower {
private String origin;
public Rose(String color, double price, String origin) {
super(color, price);
this.origin = origin;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
@Override
public void showInfo() {
super.showInfo();
System.out.println("产地是" + this.origin);
}
public void warn(){
System.out.println("不要摘玫瑰花,小心扎手!");
}
}
3_14
public class Task1 {
public static void main(String[] args) {
double money = 0;
for (int i = 0; i < 5; i++) {
money = (money + 1000) / (1 + 12 * 0.0063);
}
System.out.println("存钱时应存入:" + money);
}
}
public class Task2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个0~99999 之间的任意数");
int number = sc.nextInt();
if (number / 10000 >= 1 && number / 10000 < 10) {
System.out.println(number + " 是5位数");
} else if (number / 1000 >= 1) {
System.out.println(number + " 是4位数");
} else if (number / 100 >= 1) {
System.out.println(number + " 是3位数");
} else if (number / 10 >= 1) {
System.out.println(number + " 是2位数");
} else if (number >= 1) {
System.out.println(number + " 是1位数");
}
}
}
public class Task3 {
public static void main(String[] args) {
SportsCar car1 = new SportsCar("宝马");
car1.run();
car1.price();
}
}
abstract class Car{
private String brand;
public Car(String brand) {
this.brand = brand;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
abstract void run();
}
class SportsCar extends Car{
public SportsCar(String brand) {
super(brand);
}
@Override
void run() {
System.out.println("超级跑车");
}
public void price(){
System.out.println("售价100w");
}
}
评论区