Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我试图在一张图片上洗牌或随机放置一个输入字符串,但我不知道该使用什么代码?_Java_String_Graphics_Shuffle - Fatal编程技术网

Java 我试图在一张图片上洗牌或随机放置一个输入字符串,但我不知道该使用什么代码?

Java 我试图在一张图片上洗牌或随机放置一个输入字符串,但我不知道该使用什么代码?,java,string,graphics,shuffle,Java,String,Graphics,Shuffle,有人知道我如何在水平轴和垂直轴上随机生成一个输入字符串到图片中吗 public static String change(Graphics B) { Scanner termIn = new Scanner(System.in); System.out.print("User, please give me a string to print:\n\t"); String in = termIn.nextLine(); String s1 = in;

有人知道我如何在水平轴和垂直轴上随机生成一个输入字符串到图片中吗

    public static String change(Graphics B) {
    Scanner termIn = new Scanner(System.in);
    System.out.print("User, please give me a string to print:\n\t");
    String in = termIn.nextLine();

    String s1 = in;
    String civic = s1.replace("car", "karr").replace("cool","crazy")
    .replace("the","teh").replace("red","blue").replace("kouki","cookie")
    .replace("nissan", "datsun").replace("s14","silvia").replace("drift","slide")
    .replace("laugh","LOL").replace("240sx", "sr20").replace("drive","shift");

    System.out.println(civic);
    B.drawString(civic, 60, 259);
    return s1;

在java中,您可以随机生成数字,然后使用这些数字从字典中选择字符串或单词

class RandomizeString {
    private String[] pronouns = new String[] {"he", "I", "We"};
    private String[] verbs = new String[]{"ate", "want", "drove"};
    private String[] cars = new String{"datsun", "toyota", "subaru"};

    public String randPronoun() { return pronouns[Math.random() * pronouns.length]; }
    public String randVerb() { return verbs[Math.random() * verbs.length]; }
    public String randCar() { return cars[Math.random() * cars.length]; }

    public String randomize(String in) {
       return in.replace("he", randPronoun()).replace("she", randomPronoun())
                .replace("flew", randomVerb()).replace("smashed",randomVerb())
                .replace("ford",randomCar()).replace("chevy",randomCar());
    }
}
现在您有了一个类,它唯一的任务就是随机化字符串。它将单词存储在私有数组中,有一组方法从这些数组中选择随机单词,还有一个方法使用这些方法在字符串参数中进行替换

如果您愿意,您可以为任何内容(动物、名称、国家、颜色等)添加新数组,以及获取随机值的方法,您还可以执行以下操作

 `in.replace(randomCar(), randomAnimal()).replace(randomVegie(), randomIceCream())`

在我们提供任何帮助之前,我们需要查看您已经编写的内容。我想在图片上随机生成一个输入字符串,但不确定要使用什么代码,很抱歉,这是我第一次可能的复制:它似乎只针对“给定”点,我希望它在图片中的随机点生成。知道吗,只是一个简单的问题,当我调用我的方法时,我是将随机生成的代码放入我的方法中,还是将其放入main中?