java 基础练习
编程结构:
1. 如果一个数从左边读和从右边读都是同一个数, 就称为回文数. 例如: 686 就是一个回文数. 编程找出 1000 以内所有的回文数.
package text01;
import java.util.*;
public class t1 { public static void main(String args[]){ int i,n,m,count=0; System.out.println(\); for(i=1;i<=1000;i++) { n=i; m=0; while(n!=0) { m=m*10+n; n/=10; } if(m==i) { System.out.println(m); count++; } } } }
2. 一位妇女在河边洗碗. 邻居问:\家里来了多少个客人?\ 她回答:\ 每两个客人合用一个菜碗, 每三个客人合用一个汤碗, 每四个客人合用一个饭碗, 共用碗 65 个\ 问共来了多少客人?
package t1;
public class t2{ public static void main(String args[]){ int x=0; while(x<65){ int i=x/2; int j=x/3; int m=x/4; if(i+j+m==65){ System.out.println(x); } x++; } } }
字符串:
1 / 5
1. 给定一个字符串“ abc123ABC ”,求:1) 将字串全部转为大写 ABC123ABC
package text01;
import java.util.*;
public class t1 { public static void main(String args[]){ String str=\; String str1=str.toUpperCase(); System.out.println(str1); } }
2) 截取字串的前 3 个字符 abc
package text01;
import java.util.*; public class t1 { public static void main(String args[]){ String str=\;
String a[] = str.split(\); System.out.println(a[0]); } }
3) 去掉字符中两头的空格 abc123ABC
package text01;
public class t2 { public static void main(String[] args) { String string =\; string =myTrim(string); System.out.println(\+string+\); } public static String myTrim(String string) { int start = 0; int end = string.length()-1; while(start<=end && string.charAt(start)==' '){ start++; } while(start<=end && string.charAt(end)==' '){ end--; } return string.substring(start,end+1); } }
4) 查找字母 A 出现的位置 6
package text01;
import java.util.*; public class t1 { public static void main(String args[]){ String str=\;
System.out.println(str.indexOf(\)); } }
2. 给定一个字符串 “hello world.”,将字符串间的空格去掉.
2 / 5
(提示:可用 replaceAll 或循环方式处理)。
package text01;
import java.util.*; public class t1 { public static void main(String args[]){ String str=\; String str1=str.replaceAll(\, \); System.out.println(str1); } }
3. 求字符串”1,2,3,4,5”中所有数字的和; 4. 求字符串”1234567890”中所有数字的和;
package text01;
import java.util.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class t1 { private static List store = new ArrayList
静态方法:(以下方法写在一个类中。在 main 中写测试代码)。
1. 写一个方法,计算圆的面积。并在 main 中写测度代码。
(提示: public static double caculateArea(double radius) )
package t1;
public class Circle { double r;
3 / 5
}
Circle(double r){
this.r = r; }
public static double caculateArea(double r){ return 3.14*r*r; }
public static void main(String args[]){ System.out.println(\圆的面积是:\+caculateArea(3)); }
2. 写一个方法,计算整数数组的所有元数的算术平均 值。(提示: public static double avg(int[] data) ) 3. 写一个方法,计算下列分段函数 y=f(x)的值。 y=‐x+2.5; 0 <= x < 5
y=2‐1.5(x‐3)(x‐3); 5 <= x < 10 y=x/2‐1.5; 10 <= x < 20 (提示: public double f(double x) )
5. 写一个方法,实现对整数数组进行降序排列。(算法不限)(提示: public static void mySort(int[] data) )
package t1;
import java.util.Arrays; public class t1 { public static void main(String args[]) { int b[]={12, 90, 34, 678, 75}; Arrays.sort(b,0,5); for(int i=0;i 字符串综合练习: 1. 有一种文字,比如:“123321” ,“斗鸡山上山鸡斗”,它们正反读起来都是 完全一样的,这样一类文字称之为回文。写一支程序,从键盘读入一个字符串,然后判断是否是回文。如果是,则打印“xxxx, 是回文”,否则打印 “xxx, 不是回文!”, 其中 xxx 代表从键盘输入的字符串。 Scanner consol=new Scanner(System.in) ; //构造scanner String data=consol.nextLine();//接收键盘输入的字串存入 data,回车结束 (提示:先转字符数组后用循环比较) package t1; import java.util.Scanner; public class t2{ public static void main(String args[]){ public static boolean panduan(String str){ boolean flag = false; for(int i=0;i 4 / 5 2. 大小写字母互换 描述:把一个字符串中所有出现的大写字母都替换成小写字母,同时把小写字母替换成大写字母。 输入:输入一行:待互换的字符串。 } } } return flag; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println(\请输入一串字符串\); String str=sc.nextLine(); if(panduan(str)){ System.out.println(str+\是回文字符串\); } else System.out.println(str+\不是回文字符串\); } } 输出:输出一行:完成互换的字符串(字符串长度小于 80)。样例输入: If so, you already have a Google Account. You can sign in on the right. 样例输出: iF SO, YOU ALREADY HAVE A gOOGLE aCCOUNT. yOU CAN SIGN IN ON THE RIGHT. (提示:大小写之间 ASCII 码差 32,如:A 65 a 97) package t1; public class t1 { public static void main(String []args){ String str = \in on the right.\; String str1 = str.toLowerCase(); String str2 = str.toUpperCase(); String sum=\; for(int i=0;i if((str.substring(i, 1+i)).equals(str1.substring(i, 1+i))){ sum +=str2.substring(i, 1+i); }else{ sum +=str1.substring(i, 1+i); } } System.out.println(sum); } } 5 / 5