所有打印结果均为复制粘贴,不喜勿喷。
1.打印三角形
public class Sanjiaoxing {
public static void main(String[] args) {
for(int i=1;i<=5;i++){ //外层循环行数
for(int j=0;j<5-i;j++){ //打印空格的数量 5减去所在行数
System.out.print(" ");
}
for(int a=0;a<i*2-1;a++){ 打印*号是所在行数的二倍
System.out.print("*");
}
System.out.println();
}
}
}
打印结果:
*
***
*****
*******
*********
2.假设用户从控制台输入用户名和密码,根据用户名和密码去t_user表里查询用户信息
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("请输入用户名");
Scanner sc=new Scanner(System.in);
String name=sc.nextLine();
System.out.println("请输入密码");
Scanner sc1=new Scanner(System.in);
String root=sc1.nextLine();
StringBuffer sdl=new StringBuffer("select * from emp where name is not null ");
if(name!=null){
sdl.append("the username:"+name);
}
if(root!=null){
sdl.append("and root:"+root);
}
System.out.println(sdl.toString());
}
}
打印结果:
请输入用户名
张三
请输入密码
12365
select * from emp where name is not null the username:张三and root:12365
3.打印三角形
@Test
public void aa10(){
for(int i=1;i<=10;i++){
for(int j=1;j<=10-i;j++){
System.out.print("*");
}
System.out.println();
}
}
打印结果:
&
&&
&&&
&&&&
&&&&&
&&&&&&
&&&&&&&
&&&&&&&&
&&&&&&&&&
&&&&&&&&&&
4.
@Test
public void aa11(){
for(int i=1;i<10;i++){
for(int j=1;j<10-i;j++){
System.out.print(" ");
}
for(int k=1;k<=i;k++){
System.out.print("*");
}
System.out.println(" ");
}
}
打印结果: