提交 c86aa0d3 编写于 作者: W wizardforcel

2020-07-03 15:57:45

上级 bd08ae48
......@@ -42,10 +42,10 @@ Sum of two matrices is:
10 8 6
```
在上面的程序中,两个矩阵存储在 2d 数组中,即`firstMatrix``secondMatrix`。 我们还定义了行数和列数,并将它们分别存储在变量`行``列`中。
在上面的程序中,两个矩阵存储在 2d 数组中,即`firstMatrix``secondMatrix`。 我们还定义了行数和列数,并将它们分别存储在变量`row``column`中。
然后,我们初始化给定行和列的新数组,称为`sum`。 该矩阵数组存储给定矩阵的加法。
我们遍历两个数组的每个索引以添加和存储结果。
最后,我们使用 for(foreach 变量)循环遍历 sum 数组中的每个元素以打印元素。
\ No newline at end of file
最后,我们使用`for``foreach`变量)循环遍历`sum`数组中的每个元素以打印元素。
\ No newline at end of file
......@@ -35,6 +35,6 @@ Frequency of e = 4
我们使用`charAt()`函数遍历字符串中的每个字符,该函数获取索引(`i`)并返回给定索引中的字符。
我们将每个字符与给定字符`ch`进行比较。 如果匹配,我们将`频率`的值增加 1。
我们将每个字符与给定字符`ch`进行比较。 如果匹配,我们将`frequency`的值增加 1。
最后,我们获得了一个以`频率`存储的字符的总出现次数,并将其打印出来。
\ No newline at end of file
最后,我们获得了一个以`frequency`存储的字符的总出现次数,并将其打印出来。
\ No newline at end of file
......@@ -2,7 +2,7 @@
> 原文: [https://www.programiz.com/java-programming/examples/vowel-consonant-count-string](https://www.programiz.com/java-programming/examples/vowel-consonant-count-string)
#### 在此程序中,您将学习使用 Java 中的 if 来计算给定句子中的元音,辅音,数字和空格的数量。
#### 在此程序中,您将学习使用 Java 中的`if`来计算给定句子中的元音,辅音,数字和空格的数量。
## 示例:计算元音,辅音,数字和空格的程序
......@@ -53,9 +53,9 @@ White spaces: 3
在上面的示例中,每个检查都有 4 个条件。
* 第一个 if 条件是检查字符是否为**元音**
* if 后面的 else if 条件是检查字符是否为**辅音**。 否则顺序应相同,否则所有元音也被视为辅音。
* 第三个条件(否则-if)是检查字符是否在 **0 到 9** 之间。
* 第一个`if`条件是检查字符是否为**元音**
* `if`后面的`else if`条件是检查字符是否为**辅音**。 否则顺序应相同,否则所有元音也被视为辅音。
* 第三个条件(`else-if`)是检查字符是否在 **0 到 9** 之间。
* 最后,最后一个条件是检查字符是否为**空格**字符。
为此,我们使用`toLowerCase()`将行小写。 这样做是为了不检查大写的 A 到 Z 和元音的优化。
......
......@@ -2,7 +2,7 @@
> 原文: [https://www.programiz.com/java-programming/examples/lexicographical-order-words](https://www.programiz.com/java-programming/examples/lexicographical-order-words)
#### 在此程序中,您将学习使用 for 循环以及如果使用 Java,则按字典顺序对元素词进行排序。
#### 在此程序中,您将学习使用`for`循环以及如果使用 Java,则按字典顺序对元素词进行排序。
## 示例:按字典顺序对字符串排序的程序
......@@ -42,15 +42,15 @@ Python
Ruby
```
在上述程序中,要排序的 5 个单词的列表存储在变量 word 中。
在上述程序中,要排序的 5 个单词的列表存储在变量`word`中。
然后,我们遍历每个单词(words [i]),并将其与数组中之后的所有单词(words [j])进行比较。 这是通过使用字符串的 compareTo()方法完成的。
然后,我们遍历每个单词(`words[i]`),并将其与数组中之后的所有单词(`words[j]`)进行比较。 这是通过使用字符串的`compareTo()`方法完成的。
如果 compareTo()的返回值大于 0,则必须在位置上进行交换,即 word [i]在 word [j]之后。 因此,在每次迭代中,单词[i]包含最早的单词。
如果`compareTo()`的返回值大于 0,则必须在位置上进行交换,即`word[i]``word[j]`之后。 因此,在每次迭代中,`word[i]`包含最早的单词。
Execution Steps
| 迭代 | 初始词 | 一世 | Ĵ | 话[] |
| 迭代 | 初始词 | `i` | `j` | `word[]` |
| --- | --- | --- | --- | --- |
| 1 | `{ "Ruby", "C", "Python", "Java" }` | 0 | 1 | `{ "C", "Ruby", "Python", "Java" }` |
| 2 | `{ "C", "Ruby", "Python", "Java" }` | 0 | 2 | `{ "C", "Ruby", "Python", "Java" }` |
......
......@@ -2,7 +2,7 @@
> 原文: [https://www.programiz.com/java-programming/examples/add-complex-number](https://www.programiz.com/java-programming/examples/add-complex-number)
#### 在此程序中,您将通过创建一个名为 Complex 的类并将其传递给函数 add()来学习如何在 Java 中相加两个复数。
#### 在此程序中,您将通过创建一个名为`Complex`的类并将其传递给函数`add()`来学习如何在 Java 中相加两个复数。
## 示例:相加两个复数
......
# Java 代码创建金字塔和图案
> 原文: [https://www.programiz.com/java-programming/examples/pyramid-pattern](https://www.programiz.com/java-programming/examples/pyramid-pattern)
#### 在此程序中,您将学习如何在 Java 中创建金字塔,半金字塔,倒金字塔,Pascal 三角形和 Floyd 三角形的控制语句。
| 源代码清单 |
| --- |
| [使用*,数字和字符打印三角形的代码](#triangle) |
| [代码使用*和数字打印倒三角形](#inverted-triangle) |
| [用于打印完整金字塔的代码](#pyramid) |
| [打印帕斯卡三角形的代码](#pascal-triangle) |
| [用于打印弗洛伊德三角形的代码](#floyd-triangle) |
## 程序使用*,数字和字符打印三角形
* * *
### 示例 1:使用*打印半金字塔的程序
```java
*
* *
* * *
* * * *
* * * * *
```
**源代码**
```java
public class Pattern {
public static void main(String[] args) {
int rows = 5;
for(int i = 1; i <= rows; ++i) {
for(int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}
```
* * *
### 示例 2:使用数字打印半金字塔 a 的程序
```java
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
```
**源代码**
```java
public class Pattern {
public static void main(String[] args) {
int rows = 5;
for(int i = 1; i <= rows; ++i) {
for(int j = 1; j <= i; ++j) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
```
* * *
### 示例 3:使用字母打印半金字塔的程序
```java
A
B B
C C C
D D D D
E E E E E
```
**Source Code**
```java
public class Pattern {
public static void main(String[] args) {
char last = 'E', alphabet = 'A';
for(int i = 1; i <= (last-'A'+1); ++i) {
for(int j = 1; j <= i; ++j) {
System.out.print(alphabet + " ");
}
++alphabet;
System.out.println();
}
}
}
```
## 程序使用*和数字打印倒半金字塔
* * *
### 示例 4:使用*倒置的半金字塔
```java
* * * * *
* * * *
* * *
* *
*
```
**Source Code**
```java
public class Pattern {
public static void main(String[] args) {
int rows = 5;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}
```
* * *
### 示例 5:使用数字的倒半金字塔
```java
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
```
**Source Code**
```java
public class Pattern {
public static void main(String[] args) {
int rows = 5;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) { system.out.print(j + " "); } system.out.println(); }< code>=>
```
## 程序使用*和数字显示金字塔和倒金字塔
* * *
### 示例 6:使用*打印完整金字塔的程序
```java
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
```
**Source Code**
```java
public class Pattern {
public static void main(String[] args) {
int rows = 5, k = 0;
for(int i = 1; i <= rows; ++i, k="0)" { for(int space="1;" <="rows" - i; ++space) system.out.print(" "); } while(k !="2" * i 1) system.out.print("* ++k; system.out.println(); code>=>
```
* * *
### 示例 7:使用数字打印金字塔的程序
```java
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
```
**Source Code**
```java
public class Pattern {
public static void main(String[] args) {
int rows = 5, k = 0, count = 0, count1 = 0;
for(int i = 1; i <= 2 rows; ++i) { for(int space="1;" <="rows" - i; ++space) system.out.print(" "); ++count; } while(k !="2" * i 1) if (count system.out.print((i + k) " else ++count1; k count1) ++k; count1="count" = system.out.println(); code>=>
```
* * *
### 示例 8:使用*倒置完整金字塔
```java
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
```
**Source Code**
```java
public class Pattern {
public static void main(String[] args) {
int rows = 5;
for(int i = rows; i >= 1; --i) {
for(int space = 1; space <= rows - i; ++space) { system.out.print(" "); } for(int j="i;" <="2" * i 1; ++j) system.out.print("* system.out.println(); }< code>=>
```
* * *
### 示例 9:打印 Pascal 的三角形
```java
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
```
**Source Code**
```java
public class Pattern {
public static void main(String[] args) {
int rows = 6, coef = 1;
for(int i = 0; i < rows; i++) {
for(int space = 1; space < rows - i; ++space) {
System.out.print(" ");
}
for(int j = 0; j <= 0 i; j++) { if (j="=" || i="=" 0) coef="1;" else * (i - j + 1) j; system.out.printf("%4d", coef); } system.out.println(); }< code>=>
```
* * *
### 示例 10:打印弗洛伊德三角形。
```java
1
2 3
4 5 6
7 8 9 10
```
**Source Code**
```java
public class Pattern {
public static void main(String[] args) {
int rows = 4, number = 1;
for(int i = 1; i <= rows; i++) { for(int j="1;" <="i;" j++) system.out.print(number + " "); ++number; } system.out.println(); }< code>=>
```
\ No newline at end of file
......@@ -85,6 +85,6 @@ This is a
Test file.
```
在上述程序中,我们没有获得字符串列表,而是获得了一个包含所有内容的字符串``
在上述程序中,我们没有获得字符串列表,而是获得了一个包含所有内容的字符串`row`
为此,我们使用`readAllBytes()`方法从给定路径读取所有字节。 然后,使用默认的`编码`将这些字节转换为字符串。
\ No newline at end of file
......@@ -30,6 +30,6 @@ public class OutputStreamString {
Hello there!
```
在上面的程序中,我们基于给定的字符串``创建了`OutputStream`。 这是使用流的`write()`方法完成的。
在上面的程序中,我们基于给定的字符串`row`创建了`OutputStream`。 这是使用流的`write()`方法完成的。
然后,我们使用`String`的构造器(将字节数组)简单地将`OutputStream`转换为`finalString`。 为此,我们使用流的`toByteArray()`方法。
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册