This site requires JavaScript, please enable it in your browser!
Greenfoot back
had2ask
had2ask wrote ...

2016/12/1

Need help inputting an integer into text

had2ask had2ask

2016/12/1

#
Hello, I'm trying to create a scenario with a customer who's going over costs and sales images. Each time he does, it adds or subtracts to a profit. I need to display this profit on the bottom of the screen, but it says I can't convert the existing integer into the text string. Here's the customer code. What do I do?? Please help me! import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Customer here. * * @author (your name) * @version (a version number or a date) */ public class Customer extends Actor { private int Profit= 0; //profit=sales-cost private String salesSound = "chaching.wav"; //the string to hold the sales sound file private String costSound = "trombone.wav"; // the string to hold the cost sound file private String customerSound = ""; /** * Act - do whatever the Customer wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(1); if (Greenfoot.isKeyDown("up")) { setRotation (270); } if (Greenfoot.isKeyDown("left")) { setRotation (180); } if (Greenfoot.isKeyDown("right")) { setRotation (0); } if (Greenfoot.isKeyDown("down")) { setRotation (90); } turnAtEdge(); lookForSales(); lookForCost(); getWorld().showText ("Profit", 350,550); getWorld().showText ("The latest transaction", 350, 575); } public void turnAtEdge() { if (isAtEdge()) { turn (Greenfoot.getRandomNumber (37)-18); } } public void lookForSales() { if(isTouching(Sales.class) ) { removeTouching(Sales.class); Greenfoot.playSound(salesSound); Profit = Profit + (Greenfoot.getRandomNumber (100)+100); } } public void lookForCost() { if(isTouching(Cost.class) ) { removeTouching(Cost.class); Greenfoot.playSound(costSound); Profit = Profit - (Greenfoot.getRandomNumber (50) + 100); } } public int getProfit() { return Profit; } }
Super_Hippo Super_Hippo

2016/12/1

#
"Profit" is a String which doesn't have any relation to the 'Profit' int field. To display the 'Profit' int field, it could be done like this:
1
getWorld().showText("Profit: " + Profit, 350,550);
Where does it give you the error?
You need to login to post a reply.