回 帖 发 新 帖 刷新版面

主题:没想到才学了4天就遇到问题了,各位大虾帮忙看下

/**程序1
 *问题1:为什么定义成"float"型输出是带".0"的值?
 *
 *问题2:为什么我设置的封装无效?要怎么解决?
 *但是在Test1中就有效果!
 **/
 
 public class Test {
     private String name;
     private String sex;
     private float age;
     private int grade;
     private double laborage;
     private int year;
     private float month;
     private float day;
//     public boolean xx;    //定义属性;
     
 
 
 public Test(String n_name,String s_sex,float a_age,int g_grade,
                 double l_laborage,int y_year,float m_month,float d_day) {
                     name = n_name;
                     sex = s_sex;
                     age = a_age;
                     grade = g_grade;
                     laborage = l_laborage;
                     year = y_year;
                     month = m_month;
                     day = d_day;        //定义构造方法;
                 
         }
 /*****************************************************************************/                    
 //设置姓名,性别,年龄的方法;
             public void setName(String n_name) {
                 name = n_name;
                 }
                 public String getName() {
                     return name;
                     }
                     
 /**********/                    
             public void setSex(String s_sex) {
                 sex = s_sex;
                 }
                 public String getSex() {
                     return sex;
                     }
                     
 /**********/                    
             public void setAge(float a_age) {
                 if(a_age < 1 || a_age > 125) {
                 
                     System.out.println("年龄范围输入错误" + getAge());
                     }
                     else {
                         age = a_age;
                         }
                     }
                     public float getAge() {
                         return age;
                         }
                     
 /******************************************************************************/                    
 //设置判断学生等级的方法
             public void setGrade(int g_grade) {
                 if(g_grade < 1 || g_grade > 30) {
                     
                     
                     System.out.println("年级范围输入错误" + getGrade());
                     }
                     else if(g_grade >= 1 && g_grade <= 6) {
                         System.out.println("小学生: "+ getGrade());
                             
                         }
                     else if(g_grade >= 7 && g_grade <=9) {
                         System.out.println("初中生: "+ getGrade());
                         
                         }
                     else if(g_grade >= 10 && g_grade <= 13) {
                         System.out.println("高中生: "+ getGrade());
                             
                         }
                     else if(g_grade >= 14 && g_grade <= 18) {
                         System.out.println("大学生: " + getGrade());
                         
                         }
                     else if(g_grade >= 18 && g_grade <= 22) {
                         System.out.println("研究生: " + getGrade());
                             
                         }
                     else if(g_grade >= 23 && g_grade <= 30 ) {
                         System.out.println("科学家: " + getGrade());
                             
                         }
                         else {
                             grade = g_grade;
                             }
                         }
                     public int getGrade() {
                             return grade;
                         }
                     
 /***************************************************************************/                        
 //设置基本工资的方法                        
                 public void setLaborage(double l_laborage) {
                         laborage = l_laborage;
                     }
                     public double getLaborage() {
                             return laborage;
                         }
                         
                         
 /******************************************************/
 //设置年,月,日的方法;                        
                 public void setYear(int y_year) {
                     if(y_year < 1 || y_year > 10000) {
                         System.out.println("年份错误");
                         }
                         else {
                             year = y_year;
                             }
                     }
                     
                     public int getYear() {
                         return year;
                     }
                     
                     
                     
                 public void setMonth(float m_month) {
                     if(m_month < 1 || m_month >12) {
                         System.out.println("月份错误");
                         }
                         else {
                             month = m_month;
                             }
                     }
                     public float getMonth() {
                         return month;
                         }
                         
                         
                 public void setDay(float d_day) {
                     if(d_day < 1 || d_day > 31) {
                         System.out.println("日期错误");
                         }
                         else {
                             day = d_day;
                             }
                     }
                     public float getDay() {
                         return day;
                    }
                
/*****************************************************************/            
     }
     
     
     
 //定义一个类    
  class TestA {
     public static void main(String[] args) {
     //新建四个对象;    
     /***********/
     //设置参数的第一种方法;
     
     
        Test lp;
         lp = new Test("李鹏","男",-100f,100,500000.00,-2000,100f,100f);
         System.out.println("姓名 : " + lp.getName() 
                         + "    " +"性别 : " + lp.getSex() + "    " + "年龄 : "
                         + lp.getAge() +"    " + "年级 : " + lp.getGrade() + "    "
                         + "工资 : " + lp.getLaborage() + "\n" + lp.getYear() + "年"
                         + lp.getMonth() + "月" + lp.getDay() + "日");                
             
         /***********/    
         
         Test yy = new Test("杨琰","女",23f,20,300000.00,2008,05f,31f);
         //设置参数的第二种方法;
             System.out.println("姓名 : " + yy.getName() 
                         + "    " +"性别 : " + yy.getSex() + "    " + "年龄 : "
                         + yy.getAge() +"    " + "年级 : " + yy.getGrade() + "    "
                         + "工资 : " + yy.getLaborage() + "\n" + yy.getYear() + "年"
                         + yy.getMonth() + "月" + yy.getDay() + "日");
         
         /***********/
     /*    Test sg = new Test("邵刚","男",25f,17,200000.00,2010,2f,33f);
             System.out.println("姓名 : " + sg.getName() 
                         + "    " +"性别 : " + sg.getSex() + "    " + "年龄 : "
                         + sg.getAge() +"    " + "年级 : " + sg.getGrade() + "    "
                         + "工资 : " + yy.getLaborage() + "\n" + sg.getYear() + "年"
                         + sg.getMonth() + "月" + sg.getDay() + "日");
     
     /*    Test sg = new Test();
         sg.setName("邵刚");
         sg.setSex("男");
         sg.setAge(27f);
         sg.setGrade(14);
         sg.setLaborage(200000.00);
         sg.setYear(2008);
         sg.setMonth(03f);
         sg.setDay(16f);//在自己没有定义构造方法的时候才可以这样使用*/
         
     /*    Test yw = new Test("阎伟","男",25f,17,200000.00,2008,7f,13f);
             System.out.println("姓名 : " + yw.getName() 
                         + "    " +"性别 : " + yw.getSex() + "    " + "年龄 : "
                         + yw.getAge() +"    " + "年级 : " + yw.getGrade() + "    "
                         + "工资 : " + yy.getLaborage() + "\n" + yw.getYear() + "年"
                         + yw.getMonth() + "月" + yw.getDay() + "日");*/
         
     
         
         }
}
                 
 /**********************************************************************************/

//程序2
//为什么这里的判断就有效果,但是第12行打印语句里的"getAge()"返回的是零?
//应该怎么做才能把输入错的值也打印出来?
//
public class Test1 {
    private int age;
    
    
    public Test1() {}
        public void setAge(int gg) {
            if(gg > 10000 || gg < 1) {
        
                System.out.println("输入错误!" + getAge());
                }
            else {
                age = gg;
                }
            }
        public int getAge() {
            
            return age;
            }
            
    }
    
    
    
     class AA{
    public static void main(String[] args) {
                Test1 aa = new Test1();
                aa.setAge(88888);
                System.out.println(aa.getAge());
                
                }
            }
            

回复列表 (共4个回复)

沙发


四天能学这么多?

板凳


我也感动奇怪啊
我要是有这种能力就好啊 
第一个问题我可以回答 
浮点型不带小数吗
要和MONEY型区别一下了哦

3 楼

花了1.2万RMB报的班,老师讲的飞快,我现在都快赶不上了,才半个月,一本书就完了。

4 楼

四天就能学这么多,佩服!
   float单精度浮点型和int整型的区别在于前者带有小数位,而后者不带

我来回复

您尚未登录,请登录后再回复。点此登录或注册