-1

I am using the NetBeans IDE to run and compile Java.

This is a piece of code that I have made with C++.

I want to print out the variable i when it's not the same as the value it's compared to. This is my C++ code:

#include <iostream>
#include <string>

int v[5000];
int main(void) {
    int check_self = 0;

    for(int i = 1; i < 5000; ++i) {
        //output any value i that is not the same as v[i]
        if(!(v[i])) std::cout << i << '\n';

        check_self = i + (i%10) + (i/10)%10 + (i/100)%10 + (i/1000)%10;
        v[check_self] = 1;
        enter code here
    }

    return 0;
}

If I declare this in Java should it be like this?

int v[] = new int [5000];

When I tried to write: if ( ! v [ i ] ), it shows an error. Is the way Java handles this different?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Dante26
  • 31
  • 1
  • 5

1 Answers1

1

Your comment is confusing:

//output any value i that is not the same as v[i]
if(!(v[i])) std::cout << i << '\n';

The C++ code doesn't appear to match the comment. Do you know that this is what you want in C++?

Here's how to declare that array in Java:

int [] v = new int[5000];

This will create an array of 5000 zeros.

I think it should be written this way in Java:

//output any value i that is not the same as v[i]
if(i != v[i]) { 
    System.out.println(i);
}
duffymo
  • 305,152
  • 44
  • 369
  • 561