我是Java的新手,我的代码需要一些帮助。
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class asdf {
public static void main(String[] args)
{
Random generator = new Random();
int num1;
String theWord;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to rearrange");
theWord = in.nextLine();
int length = theWord.length();
Random times=new Random();
System.out.println("Length of String:"+length);
char[] chars=theWord.toCharArray();
Arrays.sort(chars);
int timez=times.nextInt();
num1=generator.nextInt(length);
char c=theWord.charAt(num1);
String newWord=new String(chars);
for(int i=0;i+=1;i<timez)
{
newWord=newWord.replace(c, '*');
}
System.out.println(newWord);
}
}
所以我试图重新排列字符串并用&#39; *&#39;替换一些字符,但它没有用,我也不知道如何解决它。< / p>
答案 0 :(得分:0)
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class asdf {
public static void main(String[] args) {
Random generator = new Random();
int num1;
String theWord;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to rearrange");
theWord = in.nextLine();
int length = theWord.length();
Random times=new Random();
System.out.println("Length of String:"+length);
char[] chars=theWord.toCharArray();
Arrays.sort(chars);
int timez=times.nextInt();
num1=generator.nextInt(length);
char c=theWord.charAt(num1);
String newWord=new String(chars);
for(int i=0;i<timez;i++) {
newWord=newWord.replace(c, '*');
}
System.out.println(newWord);
}
}
Ideone示例:http://ideone.com/PjfAor
答案 1 :(得分:0)
问题在于import glob
filenames = glob.glob(os.path.join(dirname,'*.jpg'))
循环。您应该按如下方式更改循环:
for
如果您感兴趣,这是实现您想要实现的目标的另一种更清洁的方式:
for (int i = 0; i < timez; i++)
<强>输入:强>
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char[] input = in.nextLine().toCharArray();
Random rand = new Random();
for (int k = 0; k < 5; k++) {
int leftIndex = rand.nextInt(input.length - 1);
int rightIndex = rand.nextInt(input.length - 1);
if (leftIndex == rightIndex)
continue;
char buffer = input[leftIndex];
input[leftIndex] = input[rightIndex];
input[rightIndex] = buffer;
}
System.out.println(input);
in.close();
}
<强>输出:强>
mango