11.10
Output a line of straight lines.
Needs to
use loops to complete.
Receive an integer from the keyboard.
Assuming that the user enters a 6
then output 6 star flowers, solve the code in a row of
6
import java.util.Scanner;
public class ppp {
public static void main(String[] args) {
// 固定的获得Scanner工具的写法
// 类型名称 变量名 = 值;
Scanner ipt = new Scanner(System.in);
// 使用工具的功能,得到一个整数
System.out.println("请输入内容:");
int a = ipt.nextInt();
System.out.println("从键盘接收到了一个整数,值为" + a);
for (int i = 0; i < a; i++) {
System.out.print("*");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Article
Output a planet flower
Output multi-line straight line
Demand
Receive an integer from the keyboard
If the user enters 2
Then output two lines of content
The content of each line is *****
A line of content composed of five star flowers
Solve the
code
import java.util.Scanner;
public class ppp { public static void main(String[] args) {
// 获得键盘输入的工具
Scanner ipt = new Scanner(System.in);
// 使用工具的方法
System.out.println("请问需要打印几行:");
int num = ipt.nextInt();
for (int i = 0; i < num; i++) {
// 需要重复做的内容
System.out.println("*****");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Output a rectangle
Need
to receive an integer from the keyboard
If the user input is 3,
then output three lines of content,
each line is composed of three *
Solution
code
import java.util.Scanner;
public class ppp {
public static void main(String[] args) {
// 代码简化,输出矩形
int num = 4;
for (int j = 0; j < num; j++) {
// 输出一行星花,由三个符号组号
for (int i = 0; i < num; i++) {
System.out.print("*");
}
System.out.println();
}
}
}