Java 在字符串数组中的随机索引处生成星号
我很难理解编程逻辑。如何让字符串数组以随机索引打印星号。请注意,3是要在数组中生成的星号数 因此,输出可以是[a,b*,c*,d,e*,f,g,h]或[a*,b,c,d,e,f,g*,h*]Java 在字符串数组中的随机索引处生成星号,java,Java,我很难理解编程逻辑。如何让字符串数组以随机索引打印星号。请注意,3是要在数组中生成的星号数 因此,输出可以是[a,b*,c*,d,e*,f,g,h]或[a*,b,c,d,e,f,g*,h*] public class Generate { public static void main(String[] args) { String[] list = {"a","b","c","d","e","f","g","h"}; for(i
public class Generate
{
public static void main(String[] args)
{
String[] list = {"a","b","c","d","e","f","g","h"};
for(int i =0; i <list.length; i++)
{
System.out.print(" " + list[i]);
generateAsterisk(list);
}
System.out.println();
}
public static void generateAsterisk(String[] list)
{
for(int i = 0; i < 3; i++)
{
int index = (int)(Math.random()*i);
}
System.out.print("*");
}
}
公共类生成
{
公共静态void main(字符串[]args)
{
字符串[]列表={“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”};
对于(int i=0;i你可以这样做:
import java.util.Random;
public class Generate {
public static void main(String[] args) {
String[] list = { "a", "b", "c", "d", "e", "f", "g", "h" };
//do it 3 times or change to as many times you want to add an asteriks
for (int i = 0; i < 3; i++) {
addRandomAsteriks(list);
}
//print the array
for (int i = 0; i < list.length; i++) {
System.out.print(" " + list[i]);
}
System.out.println();
}
public static void addRandomAsteriks(String[] list) {
Random rand = new Random();
int randomNumber = rand.nextInt(list.length - 1);
String string = list[randomNumber]; //get the string at the random index
if (!string.contains("*")) {
// add the asteriks
list[randomNumber] = string.concat("*");
}else {
//if it had already an asteriks go through the
//add-method again until you find one that has no asteriks yet.
addRandomAsteriks(list);
}
}
import java.util.Random;
公共类生成{
公共静态void main(字符串[]args){
字符串[]列表={“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”};
//做3次或更改为您想要添加星号的次数
对于(int i=0;i<3;i++){
addRandomAsteriks(列表);
}
//打印阵列
for(int i=0;i
这是一个比YassinHH的答案更面向对象的观点
此解决方案有效且仅使用数组。您只能调用一次generateAsteriks
方法,并使用所需的星号:
String[] list = {"a","b","c","d","e","f","g","h"};
generateAsterisks(list, 3);
...
并将您的GeneratorEasteriks
方法更改为:
public static void generateAsterisks(String[] list, int numAsteriks)
{
for(int i = 0; i < numAsteriks && i < list.length; i++)
{
int index = (int)(Math.random()*list.length);
//check if already added *
if(list[index].lastIndexOf('*') != list[index].length()-1) {
list[index] = list[index] + "*";
} else {
//don't count this loop iteration
i--;
}
}
}
publicstaticvoidgenerationeasterisks(字符串[]列表,int-numAsteriks)
{
对于(int i=0;i
Concat*到随机生成的索引值处的值。为什么第一个输出只有2个星号?在获得相同的索引两次后是否忽略它?很抱歉,这是一个错误。这可能会导致数组越界异常。当使用实际元素数作为最大值时,问题与yassinHH的答案相同。数组索引start为0,但8可能正在使用您的策略。数组索引越界异常不会发生,因为我总是使用索引变量访问数组,它的范围在0到列表。length-1
之间。但是如果'numAsteriks'大于列表。length
,则循环将成为无止境循环。我会解决这个问题。嘿,我有一个问题。可以在字符串数组上使用此代码。如果创建一个类对象并使用一个对象数组,代码会工作吗?int index=(int)(Math.random()*list.length);
int=8(int)(Math.random()*list.length)
返回0到列表之间的整数。长度-1
,因此,如果数组的长度为8,最大索引将为7。无法发生ArrayIndexOutOfBoundsException
异常。是否有方法消除随机数重复项?:)使用时似乎有问题。随机类LOL当再次生成随机数时,我们会检查是否有已添加的星号。因此它不会造成伤害。但是如果有很多碰撞(相同的数字),或者即使在最坏的情况下,我们也不会得到“空闲位置”代码运行无限。不发生冲突几乎是不可能的…使用数组和随机项查看本文: