【问题标题】:Can you give me idea on how to code this program using array and for loops?你能告诉我如何使用数组和 for 循环来编写这个程序吗?
【发布时间】:2014-08-28 12:36:18
【问题描述】:

我应该使用 for 循环和数组来制作一个程序,现在输出程序需要显示类似这样的内容

0-1-0-0-1-0-0-0-1-0-0-0-0-1

请注意,0 会在 1 的节目之后添加,到目前为止,我坚持使用此代码,您能帮帮我吗...

int[] binary = new int[150];
int b = 0;
int x = 3;
for(int a = 0; a < binary.length; a=a+2) {
  box[a] =1;
}

for(int i =0; i < binary.length; i++){ 
  for(b = 0; b < binary.length; b = b+x){
    if(box[b] == 1) {
      box[b] =0;  // this condition changes the value of  1 to 0 if the binary is already "1".
    }
    else {
      box[b] =1; // if the value of the binary is 0 it changes it to 1.
    }
  }
  x++; // Putting this changes the value of the x making x=4 so that the next time the for loop runs it adds the int b to 4..(NOT SURE IF THIS IS RIGHT THOUGH)
}

for(int c =0; c < binary.length; c++) {
  System.out.print(binary[c]); // "this should print the output "
}

我的问题是让它显示这样的东西

01001000100001000001

【问题讨论】:

  • 当前输出是多少?
  • 你真的需要弄清楚你想要完成什么。
  • @xgeorgekx 10110111 像这样它最多 150 所以如果全部写下来它就太长了
  • @IvanLorenzo 我已经添加了答案

标签: java arrays for-loop


【解决方案1】:

您混合了多个变量名称,因为您将值分配给框,您可能应该删除 binary - 类似,

int[] box = new int[150];
int b = 0;
int x = 3;
for (int a = 0; a < box.length; a = a + 2) {
    box[a] = 1;
}

for (int i = 0; i < box.length; i++) {
    for (b = 0; b < box.length; b = b + x) {
        if (box[b] == 1) {
            box[b] = 0;
        } else {
            box[b] = 1;
        }
    }
    x++;
}

for (int c = 0; c < box.length; c++) {
    System.out.print(box[c]); // "this should print the output "
}

输出是

101101111011111101111111101111111111011111111111101111111111111101111111111111111011111111111111111101111111111111111111101111111111111111111111011111

【讨论】:

  • 就在 x++ 之前;你认为我应该在它的顶部添加.right b=0 只是为了将“b”的值重置为 0。?
  • @IvanLorenzo 我不这么认为,下一次循环迭代无论如何都会这样做。
【解决方案2】:

我不确定完全理解您想要什么。从您的帖子中,我只了解到您的目标是输出仅由 0 和 1 组成,并且以 0 开头,紧跟 1。 1 之后的零数依次增加 1:所以之后您有 2零后跟 1,然后 3 个零后跟 1,4 个零后跟 1,依此类推:0-1-00-1-000-1-0000-1。

我说的对吗?

如果这是您想要的,请尝试类似的方法:

    int num_zero = 1;
    int cptr = 0;
    int[] arr = new int[20];

    for(int j = 0; j < arr.length; j++){


        if(cptr == num_zero){

            arr[j] = 1;
            num_zero = 1+ cptr;
            cptr = 0;


        }else{

            arr[j] = 0;
            ++cptr;

        }
    }

这是输出:

01001000100001000001

【讨论】:

  • 实际上 cptr 用于存储在 1 之后添加的零的数量。当达到存储在 num_zero 中的正确数字时,您在 if 语句中输入,添加 1 并重置 cptr。
  • else条件下增加cptr的值是++cptr的意思吗?
【解决方案3】:

试试这个:

int[] binary = new int[150];
int onesNextIndex = 1;
int onesGap = 2;

for(int i = 0; i < binary.length; i++){
    if(i == onesNextIndex){
        binary[i] = 1;
        onesNextIndex += onesGap + 1;
        onesGap++;

    }else{
        binary[i] = 0;
    }   
}

您可以使用以下方法对其进行测试:

for(int x : binary){
    System.out.print(x);
}

它将打印:0100100010000100000100000010000000100000000..... etc

如果你想反过来的话:

int[] binary = new int[150];
int zerosNextIndex = 1;
int zerosGap = 2;

for(int i = 0; i < binary.length; i++){
    if(i == zerosNextIndex){
        binary[i] = 0;
        zerosNextIndex += zerosGap + 1;
        onesGap++;

    }else{
        binary[i] = 1;
    }

}

这将打印:101101110111101111101111110111111101111111101111.......etc

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-16
    • 2022-06-15
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2021-12-07
    相关资源
    最近更新 更多