我在java中有以下类:
public class Percentage
{
private double n;
Percentage (double n )
{
this.n=n;
}
public void setN()
{
this.n=n;
}
public double getN()
{
return n;
}
public double percntage ()
{
return this.n/100;
}
}
此Class Percentage将返回一个double值,但问题是我们无法使用以下值进行任何数学运算:
public static void main (String args[])
{
Percentage p = new Percentage(5);
double b=1;
b=p*12; // this is error because the class Percentage in not of type double
}
有什么方法可以使Percentage类型为double?
答案 0 :(得分:3)
这是一个错误,因为您将Percentage对象与double值相乘。 替代方案是
public static void main (String args[])
{
Percentage p = new Percentage(5);
double b=1;
b=p.getN()*12;
}
答案 1 :(得分:1)
您无法进行类型double
。您可以改为使用n
值执行操作。
b = p.getN()*12;
答案 2 :(得分:1)
您不能将类定义为double,因为double是基本类型。您可以做的是其他用户建议的内容:
<?php
$eps = array("house.1x02.paternity.ws_dvdrip_xvid-fov.mp4","house.1x03.occams_razor.ws_dvdrip_xvid-fov.mp4","house.1x04.maternity.ws_dvdrip_xvid-fov.mp4","house.1x05.damned_if_you_do.ws_dvdrip_xvid-fov.mp4","house.1x06.the_socratic_method.ws_dvdrip_xvid-fov.mp4","house.1x07.fidelity.ws_dvdrip_xvid-fov.mp4","house.1x08.poison.ws_dvdrip_xvid-fov.mp4","house.1x09.dnr.ws_dvdrip_xvid-fov.mp4","house.1x10.histories.ws_dvdrip_xvid-fov.mp4","house.1x11.detox.ws_dvdrip_xvid-fov.mp4","house.1x12.sports_medicine.ws_dvdrip_xvid-fov.mp4","house.1x13.cursed.ws_dvdrip_xvid-fov.mp4","house.1x14.control.ws_dvdrip_xvid-fov.mp4","house.1x15.mob_rules.ws_dvdrip_xvid-fov.mp4","house.1x16.heavy.ws_dvdrip_xvid-fov.mp4","house.1x17.role_model.ws_dvdrip_xvid-fov.mp4","house.1x18.babies_and_bathwater.ws_dvdrip_xvid-fov.mp4","house.1x19.kids.ws_dvdrip_xvid-fov.mp4","house.1x20.love_hurts.ws_dvdrip_xvid-fov.mp4","house.1x21.three_stories.ws_dvdrip_xvid-fov.mp4","house.1x22.honeymoon.ws_dvdrip_xvid-fov.mp4");
foreach ($eps as $value) {
if ($c=preg_match_all ("/.*?(\\d+).*?(\\d+)/is", $value, $matches)) {
$int1=$matches[1][0];
$int2=$matches[2][0];
$array2=array('10','20','30');
if (!in_array($int1,$array2)) {
$int1=str_replace("0","",$int1);
}
if (!in_array($int2,$array2)) {
$int2=str_replace("0","",$int2);
}
echo "INSERT INTO `tvshow` (`id`, `show_id`, `season`, `episode`, `file`) VALUES (NULL, '18', '$int1', '$int2', '$value'); <br>";
}
}
?>
它将返回您需要的双精度值。
答案 3 :(得分:1)
不,你不能让它表现得像double
,但是(如BigDecimal
)你可以提供执行相关操作的方法。
由于您的代码似乎暗示n = 10
表示10%
,即因素0.10
,您可以制作以下方法:
public double of(double value) {
return value * this.n / 100d;
}
public double add(double value) {
return value * (100d + this.n)) / 100d;
}
然后像这样使用它:
Percentage p = new Percentage(10);
double input = 55;
double d1 = p.of(input); // 10% of 55 = 5.5
double d2 = p.add(input); // 55 + 10% = 60.5