PC Scene > Troubleshooting
Help with basic java coding.
MadTarker:
hey i need help i just started my java programming class and im a little late in the class .
anyway i have 2 assignment:
the first is a programe that checks if the number you entered is dividable by 5 and 6 .
examples:
ex1: Enter a Number: 30
30 is dividable by 5 and 6
ex2: Enter a Number : 10
is dividable by 5 but not 6
ex3: Enter a Number: 23
23 is not dividable by 5 and 6
im lost so far i got this
import java.io.*
public class assignment1{
public static void main (string[]args) throws Exception
{
BufferedReader Datain = new BufferedReader (new InputStreamReader (system.in));
system.out.println("Enter a Number:");
int number Integer.pargeInt(Datain.readLine());
int n2 = 5;
int n3 = 6;
}
}
should i use an if statement now ? or what my first time coding so go easy ;/ !
and also this
Find the number of days in a month
ex1:
Enter the month: 2
Enther the year: 2009
feb 2009 has 28 days.
ex2:
Enter the month: 12
Enter the year:2008
Dec 2008 has 31 days.
Grelot:
for the first you should use an if... always try to keep your code as simple as possible... like if you need to do a chain( like 5 or 6 ) of if else if, use a switch statement...
also for the first part using modulo is a good way to check if a number is dividable by another... if it return 0 it is.. else it is not..
for the second i dont see any question...
MadTarker:
Quote from: Grelot on February 18, 2009, 07:42:10 PM>>>>for the first you should use an if... always try to keep your code as simple as possible... like if you need to do a chain( like 5 or 6 ) of if else if, use a switch statement...
also for the first part using modulo is a good way to check if a number is dividable by another... if it return 0 it is.. else it is not..
for the second i dont see any question...
<<<<
thanks for the first im working on it right now . for the second how should i go about that ? i just have no clue .
Grelot:
well you know that the only month that the number of days change is feb.. so you could do a switch case for the 11 others and when it's feb that is choosen do a modulo 4 on the year, if it return 0 it mean it's a 29 days feb...
MadTarker:
ok did a lil more research found this :
import java.util.Calendar;
public class GetDaysInMonth {
public static void main(String[] args)
{
Calendar calendar = Calendar.getInstance();
int year = 2008;
int month = Calendar.FEBRUARY;
int date = 1;
calendar.set(year, month, date);
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("Number of Days: " + days);
}
}
and edited it to this i just don't have a compiler to try and compile it right now been busy with other stuff mm . let's see:
import java.util.Calendar;
import java.text.*;
public class GetDaysInMonth {
public static void main(String[] args)
{
Calendar calendar = Calendar.getInstance();
BufferedReader yearin = new BufferedReader (new InputStreamReader(system.in));
system.out.println("Enter the year: ");
int year = Integer.pargeInt(yearin.readLine());
BufferedReader monthin = new BufferedReader (new InputStreamReader(system.in));
system.out.println("Enter the month: ");
int month = Integer.pargeInt(monthin.readLine());
int date = 1;
calendar.set(year, month, date);
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
MonthName = DateFormatSymbols().getMonths()[month]
System.out.println(MonthName + " " + year + " has " + days + "days.");
}
}
Navigation
[0] Message Index
[#] Next page