实验七 常用类

实验目的

1. 了解类库的概念及API的使用方法。

2. 掌握常用类的使用方法。

实验学时 2学时

实验内容

1. 输入两个字符串str1、str2,统计字符串str2出现在str1中的次数。

如:str1=”aaas lkaaas” ,str2=” as” ,则应输出 2

提示:文本输入

import java.util.Scanner;

public class test {

public static void main(String[] args) {

 Scanner sc=new Scanner(System.in); //定义扫描键盘输入的对象

    String s = sc.nextLine();     //从键盘读入一行文本

...

sc.close();

}

package code71;
import java.util.Scanner;
public class code71 {
	public static void main(String[] args) {
	     Scanner in=new Scanner(System.in);
	     int sum=0;
		System.out.print("请输入第一个字符串:");
       String str1=in.nextLine();  //输入第一个字符串
      System.out.print("请输入第二个字符串:");
        String str2=in.nextLine();  //输入第二个字符串
        int a=str1.indexOf(str2);  //搜索str2在str1中出现的位置
      while(a!=-1) {
    	  sum++;
    	  a=str1.indexOf(str2,a+1);  //统计次数
      }
      System.out.println("字符串str2出现在str1中的次数为:"+sum+"次!");
	}
 
}

2.从键盘输入一串字符,输出有多少个不同的字符、每个字符出现的次数

package code72;
import java.util.Scanner;
import java.util.HashMap;
public class code72{
	public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    System.out.println("请输入一串字符:");
    String str=in.nextLine();
    StringBuffer b=new StringBuffer(str);  
int length=b.length();  
    str=str.toLowerCase();  
    int c=0;
    HashMap hm=new HashMap();
    for(int i=0;i<str.length();i++) {
    	char ch=str.charAt(i);  
    	if((ch>='a'&&ch<='z')) {
    		if(!hm.containsKey(ch)) { 
    			hm.put(ch,new Integer(1)); 
    			c++;
    		}
    		else {
    			Integer d=(Integer)hm.get(ch)+1;
    			hm.put(ch,d);
    		}
    	}
    }
    System.out.println("其中不同的字符总数为:"+c);
    String a;  
    int index,sum;
 for(int i=0;i<b.length();) {
	sum=0;
   a=b.substring(0,1);  
index=b.indexOf(a);  
	 while(index!=-1) {
		 sum++;
		 b.deleteCharAt(index); 
		 index=b.indexOf(a,index);  
	 }
	 System.out.println(a+"字母有"+sum+"个!");
 }
	}
 
}

3.编写一个 Java 程序,将用户输入的句子当中每一个单词的第一个字母大写。

package code73;
import java.util.Scanner;
public class code73 {
 
	public static void main(String[] args) {
		
		System.out.println("请输入一个句子:");
		Scanner in = new Scanner(System.in);
		String str = in.nextLine(); 
		String str1 = "";  
		String[] a=str.split(" "); 
		for(int i=0;i<a.length;i++){  
			a[i]=a[i].replace(a[i].charAt(0),a[i].toUpperCase().charAt(0));
            str1+=a[i]+" ";  
		}
		System.out.println("更改后的句子为:"+str1.trim());
 
	}
 
}

4. 输入一个八进制数字串(在整型数据范围内),分别以2进制、10进制、16进制输出。

提示:Integer.parseInt("100",8); //将8进制100转换成十进制数

          Integer.toBinaryString(100); //将十进制整数100转换成二进制数

package code74;
import java.util.Scanner;
public class code74 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("输入一个八进制数字串(在整型数据范围内):");
String a=in.nextLine();  
int b=Integer.parseInt(a);  
System.out.println("2进制为:"+Integer.toBinaryString(b)); 
System.out.println("10进制为:"+Integer.parseInt(a,8));
System.out.println("16进制为:"+Integer.toHexString(b));       }
 
}

5.编程输出一个随机字母。

package code75;
import java.util.Arrays;  
public class code75 {
	public static void main(String[] args) {
		
		Char a[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
				'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
		  
		char ch[]=new char[1]; { 
		for(int i=0;i<1;i++)
		{
		int b;
		b=(int)(Math.random()*(a.length)); 
		ch[i]=a[b];  
		}
		System.out.println(Arrays.toString(ch));  
		}
	}
}

6编程输出100天以后的日期和星期。

package code76;
import java.time.LocalDate;  
public class code76 {
	public static void main(String[] args) {
		
		String[] weekday = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"}; 
		LocalDate today = LocalDate.now(); 
		System.out.println("今天是:" + today + "," + weekday[today.getDayOfWeek().getValue()-1]);  
		LocalDate hundredDayLater = LocalDate.now().plusDays(100); 
		System.out.println("100天后是:" + hundredDayLater + "," + weekday[hundredDayLater.getDayOfWeek().getValue()-1]);  
	}
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值