| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 556 人关注过本帖
标题:关于一道类的问题
只看楼主 加入收藏
枫xby
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:82
专家分:371
注 册:2014-10-19
结帖率:71.43%
收藏
已结贴  问题点数:5 回复次数:4 
关于一道类的问题
import java.util.Scanner;

public class TestQuadraticEuqation {
    public static void main(String[] args){
        //Scanner input = new Scanner(System.in);
        //System.out.print("please input a,b,c:");
        //double x = input.nextDouble();
        //double y = input.nextDouble();
        //double z = input.nextDouble();
         
         QuadraticEuqation quadraticEuqation = new QuadraticEuqation(0,0,0);
         quadraticEuqation.geta();
         quadraticEuqation.getb();
         quadraticEuqation.getc();
        
       if(quadraticEuqation.getDiscriminant()>0)
       System.out.println("The equation has two roots,root1 = : "+quadraticEuqation.getRoot1()+"root2 = "+quadraticEuqation.getRoot2());
        else if(quadraticEuqation.getDiscriminant()==0)
           System.out.println("The equation has the same root,root = "+quadraticEuqation.getRoot1());
        else
            System.out.println("The equaton has no roots");
    }

}
class QuadraticEuqation{
    Scanner input = new Scanner(System.in);
     private double a;
     private double b;
     private double c;
   
     QuadraticEuqation(double newa,double newb,double newc){
         a = newa;
         b = newb;
         c = newc;
        }
    public double geta(){
        System.out.print("please input a:");
        double a = input.nextDouble();
        return a;
    }
    public double getb(){
        System.out.print("please input b:");
        double b = input.nextDouble();
        return b;
        }
    public double getc(){
        System.out.print("please input c:");
        double c = input.nextDouble();
        return c;
        }
    public double getDiscriminant(){
        return b*b-4*a*c;
    }
    public double getRoot1(){
        return (-1*b+Math.pow(getDiscriminant(),1/2))/(2*a);
    }
    public double getRoot2(){
        return (-1*b-Math.pow(getDiscriminant(),1/2))/(2*a);
    }
   
}
错误是
please input a:1
please input b:2
please input c:1
The equation has the same root,root = Infinity
 请大神指教
搜索更多相关主题的帖子: please import double public 
2015-04-10 23:30
hhwz
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:87
帖 子:687
专家分:4502
注 册:2011-5-22
收藏
得分:0 
QuadraticEuqation quadraticEuqation = new QuadraticEuqation(0,0,0); //给方法参数赋值0
程序代码:

 public double geta(){
        System.out.print("please input a:");
        this.a = input.nextDouble();  //修改
        return this.a;
    }
    public double getb(){
        System.out.print("please input b:");
        this.b = input.nextDouble();  //修改
        return this.b;
        }
    public double getc(){
        System.out.print("please input c:");
        this.c = input.nextDouble();   //修改
        return this.c;
        }

错误原因:以上3个写入参数 没有保存到 a,b,c 3个参数中,还是初始化的 0 所以在后面调用除法时 被除数为0发生错误
2015-04-11 10:08
枫xby
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:82
专家分:371
注 册:2014-10-19
收藏
得分:0 
回复 2楼 hhwz
那个这个this的用法我不太懂 能大概解释一下吗
2015-04-11 19:13
hhwz
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:87
帖 子:687
专家分:4502
注 册:2011-5-22
收藏
得分:4 
程序代码:

 private double a;

  public double geta(){
        
        return this.a;
        }

 public double seta(double a){
        this.a = a;//this就是指在类中定义的a 就是上面的private double a  不用this 就是传过来的a 就是seta(double a) 这个a
        }
2015-04-11 21:09
秦时的明月夜
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:13
帖 子:126
专家分:504
注 册:2013-3-12
收藏
得分:1 
回复 3楼 枫xby
this (这个)就是指本类中的东西,this.a 就是说这个类中的成员变量a
2015-04-14 20:22
快速回复:关于一道类的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018526 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved