Java例题
1.题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
解题思路:通过三个for循环,分别写出不同的三位数
public class Demo08 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
for (int h = 1; h < 5; h++) {
if (i != j && j != h && h != i) {
System.out.print(h * 100 + j * 10 + i + " ");
sum++;//计算个数
if (sum % 10 == 0) {//十个一行
System.out.println();
}
}
}
}
}
System.out.println("\n总共有:"+sum+"个这样的数");
}
}
运行结果:
321 421 231 431 241 341 312 412 132 432
142 342 213 413 123 423 143 243 214 314
124 324 134 234
总共有:24个这样的数
Process finished with exit code 0
2.企业发放的奖金根据利润提成。利润(I)低于或者等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,壳体呈5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当前利润I,求应发奖金总数?
public class test{
public static void main(String[]args){
System.out.println("请输入你创造的利润(单位:万元)");
Scanner sc=new Scanner(System.in);
while(!scanner.hacNextDouble()){
System.out.println("请输入金额数字:");
scanner.next();
}
double profit=scanner.nextDouble();
double bonus=0;
if(profit<0){
System.out.println("输入错误");
}else if(profit>0&&profit<=10){
bonus=profit*0.1;
}else if(profit>10&&profit<=20){
bonus=(profit-10)*0.075+1;
}else if(profit>=20&&profit<40){
bonus=(profit-10)*0.05+1.75;
}else if(profit>=40&&profit<60){
bonus=(profit-10)*0.03+2.75;
}else if(profit>=60&&profit<100){
bonus=(profit-10)*0.015+3.35;
}else{
bonus=(profit-100)*0.01+3.95;
}
System.out.println(profit+"万元利润能够获得:"+bonus+"万元");
}
}
运行结果:
请输入你创造的利润(单位:万元):
10
10.0万元利润能够获得:1.0万元
Process finished with exit code 0
3.一个整数,它加上100后是个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
public static void main(String[] args) {
for (int i = 0;
i <10000;i++) {
int num1=(int)Math.sqrt(i+100);//开方,值已固定了
int num2=(int)Math.sqrt(i+268);
//当两个数的平方等于i+100和i+268时,符合条件
if ((num1*num1==(i+100))&&(num2*num2==(i+268)))
{//符合条件的打印出来
System.out.println(i+" ");
}
}
}
运行结果:
21
261
1581
Process finished with exit code 0
4.题目:输入某年某月某日,判断这一天是这一年的第几天?
public static void main(String[] args) {
System.out.println("请输入年月日:");
Scanner sc=new Scanner(System.in);
int year=sc.nextInt();//获取年份
int month=sc.nextInt();//获取月份
int day=sc.nextInt();//获取天数
int sum=0;//天数总和
//创建一个每个月天数的数组,先按小年计算,如果是闰年,并且在三月以后再加1
int[] arr={31,28,31,30,31,30,31,31,30,31,30,31};
sum=day;//输入的天数肯定是要加的
for (int i = 1; i < month; i++) { //加上包含的月份天数
sum+=arr[i-1];
}
//最后判断是否是闰年,如果是的话再加1,否则sum就是结果
boolean isRight=(((year%4==0)&&(year%100!=0))||(year%400==0))&&(month>2);
if (isRight) {
sum+=1;
}
System.out.println(year+"年"+month+"月"+day+"日,是这年的第"+sum+"天");
}
运行结果:
请输入年月日:
2022 5 10
2022年5月10日,是这年的第130天
Process finished with exit code 0
6.输入三个整数a,b,c,请把这三个数由小到大输出
思路:本题是通过数值替换的方法,将a替换成最小的,然后是b,c。
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("输入三个数:");
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int temp=0;
if(a>b){
temp=a;
a=b;
b=temp;
}
if(a>c){
temp=a;
a=c;
c=temp;
}
if(b>c){
temp=b;
b=c;
c=temp;
}
System.out.println("这三个数从小到大排列:"+a+" "+b+" "+c);
}
运行结果:
输入三个数:
6 3 5
这三个数从小到大排列:3 5 6
Process finished with exit code 0
7.输出9*9口诀
public static void main(String[] args) {
for (int i = 1; i<= 9; i++) {
for (int j=1;j<=i;j++){
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.println();
}
}
运行结果:
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
Process finished with exit code 0
8.猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不够隐,又多吃了一个第二个早上又将剩下的桃子吃掉一般,又多吃了一个。以后每天早晨都吃了前一天剩下的一半零一个。到第10天早上想吃再吃时,见只剩下一个桃子了。求第一天一共摘了多少。
public static void main(String[] args){
int sum=1;//第一天桃子的数量
for(int i=2;i<=10;i++){
sum=(sum+1)*2;
}
System.out.println("猴子摘得桃子数为:"+sum);
}
运行结果:
猴子摘得桃子数为:1534
Process finished with exit code 0
9.两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编写程序找出三队赛手的名单。
public static void main(String[] args)
{
//第一个循环,定义a循环 x-y
for(char a='x';a<='z';a++){
for(char b='x';b<='z';b++){
//判断a!=b
if(a!=b){
//第三个循环,定义c循环x-y
for(char c='x';c<='z';c++){
//判断a!=c b!=c
if(a!=c&&b!=c){
//判断a!='x'&&c!='x'&&c!='z'
if(a!='x'&&c!='x'&&c!='z'){
System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("c:"+c);
}
}
}
}
}
}
}
运行结果:
a:z
b:x
c:y
Process finished with exit code 0
10.打印菱形图案
public static void main(String[] args){
//打印上面的部分
int n=4;
for(int i=0;i<n;i++){//控制行
for(int k=3-i;k>0;k--){//控制*前面的空格
System.out.print(" ");
}
for(int j=0;j<=2*i;j++){//控制列
System.out.print("*");
}
//输出完符号马上换行
System.out.println();
}
//打印下面部分
n=3;
for(int i=n;i>0;i--){//控制行
for(int k=3-i+1;k>0;k--){
//控制*前面空格的输出,第一行要加空格
System.out.print(" ");
}
//输出符号,但不要换行
for(int j=0;j<=2*i-2;j++){//控制列
System.out.print("*");
}
//输出完符号马上换行
System.out.println();
}
}
运行结果:
*
***
*****
*******
*****
***
*
Process finished with exit code 0
11.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和
public static void main(String[] args){
float fenzi=2;
float fenmu=1;
float sum=0;
for(int i=0;i<20;i++){
sum+=fenzi/fenmu;
fenzi=fenzi+fenmu;//分子等于前一项的分子加坟墓
fenmu=fenzi-fenmu;//分母等于上边的分子减去前一个的分母
}
System.out.println("前20项和为"+sum);
}
运行结果:
前20项和为32.660263
Process finished with exit code 0
12.求1+2!+3!+…+20!的和
public static void main(String[] args){
int sum=0;//总和
for(int i=1;i<=20;i++){
sum+=factorial(i);//累加
}
System.out.println(""+sum);
}
//求阶乘的实现
public static int factorial(int i){
int mult=1;
for(int j=1;j<=i;j++){
mult*=j;
}
return mult;//返回阶乘结果
}
运行结果:
268040729
Process finished with exit code 0
13.使用递归方法求5!
public static void main(String[] args){
System.out.println("5的阶乘为:"+Fact(5));
}
private static int Fact(int i){
if(i==1){
return 1;
}else{
return i*Fact(i-1);
}
}
5的阶乘为:120
Process finished with exit code 0
14.利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需要知道第四人的岁数,依次类推,推到第一个人(10岁),再往回推
public static void main(String[] args){
System.out.println("第五个人的岁数:"+getAge(5,2));
}
//求第m位同志的年龄
private static int getAge(int m,int n){
if(m==1)
return 10;
else
return getAge(m-1,n)+n;
}
运行结果:
第五个人的岁数:18
Process finished with exit code 0
15.给一个不多与5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。将五位数输入数组中,计算数组的长度以及它的逆序遍历
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入不多于五位的正整数:");
char[] s=sc.next().toCharArray();
System.out.println("你输入的数字是"+s.length+"位");
for(int i=s.length-1;i>=0;i--){
System.out.print(s[i]);
}
}
运行结果:
请输入不多于五位的正整数:
3242
你输入的数字是4位
2423
Process finished with exit code 0
16.一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int[] num=new int[5];
System.out.println("请输入一个五位数:");
int n=sc.nextInt();
//把每一个数放到数组中
for(int i=num.length-1;i>=0;i--){
num[i]=n%10;
n/=10;
}
//第一位数字==最后一位数字
//第二位数字==倒数第二位数组
for(int i=0;i<num.length/2;i++)
if(num[i]!=num[num.length-1-i]){
System.out.println("这个数字不是回文数");
return;
}
System.out.println("这个数字是回文数");
}
请输入一个五位数:
12321
这个数字是回文数
Process finished with exit code 0
17.请输入星期几的第一个字母来判断一下字母判断一下是星期几,如果第一个字母一样,则继承判断第二个字母
public static void main(String[] args){
System.out.println("请输入第一个字母:");
Scanner sc=new Scanner(System.in);
String input=sc.next();
String input2="";//注意input2在当首字母相同时才能输入
switch(input){
case "m":
System.out.println("今天是星期一");
break;
case "t":
System.out.println("请输入第二个字母:");
input2=sc.next();
if(input2.equals("u")){
System.out.println("今年是星期二");
}else if(input2.equals("h")){
System.out.println("今天是星期四");
}else{
System.out.println("输入的字母有误");
}
break;
case"w":
System.out.println("今天是星期三");
break;
case"f":
System.out.println("今天是星期五");
break;
case"s":
System.out.println("请输入第二个字母:");
input2=sc.next();
if(input2.equals("a")){
System.out.println("今天是星期六");
}else if(input2.equals("u")){
System.out.println("今天是星期日");
}else{
System.out.println("输入的字母有误");
}
break;
default:
System.out.println("输入的字母有误");
break;
}
}
运行结果:
请输入第一个字母:
t
请输入第二个字母:
u
今年是星期二
Process finished with exit code 0