Tăng mỗi ký tự trong chuỗi thêm 1 C++
Bất cứ khi nào bạn nhận được một phương pháp không thanh lịch, điều đầu tiên, hãy cố gắng chia logic của bạn thành logic nhỏ hơn
bằng cách sử dụng các phương thức trợ giúp ở trên, mã của bạn sẽ rõ ràng và thanh lịch hơn
Tôi đã viết bài kiểm tra đơn vị và chạy mã ở trên và nó cho kết quả giống như mã của bạn This chapter explains the basic syntaxes of the Java programming language. I shall assume that you have written some simple Java programs. Otherwise, read "Introduction To Java Programming for First-time Programmers" To be proficient in a programming language, you need to master two things
The first few sections are a bit boring, as I have to explain the basic concepts with some details You may also try the "Exercises on Java Basics" Basic SyntaxesSteps in Writing a Java ProgramThe steps in writing a Java program is illustrated as follows Step 1. Write the source code boolean done = true; boolean gameOver = false; boolean isValid; isValid = false;6 using a programming text editor (such as Sublime Text, Atom, Notepad++, Textpad, gEdit) or an IDE (such as Eclipse or NetBeans) Step 2. Compile the source code boolean done = true; boolean gameOver = false; boolean isValid; isValid = false;6 into Java portable bytecode boolean done = true; boolean gameOver = false; boolean isValid; isValid = false;8 using the JDK Compiler by issuing command javac Xxx.java Step 3. Run the compiled bytecode boolean done = true; boolean gameOver = false; boolean isValid; isValid = false;8 with the input to produce the desired output, using the Java Runtime by issuing command java Xxx Java Program TemplateYou can use the following template to write your Java programs. Choose a meaningful "Classname" that reflects the purpose of your program, and write your programming statements inside the body of the "Hello" + "world" ⇒ "Helloworld" "Hi" + ", " + "world" + "!" ⇒ "Hi, world!"0 method. Don't worry about the other terms and keywords now. I will explain them in due course. Provide comments in your program 0A Sample Program Illustrating Sequential, Decision and Loop ConstructsBelow is a simple Java program that demonstrates the three basic programming constructs. sequential, loop, and conditional. Read "Introduction To Java Programming for First-time Programmers" if you need help in understanding this program 1The expected outputs are 2CommentsComments are used to document and explain your code and your program logic. Comments are not programming statements. They are ignored by the compiler and have no consequences to the program execution. Nevertheless, comments are VERY IMPORTANT for providing documentation and explanation for others to understand your programs (and also for yourself three days later) There are two kinds of comments in Java
I recommend that you use comments liberally to explain and document your code During program development, instead of deleting a chunk of statements irrevocably, you could comment-out these statements so that you could get them back later, if needed Statements and BlocksStatement. A programming statement is the smallest independent unit in a program, just like a sentence in the English language. It performs a piece of programming action. A programming statement must be terminated by a semi-colon ( "Hello" + "world" ⇒ "Helloworld" "Hi" + ", " + "world" + "!" ⇒ "Hi, world!"5), just like an English sentence ends with a period. (Why not ends with a period like an English sentence? This is because period crashes with decimal point - it is challenging for the dumb computer to differentiate between period and decimal point in the early days of computing. ) For examples, Block. A block is a group of programming statements surrounded by a pair of curly braces "Hello" + "world" ⇒ "Helloworld" "Hi" + ", " + "world" + "!" ⇒ "Hi, world!"6. All the statements inside the block is treated as one single unit. Blocks are used as the body in constructs like class, method, if-else and loop, which may contain multiple statements but are treated as one unit (one body). There is no need to put a semi-colon after the closing brace to end a compound statement. Empty block (i. e. , no statement inside the braces) is permitted For examples, White Spaces and Formatting Source CodeWhite Spaces. Blank, tab and newline are collectively called white spaces You need to use a white space to separate two keywords or tokens to avoid ambiguity, e. g. , Java, like most of the programming languages, ignores extra white spaces. That is, multiple contiguous white spaces are treated as a single white space. Additional white spaces and extra lines are ignored, e. g. , Formatting Source Code. As mentioned, extra white spaces are ignored and have no computational significance. However, proper indentation (with tabs and blanks) and extra empty lines greatly improves the readability of the program. This is extremely important for others (and yourself three days later) to understand your programs For example, the following one-line hello-world program works. But can you read and understand the program? 9Braces. Java's convention is to place the beginning brace at the end of the line, and to align the ending brace with the start of the statement. Pair-up the { } properly. Unbalanced { } is one of the most common syntax errors for beginners Indentation. Indent each level of the body of a block by an extra 3 or 4 spaces according to the hierarchy of the block. Don't use tab because tab-spaces is editor-dependent "Code is read much more often than it is written. " Hence, you have to make sure that your code is readable (by others and yourself 3 days later), by following convention and recommended coding style Variables and TypesVariables - Name, Type and ValueComputer programs manipulate (or process) data. A variable is used to store a piece of data for processing. It is called variable because you can change the value stored More precisely, a variable is a named storage location, that stores a value of a particular data type. In other words, a variable has a name, a type and stores a value
Sơ đồ sau đây minh họa ba loại biến. 34, 37 và java Xxx05. Biến 34 lưu trữ một số nguyên (hoặc số nguyên hoặc số điểm cố định); Mã định danh (hoặc Tên)Cần có một mã định danh để đặt tên cho một biến (hoặc bất kỳ thực thể nào khác, chẳng hạn như một phương thức hoặc một lớp). Java áp đặt các quy tắc sau đối với số nhận dạng
ví dụ. java Xxx54, java Xxx55, java Xxx56, java Xxx57 là các định danh hợp lệ. Nhưng java Xxx58, java Xxx59, java Xxx60, java Xxx61 KHÔNG phải là số nhận dạng hợp lệ thận trọng. Các lập trình viên không sử dụng ký tự trống trong bất kỳ tên nào (tên tệp, tên dự án, tên biến, v.v. ). Nó không được hỗ trợ (e. g. , trong Java và C/C++), hoặc sẽ đặt ra cho bạn nhiều thử thách hơn Quy ước đặt tên biếnTên biến là một danh từ hoặc một cụm danh từ được tạo thành từ nhiều từ không có khoảng cách giữa các từ. Chữ đầu viết thường, các chữ còn lại viết hoa. Ví dụ: "Hello" + "world" ⇒ "Helloworld" "Hi" + ", " + "world" + "!" ⇒ "Hi, world!"7, "Hello" + "world" ⇒ "Helloworld" "Hi" + ", " + "world" + "!" ⇒ "Hi, world!"8, java Xxx64, 31, java Xxx66, java Xxx67, java Xxx68, java Xxx69 và java Xxx70. Quy ước này còn được gọi là trường hợp lạc đà khuyến nghị
Sự định nghĩa biếnĐể sử dụng một biến trong chương trình của bạn, trước tiên bạn cần giới thiệu nó bằng cách khai báo tên và loại của nó, theo một trong các cú pháp sau. Hành động khai báo một biến phân bổ một bộ nhớ có kích thước có khả năng chứa một giá trị của loại Cú phápExampleint sum;trung bình nhân đôi; Trạng thái chuỗiMsg;int number, count; tổng nhân đôi, chênh lệch . 14169265; String helloMsg, gameOverMsg; int magicNumber = 99; double pi = 3.14169265; Chuỗi helloMsg = "xin chào,"; int sum = 0, product = 1; double height = 1. 2, chiều dài = 3. 45; Chuỗi lời chàoMsg = "xin chào. ", quitMsg = "tạm biệt. "; lưu ý rằng
Hằng số (_______1004 biến)Hằng là biến không thể thay đổi (immutable), được khai báo với từ khóa 004. Bạn chỉ có thể gán giá trị cho các biến cuối cùng MỘT LẦN. Giá trị của chúng không thể thay đổi trong quá trình thực hiện chương trình. Ví dụ nhưQuy ước đặt tên không đổi. Sử dụng các từ viết hoa, nối với dấu gạch dưới. Ví dụ: 006, 007 và 008Biểu thứcMột biểu thức là sự kết hợp của các toán tử (chẳng hạn như 009 và 010) và toán hạng (biến hoặc ký tự), có thể được đánh giá để mang lại một giá trị duy nhất thuộc một loại nhất địnhVí dụ, Bài tập ( public static string Increment(this String str)
{
var charArray = str.ToCharArray();
for (int i = charArray.Length - 1; i >= 0; i--)
{
char originalChar = charArray[i];
charArray[i] = charArray[i].Increment();
if (!originalChar.isMaxChar() && char.IsLetterOrDigit(originalChar))
break; // break when update the first alphanumeric char and it's not a max char
}
return new string(charArray);
}
|