java基础知识

1.(标识符命名)下面几个变量中,那些是对的?那些是错的?错的请说明理由
A. ILoveJava B. $20 C. learn@java D. antony.lee E. Hello_World F. 2tigers
正确的:A B E
错误的:
C(@符号不满足) D ( . 符号不满足) F(不能数字开头)
1>标识符由大小写字母, 下划线, 数字, .2>,线, 符 号 组 成 . 2 > 开 头 可 以 是 大 小 写 字 母 , 下 划 线 , 和 符号.(数字不能开头)
3>标识符长度没有限制
4>标识符不能是关键子和保留字
标识符的命名最好能反映出其作用 ,java语言对字母的大小写有严格的要求.所有自定义标识符需全部遵循标识符的命名规范.

变量:

1>如果是单个单词, 单词全部字母小写. 如:intcount;
2>如果是由多个单词组成的复合单词, 除第一个单词外, 其后所有单词首字母大写. 如: codeName;
常量 :
常量所有单词字母大写, 如果是由多个单词组成, 由下划线连接.如:String PERSON_NAME;
方法:
方法命名规范与变量相似, 如 count(); getSum();
类:
类名的所有单词首字母均大写. 如Person{} , DataCenter{};
包:

用小写的倒置域名来命名. 格式: 前缀 + 项目名 + 模块名 + 层如: cn.gkate.domain.mangerweb

2.(Java 程序的编译与运行)假设有如下程序:

package com.corejava.chp1;
public class HelloWorld {    
public static void main(String args[]){     
    }
 }

问:
1)假设这个代码存在 hello.java 文件中,那这个程序能够编译通过?为什 么?如果编译不通过,应该如何改进? 2) 假设这个.java 文件放在 C:\javafile\目录下, CLASSPATH=., 则生的.class文 件应该放在什么目录下?如何运行?

1)不能够编译通过,文件名不和public修饰的主类名一致,java文件名和公共类名必须一致
修改后的代码:

package com.corejava.chp1;
public class Hello {
public static void main(String args[]){     
    }
 }

2)生成的.class文件在com/corejava/chp1文件夹中,
java -cp . com.corejava.chp1.Hello(cp代表目录)
可以使用javac -d 路径 java文件(.java) 可以快速建立包

4.(操作符)有如下代码:int a = 5;int b = (a++) + (–a) +(++a);问执行完之后,b 的结果是多少?
(5)+(5)+(6)=16
5.(基本类型的运算)一家商场在举行打折促销,所有商品都进行8 折优惠。一位程序员把这个逻辑写成:
short price = …; // 先计算出原价
short realPrice = price * 8 / 10; //再计算出打折之后的价格
问:这段代码是否正确?如果正确,假设price 为100,那计算之后的
realPrice值为多少?如果不正确,应该怎么改正?
不正确,因为整数计算打折有可能丢失精度;
改正:货币金额的计算用BigDecimal(这里可以用double),并double realPrice=price*0.8;
注:float和double只能用来做科学计算或者是工程计算,在商业计算中我们要用 java.math.BigDecimal,而且使用BigDecimal类也可以进行大数的操作
6.(操作符)请说明>>与>>>之间的区别。

右移运算符:
如果该数为正,则高位补0,若该数为负,则高位补1

无符号右移运算符:
也称逻辑右移,即若该数不论正负,高位都补0;
补充:
<<是与>>对应的左移运算符,低位补0,;其实向左移动n位,就相当于乘以2的n次
7.a = (a>b)?a:b;请问这段代码完成了什么功能?
求最大值

8.(if 语句)读入一个整数,表示一个人的年龄。如果小于6 岁,则输出“童”,6 岁到13 岁,输出“少儿”;14 岁到18 岁,输出“青少年”;18 岁到35 岁,输出“青年”;35 岁到50 岁,输出“中年”;50 岁以上输出“中老年”。

 package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int x=new Scanner(System.in).nextInt();
if(x<1&&x>200){
    System.out.println("输入年龄范围有误!");
}else if(x<=6){
    System.out.println("童");
}else if(x<=12){
    System.out.println("少儿");
} else if(x<=18){
    System.out.println("青少年");
}else if(x<=36){
    System.out.println("青年");
}else if(x<=50){
    System.out.println("中年");
}else{
    System.out.println("中老年");
}
}   
}

9.(switch 语句)读入一个整数,如果是1~5 之间,则分别输出5 个福娃的名字,否则输出“北京欢迎你”

 package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int x = new Scanner(System.in).nextInt();
switch (x) {
case 1: {
    System.out.println("贝贝");
    break;
}
case 2: {
    System.out.println("晶晶");
    break;
}
case 3: {
    System.out.println("欢欢");
    break;
}
case 4: {
    System.out.println("迎迎");
    break;
}
case 5: {
    System.out.println("妮妮");
    break;
}
default: {
    System.out.println("北京欢迎您!");
}
}
}
  1. (if 语句,赋值操作)*读入三个整数,输出这三个整数中最大的一个。

    package com.tedu.study._day01;

    import java.util.Scanner;

    public class Demo01 {
    public static void main(String[] args) {
    System.out.println(“请输入第一个整数:”);
    int a1=new Scanner(System.in).nextInt();
    System.out.println(“请输入第二个整数:”);
    int a2=new Scanner(System.in).nextInt();
    System.out.println(“请输入第三个整数:”);
    int a3=new Scanner(System.in).nextInt();
    int max=a1;
    if((max>a2)&&(max>a3)){
    max=max;
    }else{
    if(a2>a3){
    max=a2;
    }else{
    max=a3;
    }
    }
    //注意扩展:最大值有多个时,且把key-value记住时需要=也加入
    System.out.println(“最大值:”+max);
    }

    }

  2. (if 语句)*读入一个表示年份的整数,判断这一年是否是闰年。如何判断一个年份是否是闰年:1. 如果这个年份能够被4 整除,且不能被100 整除,则这一年是闰年。例如,1996 年是闰年,而相应的,1993 年就不是闰年。2. 如果这个年份能够被100 整除,则这个数必须要能被400 整除,才是闰年。例如,2000 年是闰年,1900 年不是闰年。

    package com.tedu.study._day01;

    import java.util.Scanner;

    public class Demo01 {
    public static void main(String[] args) {
    int x = new Scanner(System.in).nextInt();
    if ((x % 400 == 0) || (x % 4 == 0 && x % 100 != 0)) {
    System.out.println(x + “是闰年”);
    } else {
    System.out.println(x + “是平年”);
    }
    }
    }

  3. (switch 语句)*完成一个简单的计算器程序。程序要求如下:1. 读入两个整数2. 提示用户选择对这两个整数的操作,即输出1 : +2 : -3 : *4 : /请输入您的选择:读入用户的选择,输出运算结果。

    package com.tedu.study._day01;

    import java.util.Scanner;

    public class Demo01 {
    public static void main(String[] args) {
    System.out.println(“请输入第一个整数:”);
    int x1=new Scanner(System.in).nextInt();
    System.out.println(“请输入第二个整数:”);
    int x2=new Scanner(System.in).nextInt();
    System.out.println(“请选择操作+-*/:”);
    String op=new Scanner(System.in).nextLine();
    switch(op){
    case “+” :{
    System.out.println(x1+x2);
    break;
    }
    case “-” :{
    System.out.println(x1-x2);
    break;
    }
    case “*” :{
    System.out.println(x1*x2);
    break;
    }
    case “/” :{
    System.out.println(x1/x2);
    break;
    }
    default :{
    System.out.println(“输入操作有误!”);
    }
    }
    }
    }

  4. (if 语句)*托运计费问题:当货物重量小于20 公斤的时候,收费5 元,大于20 公斤小于100 公斤的时候超出20 公斤的部分按每0.2 元每公斤计费,如果超出100 公斤的时候,超的部分按照每公斤0.15 元计算。读入货物的重量,输出计算之后货物的运费。

    package com.tedu.study._day01;

    import java.util.Scanner;

    public class Demo01 {
    public static void main(String[] args) {
    double x=new Scanner(System.in).nextDouble();
    double sum=0;
    if(x<=20){
    sum=5;
    }else if(x<=100){
    sum=5+(x-20)*0.2;
    }else{
    sum=5+80*0.2+(x-100)*0.15;
    }
    System.out.println(sum);
    }
    }

  5. (if 语句)中国的个税计算方法:应税所得为税前收入扣除2000 元(起征点),然后超出部分,按照以下税率收税:500 5%500-2000 10%2000-5000 15%5000-20000 20%20000-40000 25%40000-60000 30%60000-80000 35%80000-100000 40%100000 - ? 45%例:若月收入15000,则应税所得为15000-2000=13000;总的个人所得税为(13000-5000)20% + (5000-2000)*15% + (2000-500)10% + 5005%= 2225要求:读入一个整数,表示税前收入,输出应当缴纳的个人所得税和税后实际收入。

    package com.tedu.study._day01;

    import java.util.Scanner;

    public class Demo01 {
    public static void main(String[] args) {
    double x=new Scanner(System.in).nextDouble();
    x-=2000;
    double div=0;
    if(x<=500){
    div=x*0.05;
    }else if(x<=2000){
    sum=500*0.05+x(x-500)*0.1;
    }else if(x<=5000){
    sum=500*0.05+(2000-500)*0.1+(x-5000)*0.15;
    }else if(x<=2000){
    sum=500*0.05+(2000-500)*0.1+(5000-2000)*0.15+(x-5000)*0.2;
    }else if(x<=40000){
    sum=500*0.05+(2000-500)*0.1+(5000-2000)*0.15+(20000-5000)*0.2+(x-20000)*0.25;
    }else if(x<=60000){
    sum=500*0.05+(2000-500)*0.1+(5000-2000)*0.15+(20000-5000)*0.2+(40000-20000)*0.25+(x-40000)*0.3;
    }else if(x<=80000){
    sum=500*0.05+(2000-500)*0.1+(5000-2000)*0.15+(20000-5000)*0.2+(40000-20000)*0.25+(60000-40000)*0.3+(x-60000)*0.35;
    }else if(x<=100000){
    sum=500*0.05+(2000-500)*0.1+(5000-2000)*0.15+(20000-5000)*0.2+(40000-20000)*0.25+(60000-40000)*0.3+(80000-60000)*0.35+(x-80000)*0.4;
    }else{
    sum=500*0.05+(2000-500)*0.1+(5000-2000)*0.15+(20000-5000)*0.2+(40000-20000)*0.25+(60000-40000)*0.3+(80000-60000)*0.35+(100000-80000)*0.4+(x-100000)*0.45;
    }
    System.out.println(“应缴纳”+num+”元,实际收入:”+(x+2000-sum)+”元!”);
    }
    }

  6. (if 语句,操作符)**读入一个三位数,计算其各位数字之和。例如:123各位数字之和为6

    package com.tedu.study._day01;

    import java.util.Scanner;

    public class Demo01 {
    public static void main(String[] args) {
    int x=new Scanner(System.in).nextInt();
    int sum=0;
    while(x!=0){
    sum+=(x%10);
    x=x/10;
    }
    System.out.println(sum);
    }
    }

  7. (if 语句)**读入三个整数,把这三个整数按照由大到小的顺序输

    package com.tedu.study._day01;

    import java.util.Scanner;

    public class Demo01 {
    public static void main(String[] args) {
    int max = 0, mid = 0, min = 0;
    System.out.println(“请输入第一个数:”);
    int a = new Scanner(System.in).nextInt();
    System.out.println(“请输入第二个数:”);
    int b = new Scanner(System.in).nextInt();
    System.out.println(“请输入第三个数:”);
    int c = new Scanner(System.in).nextInt();
    if (a > b && a > c) {
    max = a;
    if (b > c) {
    mid = b;
    min = c;
    } else {
    mid = c;
    min = b;
    }
    } else {
    if (b > a && b > c) {
    max = b;
    if (a > c) {
    mid = a;
    min = c;
    } else {
    mid = c;
    min = a;
    }
    } else {
    max = c;
    if (a > b) {
    mid = a;
    min = b;
    } else {
    mid = b;
    min = a;
    }
    }
    }
    }
    }

第三天作业
1.计算1+2+3+…+100的和

package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println(sum);
}
}

2.计算1+3+5+…+99的和

package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
sum += i;
}
}
System.out.println(sum);
}
}

3.用while和do-while重写第1题和第2题

//第一题while写法
package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int sum = 0;
int i = 100;
while (i > 0) {
sum += i;
i–;
}
System.out.println(sum);
}
}

//第二题do-while写法
package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int sum = 0;
int i = 100;
do{
sum += i;
i–;
}while(i>0);
System.out.println(sum);
}
}

//第二题while写法
package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int sum = 0;
int i = 100;
while (i > 0) {
if (i % 2 != 0) {
sum += i;
}
i–;
}
System.out.println(sum);
}
}

//第二题do-while写法
package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int sum = 0;
int i = 100;
do{
if (i % 2 != 0) {
sum += i;
}
i–;
}while(i>0);
System.out.println(sum);
}
}

4.(for)读入一个小于10的整数,并计算其阶乘

package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int x = new Scanner(System.in).nextInt();
int sum = 1;
for (int i = x; i > 1; i–) {
sum *= i;
}
System.out.println(sum);
}
}

//递归写法
package com.peng.demo;

public class Demo01 {
public static void main(String args[]) {
System.out.println(getJieCheng(5));
}

// 递归函数求阶乘
public static int getJieCheng(int x) {
    if (x == 1) {
        return 1;
    }
    return x * getJieCheng(x - 1);
}

}

5.(for)求100以内所有能被3整除,但不能被5整除的数字之和

package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int sum =0;
for (int i = 1; i <=100; i++) {
if(i%3==0&&i%5!=0){
sum+=i;
}
}
System.out.println(sum);
}
}

6.(for)“百钱买百鸡”:3文钱可以买一只公鸡;2文钱可以买一只母鸡;1文钱可以买3只小鸡;用100文买100只鸡,问公鸡、母鸡、小鸡各有多少只?

设公鸡a只,母鸡b只,小鸡c只:
则:a+b+c=100
3a+2b+c/3=100

100文最多可买公鸡(100/3=33),100文最多可买母鸡(100/2=50只),小鸡便宜,但最多只能99只(按100只算)即:
for(int x=0;x<=33;x++){
for(int y=0;y<=50;y++){
for(int z=0;z<=100){
if((x+y+z==100)&&(3*x+2*y+z/3==100)){
System.out.pri:nntln(“公鸡:”+x+”,母鸡:”+y+”,小鸡”+z);
}
}
}
}
//6种情况

7.搬砖问题:36块砖,36人搬砖,男搬4,女搬3,两个小孩抬一块砖;要求一次性全部把砖搬完,问:男人、女人、小孩各若干?

设男人a人,女人b人,小孩c人
则:a+b+c=36
4a+3b+c/2=36
男生最多搬(4*9=36),女生最多搬(3*12=36),小孩最多搬(36/2=18)
则:
for(int x=0;x<=36;x++ ){
for(int y=0;y<=36;y++){
for(int z=0;z<=18){
if((x+y+z==36)&&(x/4+y/3+z*2=36)){
System.out.print(“男人:”+(x/4)+”,女人:”+(y/3)+”,小孩:”+(2*z));
}
}
}
}

8.(for)编程找出四位整数abcd中满足下述关系的数:(ab+cd)(ab+cd)=abcd

package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {

    for (int i = 1000; i < 9999; i++) {
        if (i == getSS(i)) {
            System.out.println(getSS(i));
        }
    }

}

public static int getSS(int x) {
    int a1 = x % 10;
    x = x / 10;
    int a2 = x % 10;
    x = x / 10;
    int a3 = x % 10;
    x = x / 10;
    int a4 = x % 1000;
    return (((10 * a2 + a1) + (10 * a4 + a3)) * ((10 * a2 + a1) + (10 * a4 + a3)));
}

}

9.(循环)读入一个整数n, 输出如下图形
n=3时:
*



n=4时:
*




每一行星数:(2*行数-1)

每一行前空格数:(2*n-1-行数)/2
每一行后空格数:(2*n-1-行数)/2
//代码
package com.peng.demo;

public class Demo01 {
public static void main(String[] args) {
/*
* *
* *
* *
* *
*/
int n = 4;
// 遍历行
for (int i = 0; i < n; i++) {
// 遍历列
for (int j = 0; j < (2 * n - 1); j++) {
// 打印空格
if ((j < (n - (i + 1))) || (j > (n + i - 1))) {
System.out.print(” “);
} else {// 打印*
System.out.print(“*”);
}
}
// 换行
System.out.println();
}

}

}

10.(循环)输出99乘法表

package com.tedu.study._day01;

public class Demo01 {
public static void main(String[] args) {
for (int i = 1, j = 1; i <= 9; j++) {
System.out.print(i + “” + j + “=” + (i j) + “\t”);
if (i == j) {
j = 0;
i++;
System.out.println();
}
}
}

}

11.(循环)水仙花数:一个三位数abc,如果满足a3+b3+c3=abc,则abc是水仙数

package com.peng.demo;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {

    for (int i = 100; i <= 999; i++) {
        if (i == getSS(i)) {
            System.out.println(getSS(i));
        }
    }

}

public static int getSS(int x) {
    int a1 = x % 10;
    x = x / 10;
    int a2 = x % 10;
    x = x / 10;
    int a3 = x % 10;
    return (int)(Math.pow(a1,3)+Math.pow(a2,3)+Math.pow(a3,3));
}

}

  1. (循环)**输入一个整数,计算它各位上数字的和。(注意:是任意位的整数)

    package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int x = new Scanner(System.in).nextInt();
int num = 0;
while (x != 0) {
num += x % 10;
x /= 10;
}
System.out.println(num);
}
}

13.输入一个整数,判断是否是质数(提示2~Math.sqrt(x))
package com.tedu.study._day01;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
boolean isok = true;
int x = new Scanner(System.in).nextInt();
for (int i = 2; i < (int) Math.sqrt(x); i++) {
if (x % i == 0) {
System.out.println(x + “不是质数”);
isok = false;
break;
}
}
if (isok) {
System.out.println(x + “是质数”);
}
}
}

14.完数:如果一个数等于它所有因子之和,eg:6=1+2+3,求1000以内的完数(和亲密数相似)

package com.peng.demo;

public class Demo01 {
public static void main(String[] args) {
for (int i = 3; i <= 1000; i++) {
if (i == getXSum(i)) {
System.out.println(“完数:” + i);
}
}

}

public static int getXSum(int x) {
    int sum = 1;
    for (int i = 2; i <= x / 2; i++) {
        if (x % i == 0) {
            sum += i;
        }
    }

    return sum;
}

}

15.计算圆周率:PI=4/1-4/3+4/5-4/7+4/9-4/11+…,圆周率在3.1415926和3.1415927之间,求要得到这样的结果,经过多少次加减运算可以得到

package com.peng.demo;

public class Demo01 {
public static void main(String[] args) {
/*
* ###15.计算圆周率:PI=4/1-4/3+4/5-4/7+4/9-4/11+…,
* 圆周率在3.1415926和3.1415927之间,求要得到这样的结果,经过多少次加减运算可以得到
*/
double PI = 0;
int i = 1;
int num = 0;
while (PI < 3.1415926 || PI > 3.1415927) {
if ((num + 1) % 2 == 0) {
PI = PI - (4.0 / i);
} else {
PI = PI + (4.0 / i);
}
i += 2;
num++;
}
System.out.println(num);
}

}

16.faibonacci(斐波那契数列):0,1,1,2,3,5,8…输入n,求前n项

//递归(函数自己调用自己)
package com.peng.demo;

public class Demo01 {
public static void main(String[] args) {
// 前10项斐波那契数
for (int i = 1; i <= 10; i++) {
System.out.println(faibonacci(i));
}
}

// 0,1,1,2,3,5...
public static int faibonacci(int n) {
    if (n == 1) {
        return 0;
    }
    if (n == 2) {
        return 1;
    }
    return faibonacci(n - 1) + faibonacci(n - 2);
}

}

//简单方法:设置三个变量first,second,third

for(){
third=first+second;
first=second;
second=third;
}

17.(循环)一个int是由32的二进制位组成,每个二进制数要么0,要么1。要求读入一个int类型的数n,计算它的32个二进制中共有多少位1

//倒取模将10进制数转化为二进制数并在其中计算1的数量(正整数)
//注意负整数
//方法1.取反(01相转)eg:-5 -> 4; 4有一个1,则-5有32-1=31个1
//方法2.&1并且右移
eg:
1110 0001 1110 1010 0010 0010 0001 0101
&
0000 0000 0000 0000 0000 0000 0000 0001
=
0000 0000 0000 0000 0000 0000 0000 0001即1
然后右移继续判断

package com.peng.demo;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
int sum = 0;
int x = new Scanner(System.in).nextInt();
// 正数直接求,负数先取反,在求
if (x < 0) {
x = ~x;
for (int i = 0; i < get32Array(5).length; i++) {
sum += get32Array(5)[i];
}
System.out.println(“该数组1的个数为” + (32 - sum));
} else {
for (int i = 0; i < get32Array(5).length; i++) {
sum += get32Array(5)[i];
}
System.out.println(“该数组1的个数为” + sum);
}

}

public static int[] get32Array(int n) {
    int[] temp = new int[32];
    int i = 31;
    while (n != 0) {
        temp[i] = n % 2;
        n /= 2;
        i--;
    }
    return temp;
}

}

18.三天打鱼两天晒网:假设有一个人从2000,1,1开始,输入三个整数,分别代表年月日,问当前日期是在打鱼还是晒网

/*算出当日距2000,1,1的天数,取模5,如果为1,2,3位打鱼;4,5为晒网。
注意平年和闰年的2月*/
//代码
package com.peng.demo;

import javax.swing.JOptionPane;

public class Demo01 {
public static void main(String[] args) {
switch (1 + (getDays(2000, 1, 6) % 5)) {
// 1,2,3打鱼
case 1:
case 2:
case 3: {
System.out.println(“打鱼!”);
break;
}
// 4,5晒网
case 4:
case 5: {
System.out.println(“晒网!”);
break;
}
}
}

public static int getDays(int year, int month, int day) {
    // 一:数据校验:
    // 1、年份>=2000;
    // 2、1<=月份<=12;
    // 3、日子数(1,3,5,7,8,10,12月)1~31天、(4,6,9,11)1~30天、(2)平年1~28,闰年1~29
    if (year < 2000) {
        JOptionPane.showMessageDialog(null, "年份为2000以后!");
        return -1;
    }
    if (month < 1 || month > 12) {
        JOptionPane.showMessageDialog(null, "月份为1~12月!");
        return -1;
    }
    switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12: {
        if (day < 1 || day > 31) {
            JOptionPane.showMessageDialog(null, "此月为大月,范围为1~31");
            return -1;
        }
        break;
    }

    case 4:
    case 6:
    case 9:
    case 11: {
        if (day < 1 || day > 30) {
            JOptionPane.showMessageDialog(null, "此月为小月,范围为1~30");
            return -1;
        }
        break;
    }
    case 2: {
        if ((!isRunNian(year)) && (day < 1 || day > 28)) {// 平年
            JOptionPane.showMessageDialog(null, "此月为平年2月,范围为1~29");
            return -1;
        }
        if ((isRunNian(year)) && (day < 1 || day > 29)) {// 闰年
            JOptionPane.showMessageDialog(null, "此月为闰年2月,范围为1~29");
            return -1;
        }
        break;
    }
    }
    // 二:计算天数
    return getAllDays(year, month, day);
}

// 是否是闰年
public static boolean isRunNian(int year) {
    if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
        return true;
    } else {
        return false;
    }

}

// 计算所有天数
public static int getAllDays(int year, int month, int day) {
    int all_days = 0;
    int num_RunNian = 0;//
    int num_PingNian = 0;// 平年个数

    for (int i = 2000; i < year; i++) {
        if (isRunNian(year)) {
            num_RunNian++;// 计算闰年个数
        } else {
            num_PingNian++;
        }
    }

    all_days += (366 * num_RunNian + 365 * num_PingNian);
    // 计算当年的天数
    if (isRunNian(year)) {// 闰年
        switch (month) {
        case 1: {
            all_days += day;
            break;
        }
        case 2: {
            all_days += (31) + day;
            break;
        }
        case 3: {
            all_days += (31 + 29) + day;
            break;
        }
        case 4: {
            all_days += (31 + 29 + 31) + day;
            break;
        }
        case 5: {
            all_days += (31 + 29 + 31 + 30) + day;
            break;
        }
        case 6: {
            all_days += (31 + 29 + 31 + 30 + 31) + day;
            break;
        }
        case 7: {
            all_days += (31 + 29 + 31 + 30 + 31 + 30) + day;
            break;
        }
        case 8: {
            all_days += (31 + 29 + 31 + 30 + 31 + 30 + 31) + day;
            break;
        }
        case 9: {
            all_days += (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31) + day;
            break;
        }
        case 10: {
            all_days += (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30) + day;
            break;
        }
        case 11: {
            all_days += (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31)
                    + day;
            break;
        }
        case 12: {
            all_days += (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
                    + day;
            break;
        }
        }
    } else {
        switch (month) {
        case 1: {
            all_days += day;
            break;
        }
        case 2: {
            all_days += (31) + day;
            break;
        }
        case 3: {
            all_days += (31 + 28) + day;
            break;
        }
        case 4: {
            all_days += (31 + 28 + 31) + day;
            break;
        }
        case 5: {
            all_days += (31 + 28 + 31 + 30) + day;
            break;
        }
        case 6: {
            all_days += (31 + 28 + 31 + 30 + 31) + day;
            break;
        }
        case 7: {
            all_days += (31 + 28 + 31 + 30 + 31 + 30) + day;
            break;
        }
        case 8: {
            all_days += (31 + 28 + 31 + 30 + 31 + 30 + 31) + day;
            break;
        }
        case 9: {
            all_days += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31) + day;
            break;
        }
        case 10: {
            all_days += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30) + day;
            break;
        }
        case 11: {
            all_days += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31)
                    + day;
            break;
        }
        case 12: {
            all_days += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
                    + day;
            break;
        }
        }

    }
    return all_days - 1;
}

}

19.分解质因数并打印出来

package com.peng.demo;

public class Demo01 {
public static void main(String[] args) {
int n = 99;
System.out.println(“1”);
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
System.out.println(i);
}
}
}
}

第四天作业
练习1. 下列数组的声明有哪些是对的?错的应该怎么修改?
A. int[] a;
B. int a[] = new int[3];
C. int[] a;a = {1,2,3,4,5};
D. int[] a = new int[3]{1,2,3};
正确的:A、B
int[] a={1,2,3,4,5};
int[] a=new int[3];
//1.长度不同 2、不知道如何存、如何放
int[] a = new int[5]{1,2,3};

  1. 看下面的代码,写出输出的结果
    public class Ex2 {public static void main(String[] args) {
    int[] a = {1,2,3,4,5};
    expand(a);
    changeArray(a);
    printArray(a);
    }
    public static void expand(int[] a){
    int[] newArray = new int[a.length * 2];
    System.arraycopy(a, 0, newArray, 0, a.lena = newArray;
    }
    public static void changeArray(int[] a){
    a[0] = 10;
    }
    public static void printArray(int[] a){
    for(int i = 0; i

       for (int i = 0; i < max; i++) {
            array[n] = i;
            if (judge(n)) {
                check(n + 1);
            }
        }
    }

    private boolean judge(int n) {
        for (int i = 0; i < n; i++) {
            if (array[i] == array[n] || Math.abs(n - i) == Math.abs(array[n] - array[i])) {
                return false;
            }
        }
        return true;
    }

    private void print()  {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + 1 + " ");
        }
        System.out.println();
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值