The Student Room Group

Java help (NOT JAVASCRIPT)

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 :smile:
Reply 1
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
Is there a java equivalent of modulus? If so there's your answer.
Reply 3
Reply 4
Okay, briefly looked at modulo, I'm a bit confused as to how I could implement that into my code.
Reply 5
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:


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).
Reply 6
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:



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.
Reply 7
I'm still confused lmao.
Can someone simply walk me through it?
Reply 8
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.
Reply 9
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...?
Reply 11
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.
because it's better for you to work out yourself than to be spoonfed the answer.
Reply 13
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.
Reply 14
Still haven't told me how to actually set it out???
I'm a beginner..
Reply 15
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.
Reply 16
>:frown:
I have but It's all gobbledegook, i need a walkthrough tbh
Reply 17
Original post by uali511
>:frown:
I have but It's all gobbledegook, i need a walkthrough tbh


Can't you talk to whoever set the assignment...
Reply 18
Yes, they've said to me to read through other examples but I still do not understand?
Reply 19
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:










int userEnteredMonths= readMonthInput();
int years = userEnteredMonths / 12;
int months = userEnteredMonths - ( years *12);









(edited 6 years ago)

Quick Reply

Latest

Trending

Trending