代码改变世界

Java小程序1(2015-8-30)

2015-08-30 11:21  马尔代夫_珍  阅读(298)  评论(0编辑  收藏  举报

1、/*
定义一个学生类,其属性有名字、年龄、性别,
要求用户键盘输入姓名、年龄、性别,
有个方法info,info的作业打印学生的信息
(姓名为:xx,年龄为:xx,性别为:xx
),要求,如果用户输入的年龄有误(范围不在0-150之间的),
则提示用户从新输入年龄,输入正确之后,
再进行输入性别,如果输入的性别不为:男或女,
则提示用户从新进行输入。输入完毕后,调用info方法,
将info打印的内容显示到控制台:
姓名为:xx,年龄为:xx,性别为:xx。
*/

import java.util.Scanner;

public class Student{  

          String name = "";

           int age = 0;

          String sex = "";

 public static void main(String[] args){    

             Student stu = new Student();

             Scanner scan = new Scanner(System.in);   

            System.out.print("请输入学生的姓名:");  

            String name = scan.nextLine();  

            stu.name = name;     

 

           boolean flag1 = true;  

           while(flag1){    

                    System.out.print("请输入学生的年龄:");   

                    int age = scan.nextInt();   

                   if(age>0&&age<150){    

                                 stu.age = age;    

                                flag1 = false;

                   }else{    

                           System.out.println("您输入的年龄有误,请重新输入!");   

                           flag1 = true;    

                   }  

             }             

            scan.nextLine();             //

            boolean flag2 = true; 

            while(flag2){   

                         System.out.print("请输入学生的性别:");  

                         String sex = scan.nextLine();    

                         if(sex.equals("男")||sex.equals("女")){   

                                        stu.sex = sex;   

                                        flag2 = false;    

                         }else{    

                                  System.out.println("您输入的性别有误,请重新输入!");   

                                  flag2 = true;   

                         }  

              }     

            scan.nextLine();  

            System.out.println("姓名为:"+stu.name+" 年龄:"+stu.age+" 性别:"+stu.sex);  } }

 2、

import java.util.Scanner;

public class Student{

                String name = "";  

                int age = 0;

                String sex = "";  

                public void info(){   

                        System.out.println("姓名为:"+name+",年龄为:"+age+",性别为:"+sex);  

                }  

public static void main(String[] args){  

             Scanner scan = new Scanner(System.in);

             System.out.print("请输入学生的名字:");

             String xm = scan.nextLine();

             int nl = 0;   

             String xb = "";

             boolean flag_sex = true;   

              while(flag_sex){  

                       System.out.print("请输入学生的性别:");

                       xb = scan.nextLine();        //

                        if("男".equals(xb)||"女".equals(xb)){   

                                     flag_sex = false;   

                        }else{    

                                System.out.println("您输入的性别有误,请重新输入");    

                        }  

                }  

              boolean flag_age = true;  

              while(flag_age){  

                       System.out.print("请输入学生的年龄:");  

                       nl = scan.nextInt();   

                       if(nl>0&&nl<150){   

                                  flag_age = false;   

                       }else{   

                                   System.out.println("您输入的年龄有误,请重新输入");   

                       }

              }     

              Student stu = new Student();   

              stu.name = xm;

              stu.age = nl;

              stu.sex = xb;   

              stu.info();

 } }