1、鸡兔同笼
2、多重if比较三个数大小
3、输入一组数字,过滤掉其中的奇数,将偶数放入一个数组
4、输入一个三角形的三条边,判断能否组成三角形。若可以组成三角形,则计算周长,否则提示“非法三角形”
5、单循环写九九乘法表
6、打印菱形
7、双色球
8、找出一串字符串中的最大子字符串
9、生成验证码
10、计算a+aa+aaa+aaaa的值,a属于1-9之间的任一个输入的数字
11、比较两个字符串经过旋转操作后是否是同一字符串,如"abcde"和"bcdea"为同一字符串
第一题:鸡兔同笼
import java.util.Scanner; public class Test3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入腿的个数:"); int legs = input.nextInt(); System.out.print("请输入头的个数:"); int heads = input.nextInt(); int j = 0, t = 0; while (j >= 0 && j < heads) { t = (legs - 2 * j) / 4; if (j + t == heads) { System.out.println("鸡的数量是:" + j); System.out.println("兔的数量是:" + t); break; } j++; } } }
第二题:多重if比较三个数大小
import java.util.Scanner; public class demo2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入第一个数:"); int a = input.nextInt(); System.out.print("请输入第二个数:"); int b = input.nextInt(); System.out.print("请输入第三个数:"); int c = input.nextInt(); if (a>b){ if (b>c){ System.out.println("最大值是"+a); System.out.println("中间值是"+b); System.out.println("最小值是"+c); }else{ if (a>c){ System.out.println("最大值是"+a); System.out.println("中间值是"+c); System.out.println("最小值是"+b); }else{ System.out.println("最大值是"+c); System.out.println("中间值是"+a); System.out.println("最小值是"+b); } } }else{ if (a>c){ System.out.println("最大值是"+b); System.out.println("中间值是"+a); System.out.println("最小值是"+c); }else{ if (b>c){ System.out.println("最大值是"+b); System.out.println("中间值是"+c); System.out.println("最小值是"+a); }else{ System.out.println("最大值是"+c); System.out.println("中间值是"+b); System.out.println("最小值是"+a); } } } } }
第三题:输入一组数字,过滤掉其中的奇数,将偶数放入一个数组
import java.util.Scanner; public class Test3 { public static void main(String[] args) { int[] test,retest; test = new int[10]; retest = new int[10]; Scanner input = new Scanner(System.in); System.out.println("请输入十个不为零的数字:"); for (int i = 0; i<test.length;i++) { test[i] = input.nextInt(); } for (int i = 0 ; i < test.length; i++) { if (test[i] % 2 !=0){ test[i] = 0; }else{ continue; } } for (int j = 0 ; j < test.length;j++){ if (test[j] == 0 ){ continue; }else{ System.out.print(test[j]+"\t"); } } int j = 0; for (int i = 0;i < test.length;i++) { while (test[i] !=0) { retest[j]=test[i]; j++; } } for(int k:retest){ System.out.println(retest[k]+"\t"); } } }
第四题:输入一个三角形的三条边,判断能否组成三角形。若可以组成三角形,则计算周长,否则提示“非法三角形”
import java.util.Scanner; public class Test6 { public static void main(String[] args) { double a,b,c,sum; Scanner input = new Scanner(System.in); System.out.println("请输入三角形的第一条边:"); a = input.nextDouble(); System.out.println("请输入三角形的第二条边:"); b = input.nextDouble(); System.out.println("请输入三角形的第三条边:"); c = input.nextDouble(); if (a + b > c && a + c > b && b + c >a && a - b < c && a - c < b && b - c <a ){ sum = a + b + c; System.out.println("可以组成三角形,且三角形的周长是"+ sum); }else{ System.out.println("非法三角形"); } } }
第五题:单循环写九九乘法表
public class Test9 { public static void main(String[] args) { int[] a = {1,2,3,4,5,6,7,8,9}; for(int i=0,j=1;j<=9;){ System.out.print(a[i]+"*"+j+"="+a[i]*j+"\t"); if(j == a[i]){ System.out.println(); i = -1; j++; } ++i; } } }
第六题:打印菱形
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
public class Test10 { public static void main(String[] args) { int a; for (int b=6;b > 0;b--){ for (int i = 0; i <= b ; i++) { System.out.print(' '); } for (int i = 6; i >= b ; i--) { System.out.print('*'); } for (int i = 6; i > b; i--) { System.out.print('*'); } System.out.print("\n"); } for (int i = 0; i < 5; i++) { System.out.print(' '); System.out.print(' '); for (int j = 0; j <= i ; j++) { System.out.print(' '); } for (int j = 5; j > i; j--) { System.out.print('*'); } for (int j = 3; j >= i ; j--) { System.out.print('*'); } System.out.print("\n"); } } }
第七题:双色球代码
import java.util.Random; import java.util.Scanner; public class Test11 { public static void main(String[] args) { int[] red =new int[6]; int blue; int[] caipiao = new int[7]; Random rand = new Random(); Scanner input =new Scanner(System.in); System.out.println("------------------2023年双色球,必中大奖---------------------"); System.out.println("请输入你想买的6个红色球号码:(1-33号)"); for (int i = 0; i < red.length; i++) { int b = i; System.out.println("请输入第"+(i+1)+"个号码"); int a = input.nextInt(); if (a > 0 && a <= 33){ for (int j = 0; j < i + 1; j++) { if (red[j] == a && j!= i) { System.out.println("输入重复,请重新输入"); i = b - 1; break; }else{ red[i] = a; } } }else{ System.out.println("输入错误,请重新输入"); i = b - 1; } } System.out.print("请输入你购买的蓝色球号码(1-16号):"); boolean flag = false; while(flag!=true){ blue = input.nextInt(); if (blue > 0 && blue <= 16){ flag =true; }else{ System.out.println("输入错误,请重新输入"); } caipiao[6] = blue; } for (int i = 0; i < red.length; i++) { for (int j = 0 ; j < red.length; j++) { int b; b=j; if (i==j){ continue; }else { for (int k = 0; k < red.length; k++) { j = k; if (i == k) { continue; } while (red[j] == red[i]) { red[i] = rand.nextInt(33) + 1; k = 0; } } j = b; } } } for (int i = 0; i < red.length; i++) { for (int j = 0; j < red.length - 1-i; j++) { if (red[j]>red[j+1]){ int c = red[j+1]; red[j+1] = red[j]; red[j] = c; } } } for (int i = 0; i < caipiao.length-1; i++) { caipiao[i] = red[i]; } System.out.println("你购买的彩票号码是:"); System.out.print("\t"+"红色球"+" " + "蓝色球"); System.out.println(); for (int x:caipiao) { System.out.print(x +"\t"); } System.out.println(); int[] kj = new int[7]; int[] kjh = new int[6]; int kjl; for (int i = 0; i < red.length; i++) { red[i] = rand.nextInt(33)+1; for (int j = 0 ; j < red.length; j++) { int b; b=j; if (i==j){ continue; }else { for (int k = 0; k < red.length; k++) { j = k; if (i == k) { continue; } while (red[j] == red[i]) { red[i] = rand.nextInt(33) + 1; k = 0; } } j = b; } } } for (int i = 0; i < red.length; i++) { for (int j = 0; j < red.length - 1-i; j++) { if (red[j]>red[j+1]){ int c = red[j+1]; red[j+1] = red[j]; red[j] = c; } } } kjl = rand.nextInt(16)+1; for (int i = 0; i < kj.length-1; i++) { kj[i] = red[i]; } kj[6] = kjl; System.out.println(); System.out.println("正在开奖中,请稍后......"); try { Thread.sleep(3000); }catch (InterruptedException e){ } System.out.println("------------开奖结果-------------"); System.out.print("\t" + "红色球"+" " + "蓝色球"); System.out.println(); for (int x :kj) { System.out.print(x + "\t"); } System.out.println(); int[] diannao = new int[6]; for (int i = 0; i < diannao.length; i++) { diannao[i] = kj[i]; } int[] yonghu = new int[6]; for (int i = 0; i < yonghu.length; i++) { yonghu[i] = caipiao[i]; } int count = 0 ; for (int i = 0; i < yonghu.length; i++) { for (int j = 0; j < diannao.length; j++) { if (yonghu[i] == diannao[j]){ count++; } } } if (caipiao[6] == kj[6]){ switch (count){ case 0: case 1: case 2: System.out.println("恭喜中奖5元"); break; case 3: System.out.println("恭喜中奖10元"); break; case 4: System.out.println("恭喜中奖200元"); break; case 5: System.out.println("恭喜中奖3000元"); break; case 6: System.out.println("天呐,中奖20万元"); break; } }else { switch (count){ case 0: case 1: case 2: case 3: System.out.println("谢谢参与"); break; case 4: System.out.println("恭喜中奖10元"); break; case 5: System.out.println("恭喜中奖200元"); break; case 6: System.out.println("恭喜中得当前奖金池30%"); break; } } } }
第八题:找出一串字符串中的最大子字符串
import java.util.Scanner; public class Test7 { public static void main(String[] args) { Scanner input =new Scanner(System.in); System.out.println("请输入一串字符"); String a ; a = input.next(); String b ; int index = 0;//下标 int count = 1;//长度 int countmax = 0 ;//最大长度 int indexmax = 0; for (int i = 0; i < a.length()-1; i++) { if (a.charAt(i) == a.charAt(i+1)){ count+=1; }else { count =1; } if (countmax < count) { index = i; countmax = count; } } indexmax = index + 1 - (countmax-1); b = a.substring(indexmax,(indexmax+countmax)); System.out.println("最大连续子字符串是:"); System.out.println(b); } }
第九题:生成验证码,可以是小写字母,大写字母,数字。 规则,长度为5,内容是4位字母,1位数字,数字可以出现在任意位置
import java.util.Random; public class Test8 { public static void main(String[] args) { char[] da = new char[26]; char[] xiao = new char[26]; int[] shu = new int[10]; for (int i = 97 ; i <= 122; i++) { int number = i; char ch=(char)number; xiao[i-97] = ch; } for (int i = 65; i <= 90; i++) { int number1 = i; char ch=(char)number1; da[i-65] = ch; } for (int i = 0; i < 10; i++) { shu[i] = i; } Random rand = new Random(); int count = 0; for (int i = 0; i < 5; i++) { int x = rand.nextInt(61)+1; if(count >= 1 && x > 52) { i-=1; continue; } if(x > 52){ count++; } if (i == 4 ){ if (count < 1 ){ i-=1; continue; } } if (x > 0 && x <= 26) { System.out.print(xiao[i]); } else if (x > 26 && x <= 52) { System.out.print(da[x - 27]); } else { System.out.print(shu[x - 53]); } } } }
第十题:计算a+aa+aaa+aaaa的值,a属于1-9之间的任一个输入的数字
import java.util.Scanner; public class Test12 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入一个0-9之间的数字"); int a = input.nextInt(); int sum = 0; int b = 0 ; int c; b = a; for (int i = 1; i < 4; i++) { sum+=b; c = a; for (int j = 0; j < i; j++) { c = c * 10 ; sum+= c; } } sum += a; System.out.println(sum); } }
第十一题:比较两个字符串经过旋转操作后是否是同一字符串,如"abcde"和"bcdea"为同一字符串
public class Test14 { public static void main(String[] args) { String a , b; char[] c,d,e; c = new char[5];//原字符串放的位置 d = new char[5];//需要判定的字符串的位置 e = new char[5];//字符串移动的位置 Scanner input = new Scanner(System.in); System.out.println("请输入原字符串"); a = input.next(); System.out.println("请输入要进行比较的字符串"); b = input.next(); for (int i = 0; i < a.length(); i++) { c[i] = a.charAt(i); d[i] = b.charAt(i); } int mid; int count = 0; for (int i = 0; i < e.length; i++) { for (int j = 0; j < c.length; j++) { mid = i+j; if (i+j>=5){ mid = i+j-5; } e[j] = c[mid]; } for (int j = 0; j < d.length; j++) { if (e[j] != d[j]){ break; }else{ count++; } } if(count == 5){ System.out.println("true"); break; } } if(count != 5){ System.out.println("false"); } } }