Lab Purpose
1. Master the usage of the system, math, and data types in the basic language library.
2. Master the use of strings.
Lab content
1. WriteProgramThe number of characters, the number of words, and the number of sentences. (Set the sentence to "." "!" "?" Ends, and words are separated by spaces ).
Note
1. in Java, the system, math, String, data type, and other classes are all in the Java. lang package, and the system will automatically introduce them.
2. The methods of the string class are provided in Figure 2.6 On page 71 of the book. String is a class type. Therefore, when comparing whether the content of two string objects is equal, = cannot be used. It must be implemented using the equals method.
-----------------------------------------------------------------------------
Below is myCode
-----------------------------------------------------------------------------
It seems that the code I wrote does not meet the requirements of the lab. I didn't use equals. I used =. Forget it. Just do it, black cat and white cat, it is a good cat to catch mice.
Import Java. util. *; public class shiyan4 {public static void main (string Arg []) {string STR; int wordnum = 0, sentnum = 0; external scan = new external (system. in); system. out. println ("text to Statistics"); STR = scan. nextline (); For (INT I = 0; I <Str. length (); I ++) {char a = Str. charat (I); Switch (a) {Case '. ': case '? ': Case '! ': Sentnum ++; Case '': wordnum ++; break; default: break;} system. out. println ("word:" + wordnum + ", sentence:" + sentnum );}}
Here, the break in the switch should be said, if there is no break, it will continue to run in the following case, so '.','? ','! 'Both sentnum ++ and wordnum ++ are executed;
Only use spaces to execute wordnum ++;
When I got the junior exam embedded, I even got this question again. I am crazy about it. I did not speak a programming language in the embedded system. I did not test a program to read a question (C language, it is best to learn C by myself ):
Int main () {int A = 1, B = 0; Switch (a) {Case 0: A ++; Case 1: A ++; Case 2: A ++; B ++; Case 3: B ++;} printf ("A = % d, B = % d", a, B );}
The problem is to write the output result:
The answer is: a = 3, B = 2
therefore, the break in the switch should not be underestimated. I will give you a 4-point question in my junior year.