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

2014/1/26

cannot find time symbol - variable time ?????

JustMe JustMe

2014/1/26

#
i don't know what i've done wrong here..... Please help me.... import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class cc_Clock here. * * @author (your name) * @version (a version number or a date) */ public class cc_Clock extends Actor { private long startingTime; private boolean counting = false; /** * Act - do whatever the cc_Clock wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (counting) { displayTime(System.currentTimeMillis() - startingTime); } } private void displayTime(long time) { time/=1000; int minutes = (int) time / 60; int seconds = (int) time % 60; String Time = minutes + ":" + seconds; setImage(new GreenfootImage(Time, 30, Color.black, new Color(0, 0, 0, 0))); } public void startClock() { counting = true; startingTime = System.currentTimeMillis(); } public String getTime() { time /= 1000; int minutes = time / 60; int seconds = time %60; String time = minutes + ":" + seconds; return time; } }
bourne bourne

2014/1/26

#
The variable "time" in the method getTime is not declared. I'm not entirely sure as to what the purpose of the method is. But maybe you meant this:
public String getTime(long time)
Which case you can do the following for your displayTime method:
private void displayTime(long time)
{
    setImage(new GreenfootImage(getTime(time), 30, Color.black, new Color(0, 0, 0, 0)));
}
And instead of this (A problem with the variable names being the same):
String time = minutes + ":" + seconds;
return time;
just do this:
return minutes + ":" + seconds;
JustMe JustMe

2014/1/26

#
now i get possible loss of precision required:int; found: long and /60; is red public String getTime(long time) { time /= 1000; int minutes = time / 60; int seconds = time %60; return minutes + ":" + seconds; } and thanks for the quick answer
danpost danpost

2014/1/26

#
'time' is of type 'long' and needs to be converted to 'int' before assigning it to 'minutes' or 'seconds' as both of them require 'int' assignments.
int minutes = (int)(time/60);
int seconds = (int)(time%60);
JustMe JustMe

2014/1/26

#
thanks danpost it compiles now
You need to login to post a reply.