//Can\'t seem to get this to compile. Please help. import java.util.*; public cl
ID: 3533366 • Letter: #
Question
//Can't seem to get this to compile. Please help.
import java.util.*;
public class PalinArray
{
public static void main(String args[])
{
Random r = new Random();
int even = 0;
int odd = 0;
int[] p = new int[25];
for (int i=0; i<25; i++)
p[i] = palin(r);
System.out.println("The numbers are:");
for(int i = 0; i<25; i++)
{
if (p[i]%2 == 0)
even++;
else
odd++;
System.out.print(p[i]+" ");
if ((i+1) == 0)
System.out.println();
}
System.out.println(" " + even + "are even " + odd + "are odd");
}
public static int palin(Random r)
{
int n;
do
{
n=r.nextInt(90000) + 10001;
}
while
(!ispalin(n));
return n;
}
public static boolean ispalin(int n)
{
if(n!=n/10000)
return false;
if(n/10 == n/1000)
return true;
return false;
}
}
Explanation / Answer
Your code is complied but it is in infinite loop
The random number you are generating is of large range. Reduce the range so that u can check
Please rate if it helps;
-----------------------------------------------
package a;
import java.util.*;
public class PalinArray
{
public static void main(String args[])
{
Random r = new Random();
int even = 0;
int odd = 0;
int[] p = new int[25];
System.out.println("The numbers are:");
for (int i=0; i<25; i++)
p[i] = palin(r);
for(int i = 0; i<25; i++)
{
if (p[i]%2 == 0)
even++;
else
odd++;
System.out.print(p[i]+" ");
if ((i+1) == 0)
System.out.println();
}
System.out.println(" " + even + "are even " + odd + "are odd");
}
public static int palin(Random r)
{
int n;
do
{
n=r.nextInt(1000) + 10001;
System.out.println(n);
}
while
(!ispalin(n));
return n;
}
public static boolean ispalin(int n)
{
if(n!=n/10000)
return false;
if(n/10 == n/1000)
return true;
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.