Increment by one
Countdowns are fun, especially for things like birthdays, product or rocket launches. Now you have the option to simulate one below!
This exercise is part of the course
Introduction to Java
Exercise instructions
- Use the increment operator
++
to add one toage
. - Use the decrement operator to subtract one from
days
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class AddOne {
public static void main(String[] args) {
int age = 30;
int days = 20;
System.out.println("I'm " + age + " today.");
// Increment the age by 1
____;
// Decrement the days by 1
____;
System.out.println("But I'll be " + age + " soon!");
System.out.println("Only " + days + " to go!");
}
}