Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,273 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,266 people online right now. Registration is fast and FREE... Join Now!




Stuck at loops

 
Reply to this topicStart new topic

Stuck at loops, Please give me an advise

safialan
13 Oct, 2008 - 08:03 PM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 7

Hello guys, I am a beginner in java programming and I am going nuts already :s
Well I am suppose to write an application that will prompt the user 5 times to enter a year and then it will determine whether or not the year is a leap year.
I wrote the application and it determines if the year is leap or not, however it won't prompt the user 5 times to enter a year... There might be very simple way to set it but I am just stuck at this point... My application just asks once...
I'd be so happy if someone can help me with this issue...
Thank you in advance...



\\ here is the code
CODE

import java.util.Scanner;

public class LeapYearApp {
    
    
public static void main (String[] args){

Scanner input = new Scanner( System.in );


int year;


{

System.out.print( "Please enter a year. " );
           year = input.nextInt();


      
if ( year % 4 == 0 ) {
if ( year % 100 == 0 ) {
if ( year % 400 == 0) {
System.out.println ( year + " is a leap year." );
}
else {
System.out.println ( year + " is not a leap year." );
}
}
else {
System.out.println ( year + " is a leap year." );
}
}
else {
System.out.println ( year + " is not a leap year." );
}

}
}
}


*edited to add the code tag... much easier to view... much easier to cut & paste
please post your code code.gif

This post has been edited by pbl: 13 Oct, 2008 - 08:06 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Stuck At Loops
13 Oct, 2008 - 08:10 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
If you want to do it 5 times you need kind of a loop to to it 5 times

java

import java.util.Scanner;

public class LeapYearApp {


public static void main (String[] args){

Scanner input = new Scanner( System.in );


int year;


for(int i = 0; i < 5; i++) {

System.out.print( "Please enter a year. " );
year = input.nextInt();



if ( year % 4 == 0 ) {
if ( year % 100 == 0 ) {
if ( year % 400 == 0) {
System.out.println ( year + " is a leap year." );
}
else {
System.out.println ( year + " is not a leap year." );
}
}
else {
System.out.println ( year + " is a leap year." );
}
}
else {
System.out.println ( year + " is not a leap year." );
}

}
}
}



User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 05:05AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month