将C ++转换为Java,如何定义数组

时间:2021-05-03 22:28:23

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

我正在使用NetBeans IDE来运行和编译Java。

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

这是我用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:

我想打印出变量i,当它与它所比较的​​值不一样时。这是我的C ++代码:

#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?

如果我在Java中声明它应该是这样的吗?

int v[] = new int [5000];

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

当我试着写:if(!v [i])时,它显示错误。 Java处理这种方式的方式有何不同?

1 个解决方案

#1


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++?

C ++代码似乎与评论不匹配。你知道这就是你想要的C ++吗?

Here's how to declare that array in Java:

以下是如何在Java中声明该数组:

int [] v = new int[5000];

This will create an array of 5000 zeros.

这将创建一个5000个零的数组。

I think it should be written this way in Java:

我认为它应该用Java编写:

//output any value i that is not the same as v[i]
if(i != v[i]) { 
    System.out.println(i);
}

#1


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++?

C ++代码似乎与评论不匹配。你知道这就是你想要的C ++吗?

Here's how to declare that array in Java:

以下是如何在Java中声明该数组:

int [] v = new int[5000];

This will create an array of 5000 zeros.

这将创建一个5000个零的数组。

I think it should be written this way in Java:

我认为它应该用Java编写:

//output any value i that is not the same as v[i]
if(i != v[i]) { 
    System.out.println(i);
}