把一个int变成一个分数,请帮忙

时间:2015-10-22 00:35:29

标签: java

我有一个编程类的作业,要求我把所有变量都作为int,但需要我把一个值作为一个分数,但结果给我0分数,我不知道怎么做它可以工作而不会将值更改为我无法做到的双倍。如何将int转换为分数? `

private String companyname;
private int stockamount;
private int currentprice;
private int eightsprice;



public StockPortfolio (String compName, int sAmount, int nPrice, int ePrice) 
{ 
  companyname = compName;
  stockamount = sAmount;
  currentprice = nPrice;
  eightsprice = ePrice;       
  this.newcprice = newcprice;
  this.new8price = new8price;
}

public String getName()
{
   return companyname;
}

   public int getStock()
{
   return stockamount;
}    

   public int getPricenorm()
{
   return currentprice;
}

   public int getPrice8th()
{
   return eightsprice;
 } 

   public int getNewPrice()
{
   return newcprice;
}

   public int getNew8Price()
{
   return new8price;
}

   private int newcprice;
   private int new8price;   

public void changePrice(int newcprice, int new8price)
{   
    currentprice = currentprice + newcprice;
    eightsprice = eightsprice + new8price;
}


public int getValue()

{
   return (getStock()*(getPricenorm()+getPrice8th())); 
}

    }

` 这是我还没有完成的测试类,因为我只是想先解决小数部分。

public class StockPortfolioTest {

public static void main(String[] args) {
StockPortfolio portfolio = new StockPortfolio("The Ramjac Corporation", 100, 37, 5/8);
System.out.println("Company: " + portfolio.getName());
System.out.println("Shares Held: " + portfolio.getStock());
System.out.println("Opening price per sale: " + portfolio.getPricenorm() + " " + portfolio.getPrice8th());
portfolio.changePrice(7, 2);
System.out.println("Change in Share Price: " );
System.out.println("Closing price per share: " + portfolio.getPricenorm()+ " " + portfolio.getPrice8th());
System.out.println("Closing Portfolio Value: ");
}}

这些是我迄今为止得到的结果

“公司:Ramjac公司

股票持有:100

每次销售开盘价:37 0

股价变动:

每股收盘价:44 2

结束投资组合价值:“

介意,我只是开始编程而且不太了解。

另外,另一个快速的问题,我如何减少一个数字中的字符数量,以便我可以将结果作为美元和美分出来,而不是以100.5美元或100.50452为例,但只需100.50美元即可

0 个答案:

没有答案