1.写一个简单的文件或字符串加密解密的程序
需要首先确定加解密的方法,然后再根据此方法编程2.写一个时间按时区转换程序
功能:根据当前时间,算出不同时区的时间,比如本机时间是北京时间
2005年10月11日12:46,那么东七区的时间是2005年10月11日01:463.写一个字符串解析程序
一个字符串,用"||"作为分隔符,请将分隔符隔开的内容分别取出来,并
放到一个数组中。
例如:源字符串为:你好||Hello||天空||小燕子
将该字符串解析,得到的结果放到一个数组中,则该数组长度为5,数
组中的每一项分别为:
你好
Hello
天空
小燕子4.写一个手机号码解析程序,用户输入一个手机号码,判断用户输入的合法性,
如果不合法,给出提示,如果正确,给出该手机是联通还是移动的手机。5.写一个程序,找出比N小的所有素数,最后还要给出这些素数的乘积。其中N
为用户指定的某个值。
需要首先确定加解密的方法,然后再根据此方法编程2.写一个时间按时区转换程序
功能:根据当前时间,算出不同时区的时间,比如本机时间是北京时间
2005年10月11日12:46,那么东七区的时间是2005年10月11日01:463.写一个字符串解析程序
一个字符串,用"||"作为分隔符,请将分隔符隔开的内容分别取出来,并
放到一个数组中。
例如:源字符串为:你好||Hello||天空||小燕子
将该字符串解析,得到的结果放到一个数组中,则该数组长度为5,数
组中的每一项分别为:
你好
Hello
天空
小燕子4.写一个手机号码解析程序,用户输入一个手机号码,判断用户输入的合法性,
如果不合法,给出提示,如果正确,给出该手机是联通还是移动的手机。5.写一个程序,找出比N小的所有素数,最后还要给出这些素数的乘积。其中N
为用户指定的某个值。
{ public static void main(String args[])
{char a1='十',a2='点',a3='进',a4='攻';
char secret='8';
a1=(char)(a1^secret); a2=(char)(a2^secret);
a3=(char)(a3^secret); a4=(char)(a4^secret);
System.out.println("密文:"+a1+a2+a3+a4);
a1=(char)(a1^secret); a2=(char)(a2^secret);
a3=(char)(a3^secret); a4=(char)(a4^secret);
System.out.println("原文:"+a1+a2+a3+a4);
}
}
加密的原码!!
public class Example5_7
{ public static void main(String args[])
{ String s="I am Geng.X.y,she is my girlfriend";
StringTokenizer fenxi=new StringTokenizer(s," ,"); //空格和逗号做分
int number=fenxi.countTokens();
while(fenxi.hasMoreTokens())
{ String str=fenxi.nextToken();
System.out.println(str);
System.out.println("还剩"+fenxi.countTokens()+"个单词");
}
System.out.println("s共有单词:"+number+"个");
}
}
//写一个字符串解析程序
4.Pattern p = Pattern.compile("^13[0-9]{9}");
Matcher m = p.matcher(""+telNum);
return m.finds();
根据charAt(3)的判断是移动还是联通
5。主要是素数的判断算法,求积要用BigInteger,不然很容易溢出
public class Isphone{
public static main(String[] args){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();
try{
Integer.parseInt(str.substring(2,11));
int first =Integer.parseInt(str.substring(0,2));
if ( first>=15 && first<20 ){
System.out.println("is 移动的");
} else if (first <15 &&first>=10){
System.out.println("is 联通的");
} else {
throw new Exception();
}
} catch (Exception e) {
System.out.println("isn't");
}
}
}
import java.io.*;
public class Isphone{
public static main(String[] args){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();
try{
if(str.Length!=11){
throw new Exception();
}
Integer.parseInt(str.substring(2,11));
int first =Integer.parseInt(str.substring(0,2));
if ( first>=15 && first<20 ){
System.out.println("is 移动的");
} else if (first <15 &&first>=10){
System.out.println("is 联通的");
} else {
throw new Exception();
}} catch (Exception e) {
System.out.println("isn't");
}
}
}
手机号码不都13x的吗,莫非楼上的把3漏掉了只考虑第一位和第三位的?
* 将字符串按特定分隔符拆分为一个String数组
*
* @param str
* 以固定符号分割的字符串
* @param splitChar
* 分隔符
* @return 找不到时返回一个空的数组,判断数组的size
*/
public static String[] splitStr(String str, String splitChar)
{
if(str == null || str.length() == 0)
{
return new String[0];
}
if(splitChar == null || splitChar.length() == 0)
{
return new String[0];
}
int count = 1, pos = 0; while ((pos = str.indexOf(splitChar, pos)) >= 0)
{
count++;
if(pos + splitChar.length() >= str.length())
{
break;
}
else
{
pos = pos + splitChar.length();
}
} String arrSplit[] = new String[count];
if(count == 1)
{
arrSplit[0]=str;
return arrSplit;
}
if(count == 1)
{
arrSplit[0] = str;
}
else
{
int i = 0;
while (i < count)
{
if(str.indexOf(splitChar) >= 0)
{
arrSplit[i] = str.substring(0, str.indexOf(splitChar));
}
else
{
arrSplit[i] = str;
break;
}
str = str.substring(str.indexOf(splitChar) + splitChar.length());
i++;
}
}
return arrSplit;
}
public static boolean isPrime(int n){
for(int i=2;i<n;i++){if(n%i==0){//求余,做判断!
return false;
}
}
return true;
}先使把一个要被判断是不是素数的数传递进去:(int n);然后用n依次除以比不为且比自己小的所有整数。这样。一旦其中有一个可以被整除。那么他就不是素数了。反之就是素数。
只要循环从2到sqrt(n)就可以了,不需要所有<n的数
StringTokenizer sk = new StringTokenizer(s,separator);
int length = sk.countTokens();
String[] tempArray = new String[length];
int i = 0;
while(sk.hasMoreTokens()) {
tempArray[i] = sk.nextToken();
i++;
}
return tempArray;
} public static void main(String[] args) {
String s = "你好||Hello||天空||小燕子";
String[] array = StringParse.getArray(s,"||");
for (int i = 0; i < array.length; i++) {
System.out.println(i + ":" + array[i]);
}
}
}
public static String getDate(Locale country) {
Calendar c = Calendar.getInstance(country);
String time = new SimpleDateFormat("yyyy-MM-dd hh:mm").format(c.getTime());
return time;
} public static void main(String[] args) {
System.out.println(TimeTest.getDate(Locale.CHINA));
System.out.println(TimeTest.getDate(Locale.US));
}
}
package com.bb;
import java.util.StringTokenizer;
import java.util.ArrayList;
public class Split {
public static void main(String[] args)
{
String str="你好||Hello||天空||小燕子";
String strSplit="||";
String[] strArray = Split.split(str, strSplit);
for (int i = 0; i < strArray.length; i++)
{
System.out.println(strArray[i]);
}
}
public static String[] split(final String str,final String strSplit)
{
ArrayList al = new ArrayList();
StringTokenizer st = new StringTokenizer(str, strSplit);
while (st.hasMoreTokens()) {
String strElement = st.nextToken();
al.add(strElement);
}
int len = al.size();
String[] strArray = new String[len];
for (int i = 0; i < len; i++) {
strArray[i] = (String) al.get(i);
}
return strArray;
}
}
public static void main(String[] args) {
String str="13681608481";
int i=IsPhone.IsCompany(str);
if(i == 1)
System.out.println(str+"该号码为联通号码!");
else if(i == 2)
System.out.println(str+"该号码为移动号码!");
else
System.out.println(str+"该号码有问题!");
}
public static int IsCompany(String str)
{
if(11 != str.length())
return -1;
String strTemp=str.substring(0,3);
int iInt=Integer.parseInt(strTemp);
if(iInt >= 130 && iInt < 135)
return 1;
else if(iInt >= 135 && iInt < 140)
return 2;
else
return -1;
}
}