在印刷线上选择一个位置,java 字符串

choose a position on printed wire, java string

我正在尝试让我的程序 print(command window) this> "------------" 的任何长度从 1 - 100:

public void display()
   {
       for (int x=0; x < Length; x++)
       {
           System.out.print("-");
       }
   }

但是,我需要一个 "bug" 来降落在这条线上的某个地方:例如:----0---- 并且能够记住那个位置并沿着电线移动。我不是在寻求解决此问题的答案,而是帮助查找和阅读类似的关键字。

感谢您的帮助!

实现此目的的一种方法是使用指定错误位置的参数调用方法。这样你就知道错误的位置了。

public void display(int position)
   {
       for (int x=0; x < Length; x++)
       {
            if (x == position) {
                System.out.print("O");
            }
            else {
                System.out.print("-");
            }
       }
   }

However, I need a "bug" to land somewhere on this wire

您可以按如下方式使用Random

public static void main (String[] args) throws Exception
{
    Random rnd = new Random();
    int randomNumber = rnd.nextInt(100);

    char[] chr = new char[100];
    Arrays.fill(chr,'-');
    chr[randomNumber] = '0';

    System.out.print(new String(chr));
}

这里,randomNumber为大家守住bug的位置