如何在Android Studio中生成随机字母

时间:2019-02-22 20:32:42

标签: java android

我正在制作一个随机字符生成应用程序,它会随机生成字母A-Z。该应用程序将创建一个自定义服务(从服务扩展),以使用新线程(从我的老师的指导中不应该使用应用程序主线程)开始,每隔一秒钟从A到Z生成一个随机字符。该服务可以绑定到应用程序的MainActivity,以便可以在屏幕上显示随机生成的字符。我设法使该应用程序正常运行,并且希望它可以生成随机字母,但是却可以生成随机数字。 布局:app layout image 代码:

package com.example.RandomCharGen;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.Random;


public class RandomCharacterService extends Service
{
private int myRandomCharacter;
private boolean isRandomGeneratorOn;

private final int MIN = 65;
private final int MAX = 90;

private final String TAG = "Random Char Service: ";

private final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

class RandomCharacterServiceBinder extends Binder{
    public RandomCharacterService getService()
    {
        return RandomCharacterService.this;
    }
}

private IBinder myBinder = new RandomCharacterServiceBinder();

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.i(TAG, "In OnStartCommand Thread ID is "+Thread.currentThread().getId());
    isRandomGeneratorOn = true;

    new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            startRandomGenerator();
        }
    }
    ).start();

    return START_STICKY;
}

private void startRandomGenerator()
{
    while(isRandomGeneratorOn)
    {
        char alphabet = 'A';
        for (int i = 65; i < 90; i++)
        {
            try
            {
                Thread.sleep(1000);
                if(isRandomGeneratorOn)
                {
                    alphabet++;
                    myRandomCharacter = new Random().nextInt(MAX)+MIN;
                    Log.i(TAG, "Thread ID is "+Thread.currentThread().getId() + ", Random character is "+myRandomCharacter);
                }
            }
            catch(InterruptedException e)
            {
                Log.i(TAG, "Thread Interrupted.");
            }

        }

    }

}

private void stopRandomGenerator()
{
    isRandomGeneratorOn = false;
}

public int getRandomCharacter()
{
    return myRandomCharacter;
}

@Override
public void onDestroy()
{
    super.onDestroy();
    stopRandomGenerator();
    Log.i(TAG, "Service Destroyed.");
}

@Nullable
@Override
public IBinder onBind(Intent intent)
{
    Log.i(TAG, "In onBind ...");
    return myBinder;
}

}

5 个答案:

答案 0 :(得分:2)

您可以执行此操作以生成随机

Random rnd = new Random();    
Char randomChar = alphabet.charAt(rnd.nextInt(alphabet.length()));

它将Char存储在randomChar变量中,因此现在您可以打印它了。

 Log.i(TAG, "Thread ID is "+Thread.currentThread().getId() + ", Random character is "+randomChar);

您的问题

问题是您的getRandomCharacter()返回了int,所以这就是为什么打印数字的原因。

public int getRandomCharacter()
{
    return myRandomCharacter; //This is an int not a char
}

如果必须遵循这种方式,唯一可以做的就是将其解析为char形式。

char randomChar = (char)getRandomCharacter();

您可以按照之前的说明进行Log

答案 1 :(得分:0)

更改您的类型

Log.i(TAG, "Thread ID is "+Thread.currentThread().getId() + ", Random character is "+myRandomCharacter);

 Log.i(TAG, "Thread ID is "+Thread.currentThread().getId() + ", Random character is "+(char)myRandomCharacter);

如果myRandomCharacter是int类型,则在使用它时必须将其强制转换为char。 这个问题有更多信息: Java - char, int conversions

答案 2 :(得分:0)

这必须被纳入您的代码中。...

但是..

final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// convert the string to a char array
char[] alphabetArr = alphabet.toCharArray();

这与... ['a','b','c' ... ]

// create an instance of random.
Random random = new Random();
//set the max number for the int to be the length of the string.
int randomInt = random.nextInt(alphabet.length());
//print it
System.out.println(randomInt + " :: " + alphabetArr[randomInt]);

我运行它并得到了它。

  

10 :: K

它的工作方式是从0到25(1-26)中获得一个随机数 然后,我获取该随机数,并在数组的索引处获取值。

您现在只需将其转换为每秒运行一次即可。...

答案 3 :(得分:0)

您只需要投射它?

 int j = 65;
 char c = (char)j;
 System.out.println(c);

打印A。

答案 4 :(得分:0)

您可以在Java编程中使用

导入java.util.UUID;

id = UUID.randomUUID()。toString();

这可以在Android Studio中实现

公共静态随机RANDOM = new Random();

public static String randomString(int len) {
    StringBuilder sb = new StringBuilder(len);

    for (int i = 0; i < len; i++) {
        sb.append(DATA.charAt(RANDOM.nextInt(DATA.length())));
    }

    return sb.toString();
}