测试一,键盘的格式化输出
import java.util.Scanner;
public class test01 {
public static void main(String[] args) {
Scanner ipt = new Scanner(System.in);
System.out.println("请输入姓名,年龄,地点:");
String name = ipt.next();
int age = ipt.nextInt();
String place = ipt.next();
System.out.println("我叫"+name+"我"+age+"岁了我家在"+place);
}
}
运行效果:
测试二,菱形的打印
import java.util.Scanner;
public class test02 {
public static void main(String[] args) {
Scanner ipt = new Scanner(System.in);
System.out.println("请输入打印的行数:");
int rows = ipt.nextInt();
boolean bool = false;
if (rows%2==0) {
rows = rows;
bool = false;
}else {
rows = rows+1;
bool = true;
}
int index = 0;
for (int i = 1; i <= rows/2; i++) {
for (int m = 0; m < rows/2-i; m++) {
System.out.print(" ");
}
for (int n = 1; n <= 2*i-1; n++) {
System.out.print("*");
index = n+1;
}
System.out.println();
}
int residue = rows-rows/2;
if (bool) {
--residue;
for (int i = residue,m = 0; i > 0; i--,m++) {
for (int j = 0; j <= m; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i*2-1; j++) {
System.out.print("*");
}
System.out.println();
}
}else {
for (int i = residue,m = 0; i > 0; i--,m++) {
for (int j = 0; j < m; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i*2-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
运行效果: