打印小星星
public class xing { public static void main (String []args) { for(int i=0;i<=5;i++) { for(int j=0;j<=i-1;j++) { System.out.print("*"); } System.out.println(); } } }
喝酒
import java.util.Scanner; public class Test { public static void main(String[]args ) { Scanner sc= new Scanner(System.in); String name =""; int sex=0; int age=0; String ch="y"; do{ System.out.println("请输入学生姓名:"); name=sc.next(); System.out.println("请输入学生的性别(男1,女,0)"); sex=sc.nextInt(); System.out.println("请输入学生的年龄"); age=sc.nextInt(); switch(sex) { case 0: if(age<22) System.out.println("为了保护你的身体健康,不能喝酒,只能喝饮料"); else System.out.println("温馨提示,虽然能喝酒,但一定不能多喝。。"); break; case 1: if(age>18) System.out.println("温馨提示,不能过度饮酒,身体要紧。"); else System.out.println("你还是未成年人,不能喝酒"); break; default: System.out.println("请输入合法的性别(只能是男,女,没有人妖)"); } System.out.println("是否继续"); ch=sc.next(); }while("y".equals(ch)||"Y".equals(ch)); } }
调用方法和调用构造函数
public class Person { private String name; private String sex; private int age; public Person() { } public Person(String name,String sex,int age) { this.name= name; this.sex=sex; this.age=age; } public void show() { System.out.println("姓名: "+name+" –>> 性别:"+sex+"–>>年龄:"+age); } public static void main(String []args) { Person person1 = new Person("李连结","男",40); Person person2 = new Person("利智","女",26); person1.show(); person2.show(); } }[/java] [java] public class Student { private String name; private int no; private String class1; private String sex; private int age; private String address; public Student() { } public Student(String name,int no,String class1,String sex,int age,String address) { this.name=name; this.no=no; this.sex=sex; this.class1=class1; this.age=age; this.address= address; } void show() { System.out.println("姓名:"+name); } public static void main(String []args) { Student student1 = new Student("钦红",1,"1班","男",20,"龙岩"); Student student2 = new Student("啊新",2,"2班","男",22,"厦门"); if(student1.age>student2.age) student1.show(); else student2.show(); } }