Java作业5.
1. 写程序运行结果:
public class GetFilename {
请写出至少5个字符串函数 private final char pathSeparator='\\';
/* 以下方法获取文件名,文件名是最后一个分隔符\后面的子串 */
public static String pickFile(String fullpath) {
int pos=fullpath.lastIndexOf(pathSeparator);
if (pos==-1) return fullpath;
return fullpath.substring(pos+1);
}
public static void main(String ags[]) {
String filename = pickFile("d:\\java\\example\\test.java");
System.out.println("filename="+ filename);
}
}
Test.java
2. 写程序运行结果:
public class StringsDemo {
public static void main(String[] args) {
String s = "Dot saw I was Tod";
int len = s.length();
StringBuffer dest = new StringBuffer(len);
for (int i = (len - 1); i >= 0; i--)
dest.append(s.charAt(i));
System.out.String());
}
}
Dot saw I was ToddoT saw I was toD
3. 编程题:
设有一个由10个英文单词构成的字符串数组,要求:
设有一个由10个英文单词构成的字符串数组,要求:
(1) 统计以字母“w”开头的单词数;
(2) 统计单词中含“or”字符串的单词数;
(3) 统计长度为3的单词数。
import java.util.*;
public class Statistics{
public static int number_of_w(ArrayList<String> col)
{
int number=0;
Iterator<String> it=col.iterator();
for(;it.hasNext();)
{
String ();
if(s.charAt(0)=='w')
number++;
}
return number;
}
public static int number_of_or(ArrayList<String> col)
{
int count=0,i;
Iterator<String> it=col.iterator();
while(it.hasNext())
{
String ();
for(i=0;i<s.length()-1;i++)
{
String s0=s.substring(i,i+2);
if(s0.equals("or"))
count++;
}
}
return count;
}
public static int number_of_word(ArrayList<String> col)
{
int wordNum=0;
for(String s:col)
{
if(s.length()==3)
wordNum++;
}
return wordNum;
}
public static void main(String[] args){
ArrayList<String> coll=new ArrayList();
for(int i=0;i<10;i++)
{
System.out.println("Please input a word");
Scanner scanner=new Scanner(System.in);
String ();
coll.add(s);
}
Iterator ite=coll.iterator();
System.out.println("Those words is:");
while(ite.hasNext())
{
System.out.()+"\t");
}
System.out.println("The number of word with 'w' as starting is:");
System.out.println(number_of_word(coll));
System.out.println("The number of word including a string of
\"or\"is:");
System.out.println(number_of_or(coll));
System.out.println("The number of word cotaining 3 letter is:");
System.out.println(number_of_word(coll));
}
}
4. 编程题:利用随机函数产生20个10~90之间的不重复整数, 将这些数拼接在
一个字符串中, 用逗号隔开(每产生一个新数, 要保证在该串中不存在)。然后将字
符串中的整数分离存放到一个数组中,将数组的内容按由小到大的顺序输出
import java.util.*;
public class RandomSort{
public static String productString()
{
int num,j=0;
Integer Num;
String ss1,s0;
ArrayList<Integer> coll=new ArrayList();
Random ran=new Random();
Int(80)+10;
Num=new Integer(num);
coll.add(Num);
StringBuffer s=new StringBuffer(num+",");
for(int i=0;i<19+j;i++)
{
Int(80)+10;
Num=new Integer(num);
if(!ains(Num))
{
String();
coll.add(Num);
s=new StringBuffer(s0+num+",");
}
else
j++;
}
String ss2=s.substring(0,s.length()-1);
return ss2;
}
public static int[] splitString(String s)
{
String sss[]=s.split(",");
int[] a=new int[20];
for(int i=0;i<a.length;i++)
{
a[i]=Integer.parseInt(sss[i]);
}
return a;
}
public static void main(String[] args)
{
String ss=productString();
int[] aa=splitString(ss);
System.out.println("The number is:");
for(int i=0;i<aa.length;i++)
System.out.print(aa[i]+" ");
System.out.println();
Arrays.sort(aa);
System.out.println("The number sorted is:");
for(int j=0;j<aa.length;j++)
System.out.print(aa[j]+" ");
}
}
5. 编程题:
先任意输入不超过10个人的姓名和电话到电话簿,然后从电话簿中查询指定人的电话。
先任意输入不超过10个人的姓名和电话到电话簿,然后从电话簿中查询指定人的电话。
import java.util.*;
public class SearchTel{
public static void main(String[] args)
{
HashMap telList=new HashMap();
String name,tel;
for(int i=0;i<9;i++)
{
System.out.println("please inpue the name");
Scanner scanner1=new Scanner(System.in);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论