gpt4 book ai didi

java - 设置页面-设置组合结果

转载 作者:行者123 更新时间:2023-12-01 09:01:41 25 4
gpt4 key购买 nike

我正在创建一个用于生成密码的应用程序的设置页面。在我的选项页面中,用户可以选择 5 个选项。

我想知道如何使我的代码比为每个可能的结果使用 if 更简单。我一开始有 3 个选项,这让事情变得很容易,但现在有太多的组合,代码变得困惑,我不知道如何更好地编写它。

下面的代码用于用户可以选择的三个选项:

主要 Activity Java

  btnGenPass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (SettingsPage.isSpecChecked == 1 && SettingsPage.isHighCaseChecked == 1 && SettingsPage.isNumbChecked == 1) {
char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&*()".toCharArray();
final StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < SettingsPage.passLength; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}

final String output = sb.toString();
passView.setText(output);

}else if (SettingsPage.isSpecChecked == 0 && SettingsPage.isHighCaseChecked == 1 && SettingsPage.isNumbChecked == 1) {
char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
final StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < SettingsPage.passLength; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}

final String output = sb.toString();
passView.setText(output);

}else if (SettingsPage.isSpecChecked == 0 && SettingsPage.isHighCaseChecked == 0 && SettingsPage.isNumbChecked == 1) {
char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
final StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < SettingsPage.passLength; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}

final String output = sb.toString();
passView.setText(output);

}else if (SettingsPage.isSpecChecked == 0 && SettingsPage.isHighCaseChecked == 0 && SettingsPage.isNumbChecked == 0) {
char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
final StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < SettingsPage.passLength; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
final String output = sb.toString();
passView.setText(output);

}else if (SettingsPage.isSpecChecked == 1 && SettingsPage.isHighCaseChecked == 0 && SettingsPage.isNumbChecked == 0) {
char[] chars = "abcdefghijklmnopqrstuvwxyz!£$%^&*()".toCharArray();
final StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < SettingsPage.passLength; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}

final String output = sb.toString();
passView.setText(output);

}else if (SettingsPage.isSpecChecked == 1 && SettingsPage.isHighCaseChecked == 0 && SettingsPage.isNumbChecked == 1) {
char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890!£$%^&*()".toCharArray();
final StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < SettingsPage.passLength; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}

final String output = sb.toString();
passView.setText(output);

}else if (SettingsPage.isSpecChecked == 0 && SettingsPage.isHighCaseChecked == 1 && SettingsPage.isNumbChecked == 0) {
char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
final StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < SettingsPage.passLength; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}

final String output = sb.toString();
passView.setText(output);
}
}
});

正如您所看到的,有许多语句具有所有可能的组合,但现在我添加了更多设置,有很多可能的结果组合。我相信有 32 种可能的组合。

最佳答案

你有很多重复的代码。唯一真正改变的是生成密码的字符集。您应该做的第一件事是将重复代码移至方法中。

private void generatePassword(char[] charset) {
final StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < passLength; i++) {
char c = charset[random.nextInt(charset.length)];
sb.append(c);
}

final String output = sb.toString();
passView.setText(output);
}

下一部分是处理字符集生成。为此,您可以创建变量来存储每个选项的字符集,例如数字、特殊字符、大写字母、小写字母等。然后根据打开/关闭的选项生成最终的字符集。

private char[] generateCharSet() {
String numbers = "0123456789";
String special = "!£$%^&*()";
String alphabetsLower = "abcdefghijklmnopqrstuvwxyz";
String alphabetsUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// Add lower alphabets by default
StringBuilder finalCharset = new StringBuilder(alphabetsLower);

// Add special chars if option is selected
if (isSpecChecked == 1) {
finalCharset.append(special);
}

// Add upper case chars if option is selected
if (isHighCaseChecked == 1) {
finalCharset.append(alphabetsUpper);
}

// Add numbers if option is selected
if (isNumbChecked == 1) {
finalCharset.append(numbers);
}

// build the final character set
return finalCharset.toString().toCharArray();
}

onClick 方法中调用这些方法。希望你能明白。

这是我用一个单元模拟的整个 java 类来测试功能。它是用 Netbeans IDE 编写的。祝你好运:)

PasswordGeneratorClass.java

import java.util.Random;

/**
*
* @author K Neeraj Lal
*/
public class PasswordGeneratorClass {

private static final int passLength = 40;

private static int isSpecChecked;
private static int isHighCaseChecked;
private static int isNumbChecked;

public static void main(String[] args) {

// Iterates from 000 to 111
for (int i = 0; i < 7; i++) {
unitTest(i);
}
}

/**
* Used for testing only
*
* @param test input
*/
private static void unitTest(int test) {
char[] arr = Integer.toBinaryString(test).toCharArray();

// turn on/off the options based
isSpecChecked = Integer.parseInt(Character.toString(arr.length > 0 ? arr[0] : '0'));
isHighCaseChecked = Integer.parseInt(Character.toString(arr.length > 1 ? arr[1] : '0'));
isNumbChecked = Integer.parseInt(Character.toString(arr.length > 2 ? arr[2] : '0'));

char[] charset = generateCharSet();
generatePassword(charset);
}

/**
* Generates character set
*
* @return character set
*/
private static char[] generateCharSet() {
String numbers = "0123456789";
String special = "!£$%^&*()";
String alphabetsLower = "abcdefghijklmnopqrstuvwxyz";
String alphabetsUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// Add lower alphabets by default
StringBuilder finalCharset = new StringBuilder(alphabetsLower);

// Add special chars if option is selected
if (isSpecChecked == 1) {
finalCharset.append(special);
}

// Add upper case chars if option is selected
if (isHighCaseChecked == 1) {
finalCharset.append(alphabetsUpper);
}

// Add numbers if option is selected
if (isNumbChecked == 1) {
finalCharset.append(numbers);
}

// build the final character set
return finalCharset.toString().toCharArray();
}

/**
* Generates the password based on character set
*
* @param charset the character set to generate the password from
*/
private static void generatePassword(char[] charset) {
final StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < passLength; i++) {
char c = charset[random.nextInt(charset.length)];
sb.append(c);
}

final String output = sb.toString();
// passView.setText(output);
System.out.println(output);
}
}

关于java - 设置页面-设置组合结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com