当前位置:网站首页>Experiment 7 use of common classes (correction post)

Experiment 7 use of common classes (correction post)

2022-07-06 13:55:00 Wen Wen likes Guo Zia

Thank you very much for discovering and pointing out the mistakes in my experimental report in time , The following amendments are made to the two existing problems :

2. Enter a string of characters from the keyboard , How many different characters are output 、 The number of occurrences of each character .

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(" Please enter a string of characters :");
    String str=in.nextLine();
    StringBuffer b=new StringBuffer(str);  // Insert string buffer StringBuffer
    int length=b.length();  // The total number of characters 
    str=str.toLowerCase();  // Convert all uppercase letters of the input string to lowercase letters 
    int c=0;
    HashMap hm=new HashMap();
    for(int i=0;i<str.length();i++) {
    	char ch=str.charAt(i);  // Returns the char value 
    	if((ch>='a'&&ch<='z')) {
    		if(!hm.containsKey(ch)) {  // Determine whether the specified key name is included 
    			hm.put(ch,new Integer(1));  //put Method , You can send a single message to HashMap Add a key value pair to the 
    			c++;
    		}
    		else {
    			Integer d=(Integer)hm.get(ch)+1;
    			hm.put(ch,d);
    		}
    	}
    }
    System.out.println(" The total number of different characters is :"+c);
    String a;  // Declare a single character 
    int index,sum;
 for(int i=0;i<b.length();) {
	sum=0;
   a=b.substring(0,1);  // Intercept a single character 
    index=b.indexOf(a);  // Search for the position of a single character in the whole string 
	 while(index!=-1) {
		 sum++;
		 b.deleteCharAt(index);  // Delete character 
		 index=b.indexOf(a,index);  // Count the times 
	 }
	 System.out.println(a+" The letters have "+sum+" individual !");
 }
	}
 
}

4. Enter an octal digit string ( Within the range of integer data ), Respectively by 2 Base number 、10 Base number 、16 Binary output .

Tips :Integer.parseInt("100",8); // take 8 Base number 100 Convert to decimal number

          Integer.toBinaryString(100); // Decimal integer 100 Convert to binary

package code74;
import java.util.Scanner;
public class code74 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println(" Enter an octal digit string ( Within the range of integer data ):");
String a=in.nextLine();  // Enter the original string 
int b=Integer.parseInt(a);  // Convert the original string to a number 
System.out.println("2 Into the system for :"+Integer.toBinaryString(b));  // take b Convert to 2 Hexadecimal number 
System.out.println("10 Into the system for :"+Integer.parseInt(a,8));  // take b Convert to 10 Hexadecimal number 
System.out.println("16 Into the system for :"+Integer.toHexString(b));  // take b Convert to 16 Hexadecimal number 
     }
 
}

My knowledge is still shallow , I sincerely hope that some friends can point out my mistakes and put forward valuable suggestions , I will correct and adopt it in time , Strive to present better content for your reference and learning , At the same time, I hope we can make progress together !

原网站

版权声明
本文为[Wen Wen likes Guo Zia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060917072754.html