Making a program that calculates how many days you lived.
This is another code(below) from my CS Assignment. Here, I have to build a program that calculates how many days have you lived and how many hours you slept. I have seem to got the question but I get an error with Scanner sc = new Scanner(System.in);
Here is the code:
Code :
import java.util.*;
public class DaysLived {
public static void main(String args [] ){
Scanner sc = new Scanner(System.in);
int by;
int cy;
int mb;
int mc;
int db;
int dc;
int sleep;
int aliveday;
aliveday = ((cy-by)*365)+((mc-mb)*30)+(dc-db);
sleep = aliveday*8;
System.out.println("What's the date today?");
System.out.print ("Year: ");
cy = sc.nextInt ();
System.out.print ("Month: ");
mc = sc.nextInt ();
System.out.print ("Day: ");
dc = sc.nextInt ();
System.out.println ("Great, when were you born?");
System.out.print ("Year: ");
by = sc.nextInt ();
System.out.print ("Month: ");
mb = sc.nextInt ();
System.out.print ("Day: ");
db = sc.nextInt ();
System.out.println ("You have lived " + aliveday + "and you have slept " + sleep + " hours.");
}
}
So, I am trying to get an input from the user.
What should I do?
Thanks in advance.
Re: Making a program that calculates how many days you lived.
Quote:
but I get an error with Scanner sc = new Scanner(System.in);
You should certainly copy and paste the full text of any errors you see.
Re: Making a program that calculates how many days you lived.
Here are the errors I got:
variable aliveday may have not been initialized
variable sleep may have not been initialized
valu cannot be used ( on sleep = aliveday*8; )
Quote:
Originally Posted by
Sean4u
You should certainly copy and paste the full text of any errors you see.
Re: Making a program that calculates how many days you lived.
And now if you look carefully over the errors description, you will hopefully, get the answers yourself. Do as your compiler is asking you to do.