【问题标题】:Converting C++ into Java, how to define array [closed]将 C++ 转换为 Java,如何定义数组 [关闭]
【发布时间】:2016-04-25 13:37:39
【问题描述】:

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

这是我用 C++ 编写的一段代码。

当变量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;
}

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

int v[] = new int [5000];

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

【问题讨论】:

标签: java c++ arrays type-conversion


【解决方案1】:

您的评论令人困惑:

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

C++ 代码似乎与注释不匹配。你知道这就是你在 C++ 中想要的吗?

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

int [] v = new int[5000];

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

我认为应该用Java这样写:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2020-08-26
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    相关资源
    最近更新 更多