class Test
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++)
{
String mark = "";
if (i % 2 == 0)
mark = "*";
System.out.println(i + mark);
}
}
}
另一种方法是,声明在循环外部:
Java code
class Test
{
public static void main(String[] args)
{
String mark;
for (int i = 1; i <= 10; i++)
{
mark = "";
if (i % 2 == 0)
mark = "*";
System.out.println(i + mark);
}
}
}