学习内容分享

来源:互联网 发布:手机淘宝怎么申请开店 编辑:程序博客网 时间:2024/06/03 22:45

一、求 两个数 30.30 和 40 的最大值?
public class Ex0031 {

public static void main(String[] args) {    double a = 30.03;    int b = 40;    double max = a;    if(max < b){        max = b;    }    System.out.println("最大值是 :" + max);}

}

二、判断公元1988年是否为闰年? (可被4整除(但不可被100整除)为闰年,但是正百的年数必须是可以被400整除的才是闰年)
public class Ex004 {

public static void main(String[] args) {    int year = 2006;    /**     * ((year % 4 == 0) && (year % 100 !=0) ) || (year % 400 == 0)     */    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {        System.out.println(year + "年 是闰年");    } else {        System.out.println(year + "年 不是闰年");    }}

}

三、检查用random()方法产生的一个字符,判断是否为英文大写字母、小写字母、数字或是其他符号,并输出相应信息。
public class Ex002 {

public static void main(String[] args) {    char ch;    ch = (char) (java.lang.Math.random() * 128);    System.out.println("Math.random()  :"+Math.random());    System.out.println("ch  :"+(int)ch);    if (ch < ' ')        System.out.println("是不可显示字符!");    else if (ch >= 'a' && ch <= 'z')        System.out.println(ch + "是小写字母!");    else if (ch >= 'A' && ch <= 'Z')        System.out.println(ch + "是大写字母!");    else if (ch >= '0' && ch <= '9')        System.out.println(ch + "是数字!");    else        System.out.println(ch + "是其他符号!");    int year = 1988;    if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {         System.out.println(year + "是闰年。");    } else {        System.out.println(year + "不是闰年。");    }}

}

四、输入百分成绩,输出成绩等级,成绩>=90 输出A, 80<=成绩<90输出B, 70<=成绩<80输出C , 成绩<70输出D。

public class Ex008 {

public static void main(String[] args) {    Scanner scanner = new Scanner(System.in);  //实例一个类Scanner    int score = scanner.nextInt(); // 获取键盘输入成绩    char grade; // 等级    /**     * 95/10=9 98/10=9 90/10=9 88/10=8 87/10=8 76/10=7     */    int core = score / 10;  //8    System.out.println("core :" + core);    switch (core) {    case 10:    case 9:        grade = 'A';        System.out.println("a");        break;    case 8:        grade = 'B';        System.out.println("b");        break;    case 7:        grade = 'C';        System.out.println("c");        break;    default:        grade = 'D';        System.out.println("d");    }    System.out.println(score + "分 学生等级是 :"+grade);   /**    * 根据switch(表达式)值 去匹配case值,如果找到则执行,如果没有匹配值则执行default 语句    * 执行匹配到case语句后,如果有break语句则跳出switch语句,如果没有break语句则顺序向下执行,下一个case语句     */}

}

一、求 1 + 2 + 3 + 4 + 5 + ……+ 99 + 100 的和;

public class Ex004 {

public static void main(String[] args) {    int sum = 0;    int i = 0;    while (i < 100) {        ++i;        sum = sum + i;        System.out.println("i :" + i);    }

}

求 1-1/2+1/3-1/4 + ….+1/99-1/100 的和

public static void sumTwo() {    int sum = 0; // 总和    int item = 0; // 某一项值    int q = -1;    for (int i = 1; i <= 100; i++) {        q = -q;        item = q / (item + 1);        sum = sum + item;    }    System.out.println("1-1/2 +1/3-1/4 + ....+1/99-1/100的和是 :" + sum);}

六、输出1~100内可以被3整除的数前5个数。

public class Ex007 {

public static void main(String[] args) {    int count = 0;    int i = 0;    while (i < 100) {        ++i;        if (i % 3 != 0) {  //检查 i 是否能被3整除,不能被3整除则结束本次循环。            continue;        }        System.out.println("i :" + i);        ++count;   //统计能被3整除的数的个数        if(count == 5){            break;        }    }}

}

一、【例一】百元买百鸡:用一百元钱买一百只鸡。已知公鸡5元/只,母鸡3元/只,小鸡1元/3只。
public static void main(String[] args) {
for (int x = 0; x <= 100; x++) {
for (int y = 0; y <= 100; y++) {
for (int z = 0; z <= 100; z++) {
if ((x + y + z == 100) && (5 * x + 3 * y + z / 3.0 == 100)
&& (z % 3 == 0)) {

                    System.out.println("公鸡 :" + x + "只," + " 母鸡  :" + y                            + " 只," + " 小鸡 :" + z);                }            }        }    }    /*for (int x = 0; x <= 100; x++) {        for (int y = 0; y <= 100; y++) {            int z = 100 - x - y;            if (5 * x + 3 * y + z / 3.0 == 100) {                System.out.println("公鸡 :" + x + "只," + " 母鸡  :" + y + " 只,"                        + " 小鸡 :" + z);            }        }    }*/}

二、例:斐波纳契数列。前两个数都是1,第三个数是前两个数之和,以后的每个数都是其前两个数之和。

public static void fib1(){    int first = 1; // 前一个值    int second = 1; // 后一个值    int sum = first + second; // 当前值 = 前一个 + 后一个    int i = 2;    int n = 10;    while (i <= n) {        first = second;        second = sum;        sum = first + second;        ++i;        System.out.println(sum);    }}
1 0
原创粉丝点击