create a batch file named IFLOOP.BAT that uses the IF statement and a counter to
ID: 3769606 • Letter: C
Question
create a batch file named IFLOOP.BAT that uses the IF statement and a counter to control the number of times a loop is executed. The number of times the loop must be executed depends on the current month. If the current ,onth is June, the loop must execute 6 times. The value of the current month must be extracted from the system date. Each interation of the loop must set the months name in a variable named "monthname" and must display the value of the counter as well as the value of the "monthname" variable. Example if month is Februaury it will display :
1 January
2 February
Explanation / Answer
Create a class named Loop.java
class Loop
{
public static void main(String args[])
{
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
System.out.println(cal.get(Calendar.MONTH));
Date d = new Date(timestamp );
System.out.println(d);
int month=cal.get(Calendar.MONTH);
int counter=0;
String monthName=null;
if(month==1)
monthName="January";
else if(month==2)
monthName="February";
else if(month==3)
monthName="March";
else if(month==4)
monthName="April";
else if(month==5)
monthName="May";
else if(month==6)
monthName="June";
else if(month==7)
monthName="July";
else if(month==8)
monthName="August";
else if(month==9)
monthName="September";
else if(month==10)
monthName="October";
else if(month==11)
monthName="November";
else if(month==12)
monthName="December";
for(int i=0;i<month-1;i++)
{
counter=counter+1;
}
System.out.println("Number of times Loop Executed"+counter);
System.out.println("Month Name:" + monthName);
}
}
1. Create this file.
2. javac Loop.java
3. java Loop
Now you can run your .bat file. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.