2012-10-08 146 views
0

在我的代码一个用户在一个特定的温度范围内进入用这种方法来比较(默认范围是0 - 100):返回一个范围内的BigDecimal

public class range { 
    public void rangeset() 
     { 
     int range1 = 0; 
     int range2 = 100; 

     System.out.println("Please note that the default temp range is 0-100"); 
     System.out.println("What is the starting temperature range?"); 
     range1 = sc.nextInt(); 
     System.out.println("What is the ending temperature range?"); 
     range2 = sc.nextInt(); 
     System.out.println("Your temperature range is " + range1 + " - " + range2); 

     HW_5.mainMenureturn(); 

    }//end rangeset method (instance method) 

}//range class 

再往下,我有要求的输入用户输入一个他们想要转换为华氏度的数字。

public class HW_5 { 
    public static double fahrenheit2Centigrade () 
    { 
     double result; 
     BigDecimal quotient = new BigDecimal("1.8"); 


     //take in input, take input as BigDecimal 
     System.out.println("Please enter the fahrenheit temperature you wish to convert to celsius"); 
     BigDecimal fah = sc.nextBigDecimal(); 

    } 
} 

所以,我想要做的是确保(这是一个BigDecimal)便进入了数字在其他方法中指定的范围内。

1)如何获得我的范围集方法返回范围的开始数和范围的结束数,因为您无法返回两个值?

2)然后,我如何使用这些返回值来检查方法中的BigDecimal是否在这些值之内?

请要求澄清。谢谢。

回答

1

这是一个范围问题。目前,您正在声明rangeset()方法中的两个范围变量,这意味着它们只在方法的“范围”内可见(即只有该方法才能访问这些变量)。

你应该考虑的是让这些变量对整个班级都可见。

public class range { 
    private int lowerBound; 
    private int upperBound; 

    public void rangeset() 
     { 
     int lowerBound = 0; 
     int upperBound = 100; 

     System.out.println("Please note that the default temp range is 0-100"); 
     System.out.println("What is the starting temperature range?"); 
     lowerBound = sc.nextInt(); 
     System.out.println("What is the ending temperature range?"); 
     upperBound = sc.nextInt(); 
     System.out.println("Your temperature range is " + range1 + " - " + range2); 

     HW_5.mainMenureturn(); 

     }//end rangeset method (instance method) 

    public int getLowerBound() 
    { 
     return lowerBound; 
    } 

    public int getUpperBound() 
    { 
     return upperBound; 
    } 

}//range class 

一旦你有事情以这种方式设置,你可以在你的主类创建一个新的range类,调用它的相关方法,用你的getter方法来提取您所关心的数据。例如:

public class HW_5 { 
    public static double fahrenheit2Centigrade () 
    { 
     double result; 
     BigDecimal quotient = new BigDecimal("1.8"); 
     range myRange = new range(); 
     myRange.rangeset(); 
     System.out.printf("%d - %d", myRange.getLowerBound(), myRange.getUpperBound()); 


     //take in input, take input as BigDecimal 
     System.out.println("Please enter the fahrenheit temperature you wish to convert to celsius"); 
     BigDecimal fah = sc.nextBigDecimal(); 

    } 
} 

Ps。通常你应该使用大写字母来开始你的类名,即。 Range而不是range

+0

问题是通过从** fahrenheit2Centrigrade **中调用** rangeset **方法,您迫使用户在计算中定义范围。我希望能够使用他们输入的内容将其与BigDecimal进行比较。问题是这些变量Upper和Lowerbound是本地的。 – Brian

+0

他们在我的答案中不是本地的。你可以创建'range'并在你喜欢的任何地方调用'rangeset'。如果你在一个类的范围内创建'range',那么所有的类方法都可以访问它。 –

+0

为什么当我在静态方法fahrenheit2Centrigrade的Range类中创建对象时,我得到的不能从非静态错误引用它,但是当我在静态方法中从HW_5类创建对象时,它很好吗? – Brian