倒序1
import java.util.*; public class daoxu { public static void main(String[] args) { Scanner r=new Scanner(System.in); int []g=new int[5]; for(int b=0;b<=4;b++) { int c=r.nextInt(); g[b]=c; } for(int i=4;i>=0;i--) { System.out.println(g [i]); } } }
倒序2
import java.util.*; public class daoxu2 { public static void main(String[] args) { Scanner r=new Scanner (System.in); String s=r.nextLine(); char []w=new char[s.length()]; for(int a=0;a<s.length();a++) { char b=s.charAt(a); w[a]=b; } for(int j=s.length()-1;j>=0;j--) { System.out.print(w[j]); } } }
格式化
public class geshihua {public static void main(String[] args) { int a=586; double b=251111111.23; String c="hello"; System.out.printf("%d",a); System.out.printf("%-9.6f",b); System.out.printf("%s",c); } }
判断是否回文
import java.util.*; public class hwzf { public static void main(String[] args) { Scanner r=new Scanner(System.in); String a=r.nextLine(); int i=0,n,j; boolean s=true; n=a.length(); char b,c; for(j=1;j<=n/2;j++) { b=a.charAt(i); c=a.charAt(n-1-i); if(b==c) { i++; } else { s=false; } } System.out.println(" "+s); } }
截取字符串中部分字符
public class hyk { public static void main(String[] args) { String s=("啦啦啦啦你傻了吗"); char[]a= new char[80]; s.getChars(0,5,a,0); System.out.println(a); } }
求三阶方阵对角线之和
public class kehou5_3 { public static void main(String[] args) { int [][]a={{5,6,3},{4,8,9,},{8,9,4}}; for(int b=0;b<3;b++) { for(int c=0;c<3;c++){ System.out.print(a[b][c]); } System.out.println(); } System.out.println("对角线之和为"+(a[0][0]+a[1][1]+a[2][2])); } }
打印空心六边形
public class liubianxing { public static void main(String[] args) { int a[]= {0,0,1,1,1,0,2, 0,1,0,0,0,1,2, 1,0,0,0,0,0,3, 0,1,0,0,0,1,2, 0,0,1,1,1,0,2}; for(int b=0;b<a.length;b++) { if(a[b]==0) { System.out.print(" "); } if(a[b]==1) { System.out.print(" *"); } if(a[b]==2) { System.out.println(" "); } if(a[b]==3) { System.out.println(" *"); } } } }
输出三位数中所有水仙花数
public class shuixianhua {public static void main(String[] args) { System.out.println("水仙花数有:"); for(int a=1;a<=9;a++) { int z=a*100; for(int b=0;b<=9;b++) { int x=b*10; for(int c=0;c<=9;c++) { int e=z+x+c; if(e==(a*a*a)+(b*b*b)+(c*c*c)){ System.out.print(e+" "); } } } } } }
随机数
import java.util.Random; public class suijishu {public static void main(String [] args) { Random a=new Random(); int []b=new int[100]; for(int c=0;c<=99;c++) { int d=a.nextInt(6); b[c]=d; } for(int e=0;e<100;e++){ System.out.println(b [e]); } System.out.println("输出每个数的概率均是"+"1/7"); } }
选择排序
public class xuanze {public static void main(String[] args) { int []a= {99,87,66,56,40}; System.out.println("排序前"); for(int w=0;w<5;w++) { System.out.println(" "+a[w]); } for(int c=0;c<a.length-1;c++) { int d=c; for(int e=c+1;e<a.length;e++) { d=a[c]>a[e]?e:c; int f=a[c]; a[c]=a[d]; a[d]=f; } } System.out.println("排序后"); for(int g=0;g<6;g++) { System.out.println(" "+a[g]); } } }
判断输入字符串首字母是否大写并输出大写字母计算总个数
import java.util.*; public class zfcdx { public static void main(String[] args) { Scanner r=new Scanner (System.in); String a=r.nextLine(); System.out.println(a); char c=a.charAt(0); int i,j=0; if(c>=65&&c<=90) { for(i=0;i<a.length();i++) { char d=a.charAt(i); if(d>=65&&d<=90){ System.out.println(d); j++; } } System.out.println("大写字母共有"+j+"项"); } else { System.out.println("您输入的字符串无大写字母"); } } }
输出两字符串判断是否相等
import java.util.*; public class zfxd { public static void main(String[] args) { System.out.println("请输入两个字符串"); Scanner r=new Scanner(System.in); String a=r.nextLine(); String b=r.nextLine(); System.out.println(a.equals(b)); } }