המשימה הייתה:
כתוב פעולה המקבלת כקלט מספר טבעי ומחזירה 1 אם כל ספרותיו שונות זו מזו, או 0 אחרת.
קוד PHP:import java.util.Scanner;
public class LastMission {
/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Type number"); //instruction for the user
int num = in.nextInt(); //gets the number that the user put into var "num"
int varNum=0; // the variable that creates the space in the array which keeps the "num"'s digits
int[] digits = new int[varNum]; //creates the array that keeps the "num"'s digits
int fNum=1; //we'ill use that variable to find out each digit of "num"
int a=0;
for(int i=1; i<=num; i=i*10) //checking if "i" is lower than "num" and if not multiply "i" by 10. It helps to find the digits range where "num" is.
{
varNum++; //the numbers of the cells that will be in the array (digits) who will keep the digits of "num"
fNum*=10; //we will use that var to find out the first digit of "num" by division "num" in "fNum"
if(num>=i && num<(i*10-1)) //every round of the loop he checks if "num" is in the range. for example: checks if "num" between 1-9, 10-99, 100-999 etc.
{
for(int j=0; j<varNum; j++) //feels the array, "j" is the cell number
{
[B] digits[j] = num/fNum;[/B] //the first number of "num" goes into "digits[j]", "num" is changing and "j" is going up by 1.
num = num - digits[j]*fNum; // "num" equal to itself less the first digit. for example: in the beggining "num=352", after this line "num=352-3*100=52".
fNum /=10; //divide "fNum" by 10 because we want to find the first digit by dividing "num" by "fNum".
}
}
}
for(int k=0; k<varNum; k++)
{
if(digits[k]!=digits[k+1])
{
System.out.println("0");
}
a++;
}
if(a==varNum)
{
System.out.println("1");
}
}
}
השגיאה שאני מקבל היא:הדגשתי את שורה 28, אז מה הבעיה?קוד PHP:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at LastMission.main(LastMission.java:28)
תודה מראש![]()



ציטוט ההודעה

