java学习笔记: 基础知识: 一些练习题

====
day 一些练习题
java学习笔记: 基础知识: 一些练习题

练习题1.把单个数字字符转换成数字,例如'7'-->7。
思路:int num = Integer.parseInt('7' + "");
因为Integer.parseInt(String s)方法里的参数s是字符串类型,如"fdffsfds",不是字符,如'a'。所以需要把字符先转换成字符串。

练习题2.字符串中的数字相加,比如"dafd1b52h34hj``89**&&"。
思路:
(1)把字符串中非数字的字符都用空格填充。
(2)split字符串==>字符串数组,每个字符串都是一组数字。
(3)将字符串数组==>数字数组
String.split(" ")如果两边没有元素,它会把它当做空字符串

练习题3.质数:只能被1和它本身整除的数叫质数,如:2,3,5,7,11……
解:
for(int i = 2; i < 100; i++){
  for(int j = 2; j < i; j++){
    if(i%j == 0)//能整除就不是质数了
  }
}

练习题4.小字符串在大字符串中出现的次数
String big = "...";
String small = "...";
int count = 0;
while(big.contains(small)){
  //替换big中的元素,用空字符串替换small,但一次只替换一个
  big = big.replaceFirst(small, "");
  count++;
}

练习题5.字符串,将每个单词的首字母转换成大写字母,其余变成小写字母
思路:
(1)先把所有字母变成小写  str = str.toLowerCase();
(2)再用空格" "切割字符串  String[] split = str.split(" ");
(3)再把首字母变成大写,其余的原样拼接
String s = split[i];
char ch = s.charAt(0);
ch -=32;//转换成大写
ss += ch;
ss += s.subString(1);//切割字符串
再拼空格,如果是最后一个元素,就不用拼接那个空格。


练习题6.数组反转
//方法一
int[] arrNew = new int[arr.length];
int index = 0;
for(int i = arr.length - 1; i >= 0; i--){
  arrNew[index] = arr[i];
  index++;
}

//方法二
//结束条件i < j
for(int i = 0, j < arr.length - 1; i < j; i++, j--){
  swap arr[i]<-->arr[j];
}

练习题7.替换字符串,用newStr替换oldStr,原字符串为desc
while(desc.contains(oldStr)){//判断是否包含老字符串
  int index = desc.indexOf(oldStr);//判断老字符串在大字符串中的位置
  String qian = desc.subString(0, index);//截取前面的
  String hou = desc.subString(index + newStr.length());//截取后面的
  desc = qian + newStr + hou;
}

练习题8.已知字符串"2015-10-20",将该日期字符串转换为日期对象,再将日期对象转换为日历对象。根据日历对象获取该日期是星期几,以及这一年的第几天。
import java.text.SimpleDateFormat;
import text.ParseException;
import java.util.Date;
import java.util.Calendar;
...{
...
String source = "2015-10-20";
String pattern = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(source);//将日期字符串转换为日期对象
Calendar cal = Calendar.getInstance();
cal.setTime(date);//将日期对象转换为日历类对象
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
int week = cal.get(Calendar.DAY_OF_WEEK);
String weekday = getWeek(week);
String pattern1 = "yyyy年-MM月-dd日";
SimpleDateFormat sdf1 = new SimpleDateFormat(pattern1);
String source1 = sdf1.format(date);
...
}

public static String getWeek(int week){
  if((week > 7) || (week <1)){
    System.out.println("Wrong parameter.");
    return null;
  }

  String[] arr = {"星期日", "星期一", ..., "星期六"};
  return arr[week - 1];
}


练习题9.功能描述:获取长度为5的随机字符串,字符串由随机的4个0-9之间(包含0和9)的整数和1个大写英文字母组成。
import java.util.Random;

public String getStr(){
  String s = "";
  Random rd = new Random();
  int loca = rd.nextInt(5);//字符的位置,随机
  for(int i = 0; i < 5; i++){
    if(i == loca){
      int nextInt = rd.nextInt(10);
      s += nextInt;
      continue;
    }
    int num = rd.nextInt(26) + 65;//0+65='A', 1+65='B'
    s += (char)num;
  }
  return s;
}


练习题10.将方法名字从name改为getName
public static String getPropertyGetMethodName(String property){
  String s = "get";
  char charAt = property.charAt(0);
  charAt = (char)(charAt - 32);//将字母转换为大写字母
  s += charAt;
  for(int i = 1; i < property.length(); i++){
    s += property.charAt(i);
  }
  return s;
}


练习题11.统计小字符串在大字符串中出现的次数
{
  String big = "...";
  String small = "...";
  int count = 0;
  while(big.contains(small)){
    count++;
    big = big.replaceFirst(small, "");
  }
  System.out.println(count);
}
====

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值