#一些简单的java程序练习
1.求1+2!+3!+…+20!的和
public class Factorial {
static int sum = 1;
static int result=0;
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
result += Factor(i);
sum=1;
}
System.out.println(result);
}
static int Factor(int a) {
int b = a;
sum *= b;
b--;
if (b > 0) {
Factor(b);
}
return sum;
}
}
2.求101-200之内的素数
public class Sushu {
public static void main(String[] args) {
boolean b;
for (int i = 101; i < 200; i += 2) {
b = false;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
break;
}
else if (j > (i / 2)) {
b = true;
break;
}
}
if (b == true) {
System.out.println(i);
}
}
}
}
3.打印菱形
public class Diamond {
public static void main(String[] args) {
int num=7;
for (int i = 0; i < num; ++i) {
int flag=Math.abs((num/2)-i);
for (int j=0;j<flag;++j) {
System.out.print(" ");
}
for (int j = 0; j < 2*(num/2-Math.abs(i-num/2))+1; ++j) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
4.打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方
public class Narcissus {
public static void main(String[] args) {
int thousand;
int hundred;
int bit;
int sum;
for(int i=100;i<1000;++i){
thousand=i/100;
hundred=(i/10)%10;
bit=i%10;
sum=(int)(Math.pow(thousand,3)+Math.pow(hundred,3)+Math.pow(bit,3));
if(sum==i){
System.out.println(i+"是水仙数");
}
}
}
}
5.利用递归方法求5!。
public class Factorial {
static int sum = 1;
public static void main(String[] args) {
int a=5;
System.out.println(Factor(a));
}
static int Factor(int a) {
int b = a;
sum *= b;
b--;
if (b > 0) {
Factor(b);
}
return sum;
}
}
6.古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?12个月
public class Rabbits {
public static void main(String[] args) {
int sum=2;//兔子的总数
int[] flag=new int[12];//可以生殖兔子数量
for(int i=1;i<=12;++i){
if(i<3){
flag[i-1]=0;
System.out.println("第"+i+"月兔子数量为:"+sum);
continue;
}
else if(i==3){
flag[i-1]=2;
sum+=flag[i-1];
System.out.println("第"+i+"月兔子数量为:"+sum);
continue;
}
else{
flag[i-1]=flag[i-3]+flag[i-2];
sum+=flag[i-1];
System.out.println("第"+i+"月兔子数量为:"+sum);
}
}
}
}
7.有1、2、3、4四个数字,能组成多少个互不相同且无重复的三位数。
public class Different {
public static void main(String[] args) {
int sum=0;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
if (j == 1) {
continue;
}
for (int l = 1; l <= 4; l++) {
if (l == i || l == j) {
continue;
}
sum++;
}
}
}
System.out.println(sum);
}
}
8.输入三个整数x,y,z,请把这三个数由小到大输出。
public class Intsort {
public static void main(String[] args) {
int[] num = new int[3];
Scanner scan=new Scanner(System.in);
System.out.print("x=");
num[0]=scan.nextInt();
System.out.print("y=");
num[1]=scan.nextInt();
System.out.print("z=");
num[2]=scan.nextInt();
sort(num);
for(int x:num){
System.out.println(x);
}
}
}
9.利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
public class Scorechoose {
public static void main(String[] args) {
int score=80;
if (score >= 90) {
System.out.println("A");
}else if(score>=60&&score<90){
System.out.println("B");
}else{
System.out.println("C");
}
}
}
10.猴子分桃:海 滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
public class Monkeypeach {
public static void main(String[] args) {
int peach = 0;
boolean flag = true;
for (int i = 1; flag; ++i) {
if (i % 5 == 1) {
peach = ((i - 1) / 5) * 4;
if (peach % 5 == 1) {
peach = ((peach - 1) / 5) * 4;
if (peach % 5 == 1) {
peach = ((peach - 1) / 5) * 4;
if (peach % 5 == 1) {
peach = ((peach - 1) / 5) * 4;
if (peach % 5 == 1) {
peach = ((peach - 1) / 5) * 4;
System.out.println(i);
flag=false;
}
}
}
}
}
}
}
}
综合练习1
掷摋子游戏博弈游戏:三个骰子,约定三个骰子的点数
最大点数:18
最小点数:3
1、如果是 大于 10 小于等于18 表示大 、
2、否则是小
3、初始值钱数为5000钱
4、每次只能下注是50的倍数但是最大不能超过1000
//5、最后一次下注必须要大于等于50
import java.util.Scanner;
public class Dicegame {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String choose;//选择字符串
String bigSmall;
int bet = 0;//赌注
int czMoney=0;
Player A = new Player(5000);
Dice B = new Dice();
all:while (true) {
System.out.println("是否开始游戏(是/否):");
choose = scan.next();
if (choose.equals("是")) {
B.setPoint((int) (Math.random() * 16) + 3);//生成骰子数
System.out.println("已经生成骰子数!");
while (true) {
System.out.println("请投注(只能下注是50的倍数但是最大不能超过1000)");
bet = scan.nextInt();
if (bet % 50 == 0 && bet <= 1000 && bet >= 50&&A.getMoney()>50) {
break;
} else if(A.getMoney()<50){
System.out.println("余额不足,请选择:");
System.out.println("1、支付宝充值"+"\n"+"2、退出游戏");
choose=scan.next();
if (choose.equals("支付宝充值")) {
System.out.println("跳转支付宝充值");
System.out.println("输入充值数:");
czMoney=scan.nextInt();
A.setMoney(A.getMoney()+czMoney);
}else if(choose.equals("退出游戏")){
System.out.println("成功退出游戏");
break all;
}else {
System.out.println("无效命令");
}
}else{
System.out.println("投注失败");
}
}
System.out.println("选择大小(大/小):");
choose = scan.next();
if (B.getPoint() > 10 && B.getPoint() <= 18) {
bigSmall = "大";
} else {
bigSmall = "小";
}
if (choose.equals(bigSmall)) {
System.out.println("骰子点数为:"+B.getPoint());
System.out.println("恭喜获胜!");
A.setMoney(A.getMoney() + bet);
System.out.println("余额:" + A.getMoney());
} else {
System.out.println("骰子点数为:"+B.getPoint());
System.out.println("落败!");
A.setMoney(A.getMoney() - bet);
System.out.println("余额:" + A.getMoney());
}
} else if (choose.equals("否")) {
System.out.println("成功退出游戏");
break;
} else {
System.out.println("无效命令");
}
}
}
}
class Player {
private int money;
Player(int money) {
this.money = money;
}
public int getMoney() {
return this.money;
}
public void setMoney(int money) {
this.money = money;
}
}
class Dice {
private int point;
Dice() {
}
public int getPoint() {
return this.point;
}
public void setPoint(int point) {
this.point = point;
}
}
综合练习2
万年历
a:先输出提示语句,并接受用户输入的年、月。
b:根据用户输入的年,先判断是否是闰年。
C:根据用户输入的月来判断月的天数。
D:用循环计算用户输入的年份距1900年1月1日的总天数。
E:用循环计算用户输入的月份距输入的年份的1月1日共有多少天。
F:相加D与E的天数,得到总天数。
G:用总天数来计算输入月的第一天的星期数。
H:根据G的值,格式化输出这个月的日历!
import java.util.Scanner;
public class Tenthousand {
static int[] day1 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int[] day2 = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static String[] week = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scan.nextInt();
System.out.print("请输入月份:");
int month = scan.nextInt();
int day = 1;
int sum = count(year, month, day);
print(year, month, week(sum));
}
static boolean checkRN(int year) {
boolean flag = false;
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
flag = true;
}
return flag;
}
static int returnMonth(int year, int month) {
boolean flag = checkRN(year);
int day;
if (flag) {
day = day2[month - 1];
} else {
day = day1[month - 1];
}
return day;
}
static int count(int year, int month, int day) {
int count = 0;
for (int i = 1900; i < year; ++i) {
if (checkRN(i)) {
for (int x : day2) {
count += x;
}
} else {
for (int x : day1) {
count += x;
}
}
}
for (int i = 1; i < month; i++) {
if (checkRN(year)) {
for (int x : day2) {
count += x;
}
} else {
for (int x : day1) {
count += x;
}
}
}
count += day;
return count;
}
static String week(int a) {
if (a%7!=0) {
return week[a % 7 - 1];
}else{
return week[6];
}
}
static void print(int year, int month, String s) {
for (String a : week) {
System.out.print(a + "\t");
}
System.out.println();
int a = 0;
int b = 1;
for (int j = 1; !s.equals(week[j - 1]); j++) {
++a;
}
for (int i = 1; i < returnMonth(year, month)+a; ++i) {
if (i < a) {
System.out.print(" "+"\t\t");
} else {
System.out.print(b+"\t\t");
b++;
}
if (i % 7 == 0) {
System.out.println();
}
}
}
}