第一天:
求完数:
编程求出1000 以内的所有完数。
public class JavaBasic08 {
public static void main(String[] args) {
int sum = 0;
for (int i = 2;i < 1000;i +=2) { //最小的完数是2,并且完数都是偶数
for (int j = 1; j < i; j++)
if (i % j == 0)
sum += j;
if (i == sum)
System.out.print(i + " ");
sum = 0;
}
}
}
求不重复数字:
public class JavaBasic09 {
public static void main(String[] args) {
int count = 0 ;
for (int Dig_1 = 1;Dig_1 <= 4;Dig_1 ++)
for (int Dig_2 = 1;Dig_2 <= 4;Dig_2 ++)
for (int Dig_3 = 1;Dig_3 <= 4;Dig_3 ++)
if(Dig_1 != Dig_2 && Dig_1 != Dig_3 && Dig_3 != Dig_2) {
count ++;
System.out.print(Dig_3 + "" + Dig_2 + "" +Dig_1 + " ");
}
System.out.print("\n一共可以组成 " + count + " 个符合条件的三位数。");
}
}
第二天:
累加:
public class JavaBasic12 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("求前 n 个数的和,输入 n 的值(n > 0):");
int n = input.nextInt();
int sum = 0;
for (;n > 0;n --)
sum += n;
System.out.print("和为 " + sum);
input.close();
}
}
累乘:
public class JavaBasic13 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("求前 n 个数积,输入 n 的值(n > 0):");
int n = input.nextInt();
int accumulate = 1;
for (;n > 0;n --)
accumulate *= n;
System.out.print("积为 " + accumulate);
input.close();
}
}
第三天:
求最大值:
public class JavaBasic14 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请分别输入这三个数:");
int num1 = input.nextInt(),num2 = input.nextInt(),num3 = input.nextInt();
//if嵌套方式
if(num1 > num2)
if(num1 > num3)
System.out.println("max = " + num1);
else
System.out.println("max = " + num2);
else
if(num2 > num3)
System.out.println("max = " + num2);
else
System.out.println("max = " + num3);
//if非嵌套方式
int temp;
if(num1 > num2)
temp = num1;
else
temp = num2;
if(temp > num3)
System.out.println("max = " + temp);
else
System.out.println("max = " + num3);
int result = num1 > num2 ?( num1 > num3 ? num1 :num3 ) : (num2 > num3 ? num2 : num3);
System.out.println("max = " + result);
input.close();
}
}
第四天:
数列的前n项和:
public class JavaBasic11 {
public static void main(String[] args) {
int numerator = 2 , denominator = 1; //第一个数字是2/1
double result = 0;
System.out.print(numerator + "/" + denominator);
for (int num = 1;num <= 20; num ++) { //计算前20项和--循环20次
result += (double) numerator / denominator;
numerator += denominator;
denominator = numerator - denominator;
System.out.print(" + " + numerator + "/" + denominator); //分子:numerator,分母:denominator
}
System.out.println("\n结果是:" + result);
input.close();
}
}
第五天:
Swiitch练习 求某月天数:
public class JavaBasic29 {
public static void main(String[] args) {
//闰年是指能被4整除,但是不能被100整除的年份 或者 能被400整除的年份
//平年2月有28天,闰年2月有29天
Scanner input = new Scanner(System.in);
System.out.print("请输入年份和月份:");
int year = input.nextInt();
int month = input.nextInt();
int day = 28;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
day = 29;
break;
case 4:
case 6:
case 9:
case 11:
break;
}
System.out.print("在 " + year + " 年 " + month + " 月一共有 " + day + " 天。");
input.close();
}
}