uali511
Badges: 0
Rep:
?
#1
Report Thread starter 3 years ago
#1
I'm being tasked with coding a program that prompts the user to input a number of months and it will then output the number of years and months. This is the code so far:

import java.util.Scanner;
public class Exercise2
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int allMonths, years, partMonths;

System.out.print("Age in months: ");

allMonths = sc.nextInt()

//Here i want to use an if statement to calculate years from allMonths but I'm unsure how to do it exactly
if (allMonths >= 12) {
years == 1; //This gives me an error
}else{
partMonths == allMonths; //Still unsure what to write for this part
}

//Code to make program output the amount of years and months, how to?

}
}

Greatly appreciated if y'all can help
0
reply
navip
Badges: 6
Rep:
?
#2
Report 3 years ago
#2
Hint: integer division and remainders.

Do not use an if statement for this as its not scalable. What if someone inputs 123456 as months?


Edit: also you get the error because you areusing == for assignment, use = for assignment and == for checking equality
1
reply
0xFFFFail
Badges: 15
Rep:
?
#3
Report 3 years ago
#3
Is there a java equivalent of modulus? If so there's your answer.
1
reply
Dez
Badges: 20
Rep:
?
#4
Report 3 years ago
#4
Have a look into the modulus (remainder) operator: http://www.dreamincode.net/forums/to...dulo-operator/
1
reply
uali511
Badges: 0
Rep:
?
#5
Report Thread starter 3 years ago
#5
Okay, briefly looked at modulo, I'm a bit confused as to how I could implement that into my code.
0
reply
Dez
Badges: 20
Rep:
?
#6
Report 3 years ago
#6
(Original post by uali511)
Okay, briefly looked at modulo, I'm a bit confused as to how I could implement that into my code.
Think back to doing clock arithmetic in your maths lessons. Modulo arithmetic is the same idea. On a clock face, 11 + 4 = 3 (four hours after 11 o'clock is 3 o'clock). In Java code, if you wrote something like var = (11 + 4) % 12, the result would be 3.

If you modulo by 24 instead of 12, you can work out how many whole days you have, plus the remainder amount in hours:

Code:
int var, hours, days;
var = 52;
hours = var % 24; // 4
days   = (var - hours) / 24; // (52 - 4)/24 = 2
For months and years the same idea applies. Use modulus to find the remainder, then subtract from the original value to get a number exactly divisible by 12 (the number of months in a year).
0
reply
navip
Badges: 6
Rep:
?
#7
Report 3 years ago
#7
(Original post by Dez)
Think back to doing clock arithmetic in your maths lessons. Modulo arithmetic is the same idea. On a clock face, 11 + 4 = 3 (four hours after 11 o'clock is 3 o'clock). In Java code, if you wrote something like var = (11 + 4) % 12, the result would be 3.

If you modulo by 24 instead of 12, you can work out how many whole days you have, plus the remainder amount in hours:

Code:
int var, hours, days;
var = 52;
hours = var % 24; // 4
days   = (var - hours) / 24; // (52 - 4)/24 = 2
For months and years the same idea applies. Use modulus to find the remainder, then subtract from the original value to get a number exactly divisible by 12 (the number of months in a year).
Technically you don't need to subtract if you are using integer divison.
0
reply
uali511
Badges: 0
Rep:
?
#8
Report Thread starter 3 years ago
#8
I'm still confused lmao.
Can someone simply walk me through it?
0
reply
Dez
Badges: 20
Rep:
?
#9
Report 3 years ago
#9
(Original post by uali511)
I'm still confused lmao.
Can someone simply walk me through it?
I'm not sure what else I can say to be honest. Have you tried using the modulus operator? Maybe if you try a few examples you'll be able to get the gist of it.
0
reply
uali511
Badges: 0
Rep:
?
#10
Report Thread starter 3 years ago
#10
From my code, tell me how to use th operator. I'm pretty new to java so if you actually show me it would help...?
0
reply
0xFFFFail
Badges: 15
Rep:
?
#11
Report 3 years ago
#11
https://stackoverflow.com/questions/...or-mod-in-java
0
reply
uali511
Badges: 0
Rep:
?
#12
Report Thread starter 3 years ago
#12
Do any of you even know how to Java? I could really use some help but none of you are telling me how to use the mudulus operator in relation to my program.
0
reply
0xFFFFail
Badges: 15
Rep:
?
#13
Report 3 years ago
#13
because it's better for you to work out yourself than to be spoonfed the answer.
0
reply
IWMTom
Badges: 20
Rep:
?
#14
Report 3 years ago
#14
(Original post by uali511)
Do any of you even know how to Java? I could really use some help but none of you are telling me how to use the mudulus operator in relation to my program.
Divide by 12 to get number of years, mod to get number of months. It's not hard.
0
reply
uali511
Badges: 0
Rep:
?
#15
Report Thread starter 3 years ago
#15
Still haven't told me how to actually set it out???
I'm a beginner..
0
reply
IWMTom
Badges: 20
Rep:
?
#16
Report 3 years ago
#16
(Original post by uali511)
Still haven't told me how to actually set it out???
I'm a beginner..
No one is going to do your work for you - do a bit of research online.
0
reply
uali511
Badges: 0
Rep:
?
#17
Report Thread starter 3 years ago
#17
>
I have but It's all gobbledegook, i need a walkthrough tbh
0
reply
IWMTom
Badges: 20
Rep:
?
#18
Report 3 years ago
#18
(Original post by uali511)
>
I have but It's all gobbledegook, i need a walkthrough tbh
Can't you talk to whoever set the assignment...
0
reply
uali511
Badges: 0
Rep:
?
#19
Report Thread starter 3 years ago
#19
Yes, they've said to me to read through other examples but I still do not understand?
0
reply
INTit
Badges: 17
Rep:
?
#20
Report 3 years ago
#20
Before writing the code get it clear in your head the problem and how you plan to solve it.
Input: Person inputs a certain number of months.
Expected output: Number of years and months.
Step 1) Howd we calculate the number of years ? Divide the number of months by 12.
Step2) How we get the remaining months ? Well we know a yr is 12 months and we calculated the number of years so we could convert the years we calculated to months and subtract that from the number of months they entered. The result would be the months remaining excluding the years.
.Psuedocode:
Code:
int userEnteredMonths= readMonthInput();
int years = userEnteredMonths / 12;
int months = userEnteredMonths - ( years *12);
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Are you travelling in the Uni student travel window (3-9 Dec) to go home for Christmas?

Yes (120)
28.1%
No - I have already returned home (57)
13.35%
No - I plan on travelling outside these dates (84)
19.67%
No - I'm staying at my term time address over Christmas (40)
9.37%
No - I live at home during term anyway (126)
29.51%

Watched Threads

View All
Latest
My Feed