publicstaticvoidmain(String[] args){int a =1;int b =2;
System.out.println("交换前:");
System.out.println("a="+a);
System.out.println("b="+b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("交换后:");
System.out.println("a="+a);
System.out.println("b="+b);}
publicstaticvoidmain(String[] args){
System.out.println("请输入一个1~1000的整数:");@SuppressWarnings("resource")
Scanner sc =newScanner(System.in);int a = sc.nextInt();int b, c, d, e, sum;
e = a /1000;
b = a /100%10;
c = a /10%10;
d = a %10;
sum = b + c + d + e;
System.out.println("结果为:"+ sum);}
摄氏温度转成华氏温度
publicstaticvoidmain(String[] args){@SuppressWarnings("resource")
Scanner sc =newScanner(System.in);int a =0;//接收选择float x;//存温度
System.out.println("请选择操作:\n1.华氏温度->摄氏温度\n2.摄氏温度->华氏温度");
a = sc.nextInt();while(a !=1&& a !=2){//让用户只选择1或2
System.out.println("请选择操作:\n1.华氏温度->摄氏温度\n2.摄氏温度->华氏温度");
a =sc.nextInt();}//循环,用户继续转换吗?
out:while(true){switch(a){case1:{
System.out.println("请输入华氏温度 ");
x = sc.nextFloat();
System.out.println(x +"'F = "+(x -32)*5/9+"'C");
System.out.println("继续转换吗?\n0.退出\n1.继续");
a = sc.nextInt();//让用户只选择1或0while(a !=1&& a !=0){
System.out.println("0.退出\n1.继续");
a = sc.nextInt();}if(a ==0){//a=0,用户要退出break out;}else{//继续选择转换操作
System.out.println("请选择操作:\n1.华氏温度->摄氏温度\n2.摄氏温度->华氏温度");
a = sc.nextInt();while(a !=1&& a !=2){//让用户只选择1或2
System.out.println("请选择操作:\n1.华氏温度->摄氏温度\n2.摄氏温度->华氏温度");
a = sc.nextInt();}break;}}case2:{
System.out.println("请输入摄氏度 ");
x = sc.nextFloat();
System.out.println(x +"'C = "+(x *9/5+32)+"'F");
System.out.println("继续转换吗?\n0.退出\n1.继续");
a = sc.nextInt();while(a !=1&& a !=0){//让用户只选择1或0
System.out.println("0.退出\n1.继续");
a = sc.nextInt();}if(a ==0){//a=0,用户要退出break out;}else{//继续选择转换操作
System.out.println("请选择操作:\n1.华氏温度->摄氏温度\n2.摄氏温度->华氏温度");
a = sc.nextInt();while(a !=1&& a !=2){//让用户只选择1或2
System.out.println("请选择操作:\n1.华氏温度->摄氏温度\n2.摄氏温度->华氏温度");
a = sc.nextInt();}break;}}default:
System.out.println("Error!");}}}
给定一个任意的大写字母A~Z,转换小写字母。
publicstaticvoidmain(String[] args){@SuppressWarnings("resource")
Scanner sc =newScanner(System.in);
System.out.println("输入大写字母:");char a = sc.next().charAt(0);while(a <'A'|| a >'Z'){
System.out.print("输入错误,请重新输入:");
a =sc.next().charAt(0);}
a =(char)(a +32);
System.out.println("小写字母:"+ a);}
publicstaticvoidmain(String[] args){int salary =30000;int sum =30000;for(int i =1;i <=10;i++){
salary *=1.06;
sum = sum + salary;
System.out.println("第"+(i+1)+"年工资:\t"+salary);}
System.out.println("\n");
System.out.println("预计未来十年工资总和:"+sum);}
publicstaticvoidmain(String[] args){for(int i =0;i <=10;i++){if(i !=5){
System.out.println(i);}}}
求整数n的阶乘。
publicstaticvoidmain(String[] args){int a;@SuppressWarnings("resource")
Scanner sc =newScanner(System.in);
System.out.println("阶乘位数:");
a=sc.nextInt();getFactorial(a);}publicstaticintgetFactorial(int a){int b =1;int i =1;for(;i <= a;i++){
b *= i;}
System.out.println((i-1)+"位阶乘值位"+b);return b;}
找出大于200的最小质数。
publicstaticvoidmain(String[] args){for(int i=201;; i++){boolean flag =true;for(int j =2;j<i;j++){if(i%j==0){
flag =false;break;}}if(flag){
System.out.println(i);break;}}}
由命令行输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后为4321。
publicstaticvoidmain(String[] args){
System.out.println("请输入一个4位的正整数:");
Scanner sc =newScanner(System.in);int num = sc.nextInt();int len =0;int[] strArry =newint[4];int i=0;while(num%10!=0){
len++;
strArry[i]=num%10;
i++;
num = num/10;}
System.out.println("反转后");for(int j=0;j<len;j++){
System.out.print(strArry[j]);}}
数组
编写简单程序,要求数组长度为5,分别符值10,20,30,40,50,在控制台输出该数组的值。
publicstaticvoidmain(String[] args){int[] a =newint[5];for(int i =0; i <=4; i++){
a[i]=10*(i +1);}for(int b =0; b < a.length; b++){
System.out.print(a[b]+"\t");}}
将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。
publicstaticvoidmain(String[] args){char[] a ={'n','e','u','s','o','f','t','e','d','u','c','a','t','i','o','n'};char[] b =newchar[a.length];
System.arraycopy(a,0, b,0, a.length);
System.out.println(b);}
publicstaticvoidmain(String[] args){int x =0;int sum =0;int a[][]={{2,3,4},{4,6,8}};int b[][]={{1,5,2,8},{5,9,-10,-3},{2,7,-5,18}};int c[][]=newint[2][4];for(int i =0; i <2; i++){for(int j =0; j <4; j++){while(x <3){
sum += a[i][x]* b[x][j];
x++;}
c[i][j]= sum;
System.out.print(c[i][j]+"\t");
sum =0;
x =0;}
System.out.println("\n");}}
publicstaticvoidmain(String[] args){int[] a ={1,6,3,9,6,5,7,8};
System.out.println("请遍历数组:");for(int i : a){
System.out.print(i +"\t");}int temp;for(int i =0; i < a.length /2; i++){
temp = a[i];
a[i]= a[a.length - i -1];
a[a.length - i -1]= temp;}
System.out.println("\n逆序后的数组为:");for(int i : a){
System.out.print(i +"\t");}}
将一个数组中重复元素保留一个其他的清零。
publicstaticvoidmain(String[] args){int[] a ={1,6,6,3,9,6,5,7,8};
System.out.println("数组a:");for(int i : a){
System.out.print(i +"\t");}
System.out.println("\n");
List<Integer> list =newArrayList<Integer>();for(int i : a){if(!list.contains(i)){// 如果列表包含指定的元素,则返回 true
list.add(i);}}for(int i : list){
System.out.print(i +"\t");}}
给定一维数组{-10,2,3,246,-100,0,5},计算出数组中的平均,最大,最小值。
publicstaticvoidmain(String[] args){int[] data =newint[]{-10,2,3,246,-100,0,5};int max = data[0];// 假设第一个数为最大值int min = data[0];// 假设第一个数为最小值int sum = data[0];for(int i =1; i < data.length; i++){if(data[i]> max){
max = data[i];}if(data[i]< min){
min = data[i];}
sum += data[i];}
System.out.println("最大值为:"+ max);
System.out.println("最小值为:"+ min);
System.out.println("平均值为:"+(double) sum / data.length);}
统计一个字符串中中英文字符,数字,其他字符出现的次数。
publicstaticvoidmain(String[] args){
String str ="adbs13我爱东软,.!";
Homework00001 countCharacter =newHomework00001();
countCharacter.count(str);}int chCharacter =0;int enCharacter =0;int numberCharacter =0;int otherCharacter =0;voidcount(String str){if(null == str || str.equals("")){
System.out.println("字符串为空");return;}for(int i =0; i < str.length(); i++){char c = str.charAt(i);if((c >='A'&& c <='Z')||(c >='a'&& c <='z')){
enCharacter++;}elseif((c >='0')&&(c <='9')){
numberCharacter++;}elseif(isChinese(c)){
chCharacter++;}else{
otherCharacter++;}}
System.out.println("字符串:"+ str +"");
System.out.println("中文字符有:"+ chCharacter);
System.out.println("英文字符有:"+ enCharacter);
System.out.println("数字有:"+ numberCharacter);
System.out.println("其他字符有:"+ otherCharacter);}publicbooleanisChinese(char ch){
Character.UnicodeBlock a = Character.UnicodeBlock.of(ch);if(a == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| a == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| a == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| a == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B){returntrue;}returnfalse;}
publicstaticvoidmain(String[] args){//统计一个字符串中英文字符、数字、其它字符各自出现的次数int e =0;int n =0;int o =0;
String s ="sdghhjg111fjhjkh6666cfghfvghfgjfg9054";char[] a = s.toCharArray();for(char b:a){if(b>=65&&b<=90|| b>=97&&b<=122){
e++;}elseif(b>=48&& b<=57){
n++;}else{
o++;}}
String s ="aasssddddaaggggaakkhgaa";
String ss ="aa";int index =0;int n =0;int fromIndex =0;while((index = s.indexOf(ss,fromIndex))!=-1){
n++;
fromIndex = index+ss.length();}
System.out.println(n);