A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘长辉 中级黑马   /  2013-7-2 10:46  /  2030 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Just_Only 于 2013-7-2 13:00 编辑
  1. import java.util.*;
  2. class DemoArray6 {
  3. public static void main(String []args) {
  4.   int[]x=new int[500];
  5.   Arrays.fill(x,1);
  6.   kill(x);
  7.   System.out.println(getIndex(x));
  8. }
  9. public static void kill(int[]x) {
  10.   int sum=x.length;
  11.   int count=0;
  12.   while(true) {
  13.    for(int i=0;i<x.length;i++) {
  14.     if(x==1) {
  15.      count++;
  16.     }
  17.     if(count==3) {
  18.      x=0;
  19.      sum--;
  20.      count=0;
  21.     }
  22.     if(sum==1) {
  23.      System.out.println("last killed"+i);
  24.      return;
  25.     }
  26.    }
  27.   }
  28. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 神马都是浮云

查看全部评分

6 个回复

正序浏览
。子伤。 发表于 2013-7-2 23:09
我看了5min真的看不懂,你的目的。我在猜测,你要要用fill(int[] a, int val)将指定的 int 值分配给指定 in ...

  1. import java.util.*;

  2. class DemoArray8 {

  3. public static void main(String []args) {

  4.   int[]x=new int[1];

  5.   Arrays.fill(x,1);

  6.   kill(x);

  7. }

  8. public static void kill(int[]x) {

  9.   int sum=x.length;

  10.   int count=0;

  11.   while(true) {

  12.    for(int i=0;i<x.length;i++) {

  13.     if(x[i]==1) {

  14.      count++;

  15.     }

  16.     if(count==3) {

  17.      x[i]=0;

  18.      sum--;

  19.      count=0;

  20.     }

  21.     if(sum==1) {

  22.      System.out.println("last killed"+i);

  23.     }

  24.    }

  25.   }

  26. }
  27. }
复制代码
怎么死循环了,怎么输出最后剩第几位
回复 使用道具 举报
改个小错误都的话费很久的时间啊!!
回复 使用道具 举报
首先十分感谢你的耐心解答,这个类似于约瑟夫问题,每次查到第三个就出圈,问最后一个是第几个??  刚学编程代码写的很烂,也只是照着写而已,许多细节不是很清楚,一直没研究明白,就连getIndex方法调用都不知道怎么改!!感觉没学这个的天赋啊!!!
回复 使用道具 举报
  1. package text6_29;
  2. /**
  3.     @author 这个程序修改确实蛋疼,我看了几分钟,确实看不出你想呀做的!我已经把你的错误写出来了。
  4. */
  5. import java.util.Arrays;
  6. class DemoArray6 {
  7. public static void main(String []args) {
  8.   int[]x=new int[500];
  9.   Arrays.fill(x,1);  //fill(int[] a, int val)将指定的 int 值分配给指定 int 型数组的每个元素。
  10.   kill(x);
  11.   System.out.println(getIndex(x));
  12. }
  13. public static void kill(int[]x) {
  14.   int sum=x.length;
  15.   int count=0;
  16.   while(true) {
  17.    for(int i=0;i<x.length;i++) {
  18.     if(x==1) {    //x是数组名,不能这样写。
  19.      count++;
  20.     }
  21.     if(count==3) {
  22.      x=0;
  23.      sum--;
  24.      count=0;
  25.     }
  26.     if(sum==1) {
  27.      System.out.println("last killed"+i);
  28.          //return;既然你的kill(int[]x)的返回值是void ,那么就没必要有返回值。
  29.     }
  30.    }
  31.   }
  32. }
复制代码
我看了5min真的看不懂,你的目的。我在猜测,你要要用fill(int[] a, int val)将指定的 int 值分配给指定 int 型数组的每个元素),让后再把它打印出来。
给个小建议:以后发错误,希望把你的目的描述清楚!!

评分

参与人数 1技术分 +1 收起 理由
特殊服务 + 1

查看全部评分

回复 使用道具 举报
楼主你的代码错的比较多
首先:你的问题  找不到符号 System.out.println(getIndex[x]);这段代码根本没有在源程序出现么,应该是
System.out.println(getIndex(x))吧,这是小错误。
另外就算是System.out.println(getIndex(x)),在程序中也是错的我把错的地方给你在你源代码上表示出来了
希望能帮倒你
  1. package thread;

  2. import java.util.*;
  3. class DemoArray6 {
  4. public static void main(String []args) {
  5.   int[]x=new int[500];
  6.   Arrays.fill(x,1);
  7.   kill(x);
  8.   System.out.println(getIndex(x)); //这里边调用方法的话 是默认调用类DemoArray6中的静态方法,你没有定义getIndex()这个方法所以,找不到
  9. }
  10. public static void kill(int[]x) {
  11.   int sum=x.length;
  12.   int count=0;
  13.   while(true) {
  14.    for(int i=0;i<x.length;i++) {
  15.     if(x==1) { //这里怎么能直接把数组拿过来与整形变量来比较呢?改为x[i]
  16.      count++;
  17.     }
  18.     if(count==3) {
  19.      x=0;//与上面一样,改为x[i]
  20.      sum--;
  21.      count=0;
  22.     }
  23.     if(sum==1) {
  24.      System.out.println("last killed"+i);
  25.      return;
  26.     }
  27.    }
  28.   }
  29. }
  30. }//最后还少了个大括号
复制代码
至于你的代码到底是要达到什么目的,我就没仔细去看了,先把代码改到能运行才是关键

评分

参与人数 1技术分 +1 收起 理由
特殊服务 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马