A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ab8846254 中级黑马   /  2017-3-29 17:31  /  869 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我写了一个员工类  我想在 测试
  1. /*
  2. * 自己用前面所学知识写一个员工类 要求要有
  3. * 编号 名字 年龄 职业
  4. * 要求用键盘录入输入并且用if判断年龄
  5. * */
  6. import java.util.Scanner;
  7. class yuangong{
  8.         private int id;
  9.         private String name;
  10.         private int age;
  11.         private String zhiye;
  12.         //构造方法
  13.         public yuangong(){
  14.                
  15.         }
  16.    //get***方法和set***方法
  17.         public int getid(){
  18.                 return id;
  19.         }
  20.         public void setid(int id){
  21.                 this.id=id;
  22.         }
  23.         public String getname(){
  24.                 return name;
  25.         }
  26.         public void setname(String name){
  27.                 this.name=name;
  28.         }
  29.         public int getage(){
  30.                 return age;
  31.         }
  32.         public void setage(int age){
  33.                 this.age=age;
  34.         }
  35.         public String getzhiye(){
  36.                 return zhiye;
  37.         }
  38.         public void setzhiye(String zhiye){
  39.                 this.zhiye=zhiye;
  40.         }
  41.         public void show(){
  42.         System.out.println("编号:"+getid()+"--"+"姓名"+getname()+"--"+"年龄:"+getage()+"--"+"职业"+getzhiye());
  43.         }
  44.        
  45. }
  46. public class YuangongTest {
  47.         public static void main(String[]args){
  48.                 Scanner sc=new Scanner(System.in);
  49.                 yuangong y=new yuangong();
  50.                 System.out.println("请输入员工的编号");
  51.                 int setid=sc.nextInt();
  52.                 System.out.println("请输入员工的年龄");
  53.                 int setage=sc.nextInt();
  54.                 if(setage<0||setage>120){
  55.                         System.out.println("你输入的年龄有误请重新输入");
  56.                 }
  57.                 sc = new Scanner(System.in);
  58.                 System.out.println("请输入员工的名字");
  59.                 String setname=sc.nextLine();
  60.                 sc = new Scanner(System.in);
  61.                 System.out.println("请输入员工的职业");
  62.                 String setzhiye=sc.nextLine();
  63.                
  64.                 //System.out.println("编号:"+getid()+"--"+"姓名"+getname()+"--"+"年龄:"+getage()+"--"+"职业"+getzhiye());
  65.            y.show();
  66.         }

  67. }
复制代码
类里面通过键盘录入给员工类赋值 可以失败 请问有什么办法解决吗?看代码

8 个回复

倒序浏览
回复 使用道具 举报
都中级黑马了还不懂?大神别谦虚
回复 使用道具 举报
不是,你表达的问题是什么?如果编译失败,发StrackTrace
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
兄弟,你的代码有问题啊,第一,编号你应该nextLine()如果是int的话,如果编号数字前面有0,例如007这样的编号0是显示不出来的,二,你给成员变量赋值方式不对啊,应该是y.setid()
回复 使用道具 举报
类首字母大写public static void main(String[] args) {                  Scanner sc=new Scanner(System.in);          Yuangong y=new Yuangong();          System.out.println("请输入员工的编号");          y.setid(sc.nextInt());          System.out.println("请输入员工的年龄");          y.setage(sc.nextInt());          if(sc.nextInt()<0||sc.nextInt()>120){                  System.out.println("你输入的年龄有误请重新输入");          }          System.out.println("请输入员工的名字");          y.setname(sc.nextLine());          System.out.println("请输入员工的职业");          y.setzhiye(sc.nextLine());                   System.out.println("编号:"+y.getid()+"--"+"姓名"+y.getname()+"--"+"年龄:"+y.getage()+"--"+"职业"+y.getzhiye());     y.show();  }
回复 使用道具 举报
大神在哪里,同求
回复 使用道具 举报
/*
* 自己用前面所学知识写一个员工类 要求要有
* 编号 名字 年龄 职业
* 要求用键盘录入输入并且用if判断年龄
* */
import java.util.Scanner;
class yuangong{
        private int id;
        private String name;
        private int age;
        private String zhiye;
        //构造方法
        public yuangong(){
               
        }
                //有参构造
                /*public yuangong(int id,String name,int age,String zhiye){
                        this.id = id;
                        this.name = name;
                        this.age = age;
             this.zhiye = zhiye;   
        }*/

   //get***方法和set***方法
        public int getid(){
                return id;
        }
        public void setid(int id){
                this.id=id;
        }
        public String getname(){
                return name;
        }
        public void setname(String name){
                this.name=name;
        }
        public int getage(){
                return age;
        }
        public void setage(int age){
                this.age=age;
        }
        public String getzhiye(){
                return zhiye;
        }
        public void setzhiye(String zhiye){
                this.zhiye=zhiye;
        }
        public void show(){
        System.out.println("编号:"+id+"--"+"姓名"+name+"--"+"年龄:"+age+"--"+"职业"+zhiye);
        }
        
}
public class YuangongTest {
        public static void main(String[]args){
                Scanner sc=new Scanner(System.in);
                yuangong y=new yuangong();
                System.out.println("请输入员工的编号");
                int id=sc.nextInt();
                                y.setid(id);
                System.out.println("请输入员工的年龄");
                int age=sc.nextInt();
                                y.setage(age);
                if(age>=0&&age<=120){
                       
                sc = new Scanner(System.in);
                System.out.println("请输入员工的名字");
                String name=sc.nextLine();
                                y.setname(name);
                                sc = new Scanner(System.in);
                System.out.println("请输入员工的职业");
                String zhiye=sc.nextLine();
                                y.setzhiye(zhiye);
               
                //System.out.println("编号:"+getid()+"--"+"姓名"+getname()+"--"+"年龄:"+getage()+"--"+"职业"+getzhiye());
           y.show();
                                } else {
                                 System.out.println("你输入的年龄有误请重新输入");
               
                                }

        }

}
回复 使用道具 举报
你那是定义的变量,并不是给员工对象赋值,需要重新调用方法。
回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马