English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

For my assignment I need to make a class for a program. In the test program I see an "i". Does it stand for int, or is it an instance or something, that stands for invalid?

//***********************************************
// Month Class Demo Program 1 *
//***********************************************

public class MonthDemo1
{
public static void main(String[] args)
{
// Use the default constructor.
Month m = new Month();
System.out.println("Month " + m.getMonthNumber() +
" is " + m);
// Set the month number to the values 0 through 12
// (0 is invalid), and display the resulting month name.
for (int i = 0; i <= 12; i++)
{
m.setMonthNumber(i);
System.out.println("Month " + m.getMonthNumber() +
" is " + m);
}
}
}

2006-08-03 06:41:51 · 7 answers · asked by quackerjackets 1 in Computers & Internet Programming & Design

7 answers

for (int i = 0; i <= 12; i++)
{
...
...
}

the int i = 0; creates a integer variable that is valid only in that for-loop from the declaration till the terminating bracket of the for-loop. i is a temporary variable to count the months from 1 to 12.

but there is an error - that year contains 13 months, the for statement has to be written either
for (int i = 1; i <= 12; i++) // 1..12
or
for (int i = 0; i < 12; i++) // 0..11
depending on how months are denoted in Java

2006-08-03 07:42:29 · answer #1 · answered by frime 6 · 0 0

OK, I DO not have very good Java knowledge yet, however I do know C++ decently well, anyway if you are referring to the line

for (int i = 0; i <= 12; i++)

int i (would be a variable name/declaration/assignment...i is declared on this line as an integer, and its value is SET to 0)
;(statement terminator)
i<= 12 (loop until i is greater than or equal to 12)
;(statement terminator)
i++ (increment operator, this will INCREASE the value of i by 1 each time the loop executes.

Yes...be afraid....be vary afraid.

2006-08-03 13:48:20 · answer #2 · answered by D 4 · 0 0

Yes, "i" is an int and it is defined in the for statement that is coded as follows"

for (int i = 0; i <= 12; i++)


The code is actually doing 3 things, which are:

1. Lets go into a loop but I want to keep track of how many times to loop, so lets create an int and let's name it "i". (this is where the code reads "int i = 0")

2. Now, keep looping until "i" is less than or equal to 12 (this is where the codes reads "i <= 12")

3. And each time we loop, let's increase value of "i" by one (this is where the code reads "i++")

Hope this helps.

2006-08-03 16:36:40 · answer #3 · answered by tmo419 1 · 0 0

this line: "for (int i = 0; i <= 12; i++)" defines "i" as an int.

this is how a "for.. ..next" loop works:

for ( ( set your variable to the initial value); (set the condition for the loop); (set the increment value))

They are setting i equal to 0. The loop will continue as long as i is less than or equal to 12. The loop will add 1 to i each time it completes the loop. (i++ is the same as i+1)

2006-08-03 14:33:04 · answer #4 · answered by thegooddeal2000 2 · 0 0

if you are talking about down in the for loop - they are declaring "i" as a variable of the type "int"(integer)

the for statement reads
for(int i = 0; i <= 12; i++)
{
...
}

for([declare integer "i" and give a starting value of 0]; [do while value of i is lessthan or equal to 12]; [increment i by 1 after every pass])
{
...
}

2006-08-03 13:48:21 · answer #5 · answered by . 3 · 0 0

Generally most of the programmers define the looping variables as "i,j,k" instead of giving meaningful variable name, in this case, the programmer could have given the variable name as (int month_no=0; month_no<=12; month_no++)

2006-08-03 14:21:58 · answer #6 · answered by Pands 2 · 0 0

HI ,
This is sree Lakshmi.I read ur question & conformed u r in confussion that can i=int in java.But answer for that problem is it can't ,why bcoz int is a data type & it can't possible 2 initialize a datatype to a variable(i.e; i)It is possible if u declare like this i.e int i=10;it is nothing but the variable i is here of integer type.Ok clear ALL THE BEST..

2006-08-03 13:55:02 · answer #7 · answered by sree l 1 · 0 0

fedest.com, questions and answers