Identify and fix the errors in this code public class Testpu

Identify and fix the errors in this code: public class Test{public static void main(String[] args){nPrintln(\"Welcome to Java!\", 5);} public static void nPrintln(String message, int n){int n = 1; for(int i = 0; i

Solution

Answer:

public class Test
{
   public static void main(String[] args)
   {
       nPrintln(\"welcome to java\",5);
   }
   public static void nPrintln(String message,int n)
   {
int n=1;      
       for(int i=0;i<n;i++)
       {
           System.out.println(message);
       }
   }
}

In the given snippet of code the line int n=1 throws an error since n is already n is declared in the function public static void nPrintln(String message,int n),this can be fixed by making the line int n=1 as n=1 so we get the output as message \"welcome to java\" only once(one time) will be displayed.

Error Resolved Code :

public class Testss
{
   public static void main(String[] args)
   {
       nPrintln(\"welcome to java\",5);
   }
   public static void nPrintln(String message,int n)
   {
n=1;      
       for(int i=0;i<n;i++)
       {
           System.out.println(message);
       }
   }
}

If we want to print the message \"welcome to java\" for 5 times i.e upto completion of all the n values i.e 5 which is passed as argument then we must write the code as below:

public class Testss
{
   public static void main(String[] args)
   {
       nPrintln(\"welcome to java\",5);
   }
   public static void nPrintln(String message,int n)
   {
  
       for(int i=0;i<n;i++)
       {
           System.out.println(message);
       }
   }
}

 Identify and fix the errors in this code: public class Test{public static void main(String[] args){nPrintln(\
 Identify and fix the errors in this code: public class Test{public static void main(String[] args){nPrintln(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site