function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
new_bienew_bie 

How to write a apex class print 1 to 10 numbers ?

Hi

 

i am new to salesforce how to print 1 to 10 numbers in salesforce.
can you please give me Apex class and testclass for this.

 

i wrote a class

 

public class printTenNumbers{
public integer num{get;set;}
public integer print(){
for(num=0;num<=10;num++){
system.Debug(num);
}
return num;
}
}

 

how to check whether it is working or not.

 

Regards.

Vinit_KumarVinit_Kumar

Go to Developer Console and run the below code :-

 

Integer num;

 

for( num=0;num<=10;num++){
system.Debug(num);
}

new_bienew_bie
Hi vinit_kumar,

Thanks for immediate response.

your code is working fine.

but i type

public class printTenNumbers{
public integer num{get;set;}
public integer print(){
for(num=0;num<=10;num++){
system.Debug(num);
}
return num;
}
}

above code it is not working can you explain what is reason?

in generally writing an apex class is there any rules is there.

Regards.
Eugene NeimanEugene Neiman

First drop the class into Apex classes.   Here's my version (simpler)

 

public class printTenNumbers{

public static void print(){
for(integer num=0;num<=10;num++){
system.Debug(String.valueof(num));
}
}
}

 

Set yourself up for debug logging then go to the developer console and execute

 

printTenNumbers.print();

 

The debug log will display 1-10 as User Logs

 

11:10:09.047 (47697000)|USER_DEBUG|[5]|DEBUG|1
11:10:09.047 (47767000)|USER_DEBUG|[5]|DEBUG|2
11:10:09.047 (47828000)|USER_DEBUG|[5]|DEBUG|3
11:10:09.047 (47888000)|USER_DEBUG|[5]|DEBUG|4
11:10:09.047 (47949000)|USER_DEBUG|[5]|DEBUG|5
11:10:09.048 (48011000)|USER_DEBUG|[5]|DEBUG|6
11:10:09.048 (48117000)|USER_DEBUG|[5]|DEBUG|7
11:10:09.048 (48178000)|USER_DEBUG|[5]|DEBUG|8
11:10:09.048 (48238000)|USER_DEBUG|[5]|DEBUG|9
11:10:09.048 (48298000)|USER_DEBUG|[5]|DEBUG|10

 

How's that?

 

 

 

 

Thomas DvornikThomas Dvornik
new_bie, you class works fine. Just execute the following:

new printTenNumbers().print();

If you want to use printTenNumbers.print(); then change your method to static.
new_bienew_bie
Hi Eugene,

Your code is working Fine.

why you use static method. If i am not using the static method how to execute this one.
Is there any rules for execution of class static is mandatory can you explain clearly.

i want to print a integer num why you use a String.Valueof(num) alredy number is integer.

Regards.
Vinit_KumarVinit_Kumar

newbie,

 

If you want to excute your code in developer console,try below :-

 

printTenNumbers pn = new printTenNumbers();

pn.print();

new_bienew_bie
Hi

how to write a test class for this code

public class printTenNumbers{
public integer num{get;set;}
public integer print(){
for(num=0;num<=10;num++){
system.Debug(num);
}
return num;
}
}

Teach me...

Regards
Malika Pathak 9Malika Pathak 9

Hi new_bie

public class PrintNumber{
public static void printNumber()
{
for(integer i=1;i<=10;i++)
{
System.debug("number --> "+i);
}
}

If you find this helpful mark it as the best answer.