Java BASIC Programming 50 questions (22-24 questions) detailed

Source: Internet
Author: User
Tags truncated

First, describe

Topic 1: Statistical input of a string, respectively, the number of uppercase and lowercase letters in the string, and the number of occurrences.

The first method uses the character wrapper class method: Islowercase (), Isuppercase (), IsDigit () to determine whether the character is such, and the second method is counted directly using the Char range comparison.

Topic 2: The user enters a string to be counted, and then enters the number of times a word or character the user wants to count.

For example, I enter the following string: Fdhiaojavajidaoijdjava
I want to count the number of Java strings in it.
problem-solving ideas: Pass in the statistics string str, and the word key that needs to be counted, then use the Java indexof () to find the first occurrence of the word position,
Count the number plus 1 and intercept the previously counted string and match it in the remaining fields.

Topic 3: Print the Yang Hui triangle of any number of rows, as shown below is the Yang Hui triangle of 8 rows.

problem-solving ideas: Yang Hui triangle main diagonal and the first column number is all 1, in addition to the current number is the upper number + above the first number of the and, expressed as a two-dimensional array: a[i][j]=a[i][j-1]+a[i-1][j-1].


Second, the source code

Source Code 1:

Package Com.yue.day11;import Java.util.scanner;public class Staticsstr {/** * counts the number of uppercase and lowercase letters in the string, and the number of numbers * The first method: Use the Character encapsulation class method: Islowercase (), Isuppercase (), IsDigit () to determine whether the class is the character. * Second method: Use Char range comparison directly to statistics * @param args */public static void main (string[] args) {System.out.println ("please input a Strin G to count The Times of Number,char: "); Scanner sc = new Scanner (system.in); String input = Sc.next (); commonstaticstr (input); CHARSTATICSTR (input);} private static void Charstaticstr (String input) {//Uses character Encapsulation class: Islowercase (), Isuppercase (), IsDigit () method. int  Digitcount = 0;int Bigcount = 0;int Smallcount = 0;char[] Charr = Input.tochararray (); for (int i = 0; i < charr.length; i++) {Char ch = charr[i];if (character.isdigit (ch)) {digitcount++;} else if (Character.islowercase (ch)) {smallcount++;} E LSE if (Character.isuppercase (ch)) {bigcount++;} else {continue;}} System.out.println ("Method One statistic Result: Number:" + Digitcount + ", Bigchar:" + bigcount+ ", Smallchar:" + Smallcount);} private staticvoid Commonstaticstr (String input) {//uses a comparison of char characters to count int digitcount = 0;int Bigcount = 0;int Smallcount = 0;for (int i = 0; I < input.length ();  i++) {Char ch = input.charat (i), if (ch >= ' 0 ' && ch <= ' 9 ') {digitcount++;} else if (ch >= ' a ' && Ch <= ' Z ') {smallcount++;} else if (ch >= ' A ' && ch <= ' z ') {bigcount++;} else {continue;}} System.out.println ("Method Two statistical results: Number:" + Digitcount + ", Bigchar:" + bigcount+ ", Smallchar:" + Smallcount);}}

Operation Result:



Source Code 2:

Package Com.yue.day11;import Java.util.scanner;public class Statisticstr {/** * user enter a string to be counted, and then enter the number of times a word or character the user wants to count * For example, I enter the following string: Fdhiaojavajidaoijdjava * I want to count the number of Java strings in it. * Problem Solving ideas: Incoming statistics string str, and the need to count the word key, and then use the Java indexof () to find the first occurrence of the word position, the number of statistics plus 1, and the previous statistics of the string truncated, in the remaining fields match. * @param args */public static void main (string[] args) {Scanner sc = new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Please input a String:"); String str = sc.nextline (); SYSTEM.OUT.PRINTLN ("Please input the string want to Count:"); String key = Sc.nextline (); int times = statisticstring (Str,key); System.out.println (times); Sc.close ();} private static int statisticstring (string str,string key) {//pass in the string to be counted str, and the word key that needs to be counted, then take advantage of Java's own indexof () Find the first occurrence of the word position,//statistics plus 1, and the previous statistics of the string truncated, in the remaining fields to match. int times = 0;while (true) {int index = Str.indexof (key), if (Index! =-1) {times++;str = str.substring (index + key.length ( ));} Else{return Times;}}}

Operation Result:



Source Code 3:

Package Com.yue.day11;import Java.util.scanner;public class Yanghuitri {/** * Prints the Yang Hui triangle of any number of lines, As shown below is 8 rows of Yang Hui triangle 111121133114641151010511615201561172135352171 thinking: Yang Hui triangle main diagonal and the first column of the number is 1, in addition to the current number is the above number + The preceding number above and represented as a two-dimensional array is: a[i][j]=a[i][j-1]+a[i-1][j-1] * @param args */public static void main (string[] args) { System.out.println ("Please enter the number of lines to print for the Yang Hui Triangle (1-50):"); Scanner Scanner = new Scanner (system.in), int line = Scanner.nextint (), while (line<=0| | LINE&GT;50) {System.out.println ("the number you entered is incorrect, re-enter the number of lines of the Yang Hui Triangle to be printed (1-50):"); line = Scanner.nextint ();} Printyanghuitri (line);} private static void Printyanghuitri (int line) {//print Yang Hui triangle: Yang Hui triangle main diagonal and first column numbers are all 1, in addition to the current number is above the number + above the previous number and.//represented as a two-dimensional array: a[i][j ]=a[i][j-1]+a[i-1][j-1]int[][] arr = new Int[line][];for (int i = 0; i < arr.length; i++) {Arr[i] = new Int[i + 1];for (int j = 0; J <= I; j + +) {if (j = = 0) {Arr[i][j] = 1;} else if (i = = j) {Arr[i][j] = 1;} else {arr[i][j] = Arr[i-1][j-1] + arr[i-1][j];}}} Invokes the method of looping through an array printarrayline (arr);} private static void Printarrayline (int[][] arR) {//Loop traversal array for (int i = 0; i < arr.length; i++) {for (int j = 0; J < Arr[i].length; J + +) {System.out.print (arr[i][ J] + "\ t");} System.out.println ();}}}

Operation Result:



Java BASIC Programming 50 questions (22-24 questions) detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.