小结:
学了这么多天的java,终于还是学会了写一些简单的小程序:
1.回文数,例:12321(123321)
package random;
import java.util.Scanner;
public class HomeWork1 {
public static void main(String[] args) {
//判断回文数12321
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数:");
int num = sc.nextInt();
int num1 = num;
int sum = 0;
int wei = 1;
while(num/10!=0){
wei++;
num /= 10;
}
int a[] = new int[wei];
for(int i = 0;i<a.length;i++){
a[i] = num1 % 10;
num1 /= 10;
}
for(int i = 0;i<a.length;i++){
if(a[i]==a[a.length-1-i]){
sum++;
}
}
if(sum==a.length){
System.out.println("这是一个回文数");
}
sc.close();
}
}
2.练习随机数
例ran.nextInt(20)+10
相当于0~19都加10得10~29的数
随机产生50——111的数
-11——32的数
package random;
import java.util.Random;
public class HomeWork2 {
public static void main(String[] args) {
//随机数输出
Random ran = new Random();
int num1 = 0;
for(int i = 1;i<=10;i++){
num1 = ran.nextInt(20)+10;
System.out.print(num1+" ");
}
int num2 = 0;
System.out.println();
for(int i = 1;i<=10;i++){
num2 = ran.nextInt(62)+50;
System.out.print(num2+" ");
}
int num3 = 0;
System.out.println();
for(int i = 1;i<=10;i++){
num3 = ran.nextInt(44)-11;
System.out.print(num3+" ");
}
}
}
3.定义一个int型的一维数组,包含10个元素,分别赋一些随机整数,然后求出所有元素的最大值,
最小值,平均值,和,并输出出来
package random;
import java.util.Random;
public class HomeWork3 {
public static void main(String[] args) {
//求随机输入的十个数字的最大值,最小值,和,以及平均数
Random ran = new Random();
int a[] = new int[10];
int num = 0;
int sum = 0;
double avr = 0.0;
for(int i = 0;i<a.length;i++){
num = ran.nextInt(10)+1;
a[i] = num;
sum += a[i];
System.out.print(a[i]+" ");
}
avr = sum / 10;
System.out.println();
System.out.println("十个数字的和为:"+sum);
System.out.println("十个数字的平均值为:"+avr);
int min = a[0];
int max = a[0];
for(int i = 0;i<a.length;i++){
if(a[i]<min){
min = a[i];
}
}
System.out.println("十个数字中最小的为:"+min);
for(int i = 0;i<a.length;i++){
if(a[i]>max){
max = a[i];
}
}
System.out.println("十个数字中最大的为:"+max);
}
}
4.定义一个int型的一维数组,包含10个元素,分别赋值为1—10, 然后将数组中的元素都向前移一个位置,
即,a[0]=a[1],a[1]=a[2],…最后一个元素的值是原来第一个元素的值,然后输出这个数组
package random;
import java.util.Random;
public class HomeWork4 {
public static void main(String[] args) {
//随机输入十个数,将a[0]=a[1],依次输出
Random ran = new Random();
int a[] = new int[10];
int num = 0;
for(int i = 0;i<a.length;i++){
num = ran.nextInt(10)+1;
a[i] = num;
System.out.print(a[i]+" ");
}
System.out.println();
int temp = a[0];
for(int i = 0;i<a.length-1;i++){
a[i] = a[i+1];
System.out.print(a[i]+" ");
}
a[9] = temp;
System.out.print(a[9]);
}
}
5.打印出101—200(包括101与200)之间有多少个素数
package second;
public class HomeWork1 {
public static void main(String[] args) {
//101-200之间素数的个数
int i = 2;
int num = 101;
int j = 0;
for(num=101;num<=200;num++){
for(i=2;i<=num;i++){
if(num%i==0)
{
break;
}
}
if(i==num)
{
j++;
}
}
System.out.println(j);
}
}
6.斐波那契数列
1 1 2 3 5 8 13 21...按顺序求出第20个数
package second;
public class HomeWork2 {
public static void main(String[] args) {
//斐波那契数列
int a = 0;
int num = 1;
int b = 1;
int c = 0;
for(num=1;num<=20;num++){
c = a + b;
b = a;
a = c;
System.out.println(a);
}
}
}
7.使用辗转相除法求出两个数字的最大公约数
package second;
import java.util.Scanner;
public class HomeWork3 {
public static void main(String[] args) {
//随意输出两个数,求出两个数的最大公约数
Scanner sc = new Scanner(System.in);
int yushu = 0;
int num1 = 0,num2 = 0;
for(;;){
System.out.println("请输入第一个数:");
num1 = sc.nextInt();
System.out.println("请输入第二个数:");
num2 = sc.nextInt();
if(num1 == 0||num2 == 0){
continue;
}
else{
break;
}
}
while(num2!=0)
{
yushu = num1 % num2;
num1 = num2;
num2 = yushu;
}
System.out.println(num1);
sc.close();
}
}
8.随意输入两个数字,并求出最小公倍数
package second;
import java.util.Scanner;
public class HomeWork4 {
public static void main(String[] args) {
//随意输出两个数,求出两个数的最小公倍数
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int num1 = sc.nextInt();
System.out.println("请输入第一个数:");
int num2 = sc.nextInt();
int num = 1;
for(num=1;num<=num1*num2;num++){
if(num%num1==0&num%num2==0){
System.out.println("他们的最小公倍数为:"+num);
break;
}
}
sc.close();
}
}
9.算出1-10000
如果一个数恰好等于它的因子之和,则称该数为“完全数”
例:6,它有约数1、2、3、6,除去它本身6外,其余3个数相加,
1+2+3=6。
package second;
public class HomeWork5 {
public static void main(String[] args) {
//输出1-10000之间的完数
int num = 0;
int num1 = 0;
int sum = 0;
for(num=1;num<=10000;num++){
sum = 0;
for(num1=1;num1<num;num1++){
if(num%num1==0){
sum +=num1;
}
}
if(sum==num){
System.out.println(num);
}
}
}
}
10.输入随机一个日期判断是该年哪一天(输入年、月、日三个数字)
public static void main(String[] args) {
System.out.println("请输入是哪一年");
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
System.out.println("请输入是哪一月");
int month = sc.nextInt();
System.out.println("请输入是哪一天");
int day = sc.nextInt();
switch (month) {
case 12:
day += 30;
case 11:
day += 31;
case 10:
day += 30;
case 9:
day += 31;
case 8:
day += 31;
case 7:
day += 30;
case 6:
day += 31;
case 5:
day += 30;
case 4:
day += 31;
case 3:
day += 28;
case 2:
day += 31;
case 1:
break;
default:
System.out.println("输入有误");
}
if ((month > 2 && year % 4 == 0 && year % 100 != 0)
|| (month > 2 && year % 400 == 0)) {
day += 1;
}
System.out.println("这一天是该年的第" + day + "天");
}
11.输出所有的水仙花数
水仙花数是指一个数3位数,其各各位数字立方和等于其本身
例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
package first;
public class HomeWork2 {
public static void main(String[] args) {
//输出三位数的水仙花数
int i = 0,j = 0, k = 0,l = 0;
int num = 100;
for(num = 100;num<=999;num++){
i = num % 10;
j = num % 100 / 10;
k = num / 100;
l = i*i*i + j*j*j + k*k*k;
if(l==num)
{
System.out.println(num);
}
}
}
}
12.计算圆周率(建议循环10W次以上,并注意循环的变量的数据类型)
PI=4-4/3+4/5-4/7+4/9.......
package first;
public class HomeWork3 {
public static void main(String[] args) {
//求PI
double i = 1.0;
double sum = 0.0;
int num = 1;
for(num=1;num<100000;num++){
if(num%2==0){
sum-=4/i;
}
else{
sum+=4/i;
}
i += 2;
}
System.out.println(sum);
}
}
13.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
package first;
public class HomeWork4 {
public static void main(String[] args) {
double i = 1.0;
double j = 2.0;
double sum = 0;
double k = 0.0;
int num = 1;
for(num=1;num<=20;num++){
sum+=j/i;
k=j;
j=i+j;
i=k;
}
System.out.println(sum);
}
}
14.菱形
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
//判断行数,打印相应行数的正三角
for(int i = 0;i<num;i++){
//输出星星前的空格
for(int b = 1;b<num-i;b++){
System.out.print(" ");
}
//输入星星
for(int c = 0;c<2*i+1;c++){
System.out.print("*");
}
System.out.println();
}
//倒三角
//行数
for(int i=num-1;i>0;i--){
//空格
for(int b = 1;b<num-i+1;b++){
System.out.print(" ");
}
//星星
for(int c = 0;c<2*i-1;c++){
System.out.print("*");
}
System.out.println();
}
sc.close();
}
15.猴子吃桃
public static void main(String[] args) {
int num = 1;
for (int i = 1; i < 10; i++) {
num = (num + 1) * 2;
}
System.out.println("猴子第一天一共摘了"+num+"个桃");
}
16.九九乘法表
package first;
public class Test1 {
public static void main(String[] args) {
int i = 1;
int j = 1;
while(i<=9)
{
while(j<=i)
{
System.out.print(j+"*"+i+"="+j*i+"\t");
j++;
}
i++;
System.out.println();
}
}
}
17.1-2+3-4+5-......-100求和
package first;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int i = 1;
int sum = 0;
for(i=1;i<=100;i++)
{
if(i%2==0)
{
sum-=i;
}
else{
sum+=i;
}
}
System.out.println(sum);
}
}
18.100以内奇数和
package first;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int i = 1;
int sum = 0;
for(i=1;i<=100;i++)
{
if(i==1|i%3==0)
{
sum+=i;
}
}
System.out.println(sum);
}
}
19.2+1+2/3+1/2+2/5+......+1/50求和
public static void main(String[] args) {
double num = 2;
double sum = 0;
for(int i = 1;i<=100;i++){
sum+=num/i;
}
System.out.println(sum);
}