public class randomdemo {
public static void main(String[] args)
{
Random rand = new Random();
int [] varArray = new int[rand.nextInt(10)];
System.out.println(varArray.length);
int d = rand.nextInt(1999)-1000;
for (int i=0;i<varArray.length;i++)
{
varArray[i]= rand.nextInt(1999)-1000;
System.out.println(varArray[i]);
if(d==varArray[i])
{
System.out.println(d);
System.out.println(i+1);
}
else
{
System.out.println(d);
System.out.println(0);
}
}
}
}
Problems in code:
It executes the if-else statement multiple times and displays the if-else output multiple times since it is in for loop. The code should execute the if-else statement only once but the rest of the for loop should be executed multiple times. Since the if statement is using value of varArray[i]
I cannot exclude the code from the for loop. When break
statement is used it is terminating the for
loop. and the complete output is not shown.
output: currently
7 -710 -249 0 -693 -249 0 172 -249 0 -488 -249 0 -48 -249 0 955 -249 0 869 -249 0
As you can see the length of array is 7 it displays array element then value of variable d and value 0 in a loop.
expected output:
7 -710 -693 172 -488 -48 955 869 -249 0
the output of array with 7 elements should be 7 array values followed by variable d and 0.
Please identify the exact scenario in which you want to execute the if-else and make changes in the existing if-else condition so that it executes only once in the loop. Or you can wrap the existing if-else inside other if-else,for which the condition can be the scenario in which you have to execute the inner if-else.
For eg: Let the scenario is like only for a particular value of i(say when i=10) you want to execute the loop.Please modify the code as below.
if(i==10){
if(d==varArray[i])
{
System.out.println(d);
System.out.println(i+1);
}
else
{
System.out.println(d);
System.out.println(0);
}
}
May be you can set a boolean flag to track execution of your if else code .. see code below.
boolean flag = false; // flag to track the if else
System.out.println(varArray.length);
int d = rand.nextInt(1999)-1000;
for (int i=0;i<varArray.length;i++)
{
...
if(!flag){
if(d==varArray[i])
{
flag =true;
System.out.println(d);
System.out.println(i+1);
}
else
{
flag =true;
System.out.println(d);
System.out.println(0);
}
}
Try this.
public class randomdemo {
public static void main(String[] args) {
Random rand = new Random();
int[] varArray = new int[rand.nextInt(10)];
System.out.println(varArray.length);
int d = rand.nextInt(1999) - 1000;
int foundIdx = -1; // mark index if you find one!
for (int i = 0; i < varArray.length; i++) {
varArray[i] = rand.nextInt(1999) - 1000;
System.out.println(varArray[i]);
if (d == varArray[i]) {
foundIdx = i + 1; // only the last match is saved
}
}
if (foundIdx != -1) {
System.out.println(d);
System.out.println(foundIdx);
} else {
System.out.println(d);
System.out.println(0);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.