Advanced Programming-Week17
Summary
- 命令行参数
Task 85
import java.util.*;
public class Task85{
public static void main(String[] args){
if(args.length == 0){
;
}else{
String temp="";
ArrayList<String> sentence=new ArrayList<String>();
for(int i=0;i<args.length;i++)
{
temp+=args[i]+" ";
if(args[i].endsWith("."))
{
sentence.add(temp);
temp="";
}
}
for(int i=0;i<sentence.size();i++)
{
temp=sentence.get(i);
if(i%2==0)
{
char []temp2=temp.toCharArray();
for(int j=0;j<temp2.length-1;j++)
{
if(temp2[j]!=' ')
{
if(temp2[j]<='z'&&temp2[j]>='a')
{
temp2[j]+=2;
if(temp2[j]>'z')
{
temp2[j]-=26;
}
}else if(temp2[j]<='Z'&&temp2[j]>='A')
{
temp2[j]+=2;
if(temp2[j]>'Z')
{
temp2[j]-=26;
}
}
}
System.out.print(temp2[j]);
}
}else{
temp=temp.substring(0,temp.length()-2);
temp=new StringBuffer(temp).reverse().toString();
System.out.print(" "+temp+". ");
}
}
}
}
}
Task 87
import java.util.*;
import java.util.regex.*;
public class Task87 {
public static void main(String[] args)
{
String s;
Scanner in= new Scanner(System.in);
s=in.nextLine();
in.close();
String regex1="[a-zA-Z]";
String regex2="[0-9]";
int len=s.length();
int countWords=0;
int countNum=0;
Pattern pattern1=Pattern.compile(regex1);
Matcher matcher1=pattern1.matcher(s);
while(matcher1.find())
{
countWords++;
}
Pattern pattern2=Pattern.compile(regex2);
Matcher matcher2=pattern2.matcher(s);
while(matcher2.find())
{
countNum++;
}
int countOther=len-countNum-countWords;
System.out.println("The alphabetic is "+countWords+", and he probability is "+((double)countWords/len));
System.out.println("The number is "+countNum+", and he probability is "+((double)countNum/len));
System.out.println("The other is "+countOther+", and he probability is "+((double)countOther/len));
}
}
Task 92
import java.util.*;
public class Task92
{
public static Boolean check1(String s)
{
int l=0,r=s.length()-1;
while(l<r)
{
if(s.charAt(l)==s.charAt(r))
{
l++;
r--;
}else break;
}
if(l>=r)
{
return true;
}else return false;
}
public static Boolean check2(String s[])
{
int l=1,r=s.length-1;
while(l<r)
{
if(s[l].equals(s[r]))
{
l++;
r--;
}else break;
}
if(l>=r) return true;
else return false;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String str=in.nextLine();
String str1=str.replaceAll("[^a-zA-Z]","").toLowerCase();
String str2[]=str.replaceAll("[^a-zA-Z ]","").toLowerCase().split(" ");
if(check1(str1)||check2(str2))
{
System.out.println("The sentence you enter is a palindrome.");
}else{
System.out.println("The sentence you enter is not a palindrome.");
}
in.close();
}
}