java中的数字处理

Java代码 复制代码 收藏代码
  1. //利用正则表达式来判断一个字符串是否是数字
  2. import java.io.InputStream;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. public class 利用正则表达式来判断一个字符串是否是数字 {
  7. public static void main(String args[]) {
  8. Scanner s = new Scanner(System.in);
  9. String ss = s.next();
  10. String str="\\d{3,}";
  11. Pattern p =Pattern.compile(str);
  12. Matcher m = p.matcher(ss);
  13. while(m.find()){ //找到一个向后跳的
  14. System.out.print("LLLLMMMM****");
  15. }
  16. }
  17. }
  18. //3443jfkdfjkf4545545jkfjdkfjd34343jdkfjdkfjdkfj56566
  19. //LLLLMMMM****LLLLMMMM****LLLLMMMM****LLLLMMMM****
  20. public class 字符串转换为数字 {
  21. public static int convInt(String aa){
  22. return Integer.parseInt(aa);
  23. }
  24. public static int[] convIntArray(String str[]){
  25. if(str==null){return new int[0];}
  26. int ss[] = new int [str.length];
  27. for(int i=0;i<str.length;i++){
  28. ss[i]=convInt(str[i]);
  29. }
  30. return ss;
  31. }
  32. public static void main(String args[]){
  33. String s = "45679";
  34. //System.out.println(s+"字符串变为数字为:"+convInt(s));
  35. String str[] ={"295","665","32","4"};
  36. int [] a =convIntArray(str);
  37. System.out.println("把str转换为整形数组后输出为:");
  38. for(int i=0;i<a.length;i++){
  39. System.out.print(" "+a[i]);
  40. }
  41. System.out.println("\n5的3次方是"+Math.pow(5, 3));
  42. System.out.println("100的平方根是:"+Math.sqrt(100));
  43. System.out.println("5的平方根的整数值是:"+Math.round(Math.sqrt(5)));
  44. }
  45. }   
posted on 2013-02-20 20:23  蜜雪薇琪  阅读(305)  评论(0编辑  收藏  举报