耗子,数列 之和,今年第几天,建立新类型

来源:互联网 发布:分答 mac版 编辑:程序博客网 时间:2024/06/11 18:53
1、public class Mouth {
// 一对老耗子,每个月生一对小耗子,小耗子,
// 4个月后就自动变成老耗子。问24个月之后,耗子不死的情况下, 总共多少只?
public static void main(String[] args) {


int old = 2;
int first = 0;
int second = 0;
int third = 0;
for (int i = 0; i < 24; i++) {
old = old + third;
third = second;
second = first;
first = old;
}
System.out.println(old+first+second+third);
}

}

2、打印数组之和

public class Shulie {
//打印  2/1   3/2   5/3    8/5  13/8   21/13;前20项之和
public static void main(String[] args) {
double mu = 1;
double zi = 2;
double sum = 0;
for (int i = 0; i < 20; i++) {
sum += zi / mu;
zi = zi+ mu;
mu = zi - mu;
}
System.out.println(sum);
}
}

3、输入年月日算出是当年的第几天

import javax.swing.JOptionPane;


public class bb {


public static void main(String[] args) {


String a = JOptionPane.showInputDialog(null, "请输入年份");
String b = JOptionPane.showInputDialog(null, "请输入月份");
String c = JOptionPane.showInputDialog(null, "请输入天数");


int year = Integer.parseInt(a);
int month = Integer.parseInt(b);
int day = Integer.parseInt(c);
int allday = 0;


// switch (month) {
// 复杂方式
// case 1:
// allday = day;
// break;
// case 2:
// allday = 31 + day;
// break;
// case 3:
// allday = 31 + 28 + day;
// break;
// case 4:
// allday = 31 + 28 + 31 + day;
// break;
// case 5:
// allday = 31 + 28 + 31 + 30 + day;
// break;
// case 6:
// allday = 31 + 28 + 31 + 30 + 31 + day;
// break;
// case 7:
// allday = 31 + 28 + 31 + 30 + 31 + 30 + day;
// break;
// case 8:
// allday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
// break;
// case 9:
// allday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
// break;
// case 10:
// allday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
// break;
// case 11:
// allday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
// break;
// case 12:
// allday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
// break;


// 利用switch的不加break来做,会重复进行计算;
switch (month) {
case 12:
allday += 30;
case 11:
allday += 31;
case 10:
allday += 30;
case 9:
allday += 31;
case 8:
allday += 31;
case 7:
allday += 30;
case 6:
allday += 31;
case 5:
allday += 30;
case 4:
allday += 31;
case 3:
allday += 28;
case 2:
allday += 31;
case 1:
allday = day;


default:
JOptionPane.showInputDialog(null, "请输入1-12的月份");
break;
}
if (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
&& month > 2) {
allday += 1;
}




}

4、建立新的方法,定义新方法



public class Student {
//包括了 姓名  年龄  性别 以及引用的学习语   

 public String name;
    public int age;
    public  boolean gender;
    public void study(){
    System.out.println("好好学习");
    }
}


新的main方法里面引用

public class DoStudent {


public static void main(String[] args) {
Student wawa = new Student();
Student stu = new Student();
stu.age = 18;
stu.name = "哈哈";
wawa.name = "姑姑";
wawa.age = 19;
System.out.print(stu.name + "说我今年" + stu.age + "岁,我的座右铭是");
stu.study();
System.out.print(wawa.name + "  " + wawa.age);
stu.study();
}


}


0 0
原创粉丝点击