我正在寻找一些我正在进行的项目的帮助。我是java的新手,我正在努力创建一个生成密码的函数。这可能有很多错误,或者这可能是完全错误的,所以请好好对新手&gt ;.<
import java.util.Random;
public class StillTesting {
public static void main(String[] args) {
System.out.println("Your new password is: " + generateValidPassword());
}
static private String generateValidPassword() {
String numcase = "";
String lowcase = "";
String upcase = "";
String halfpass = numcase.concat(upcase);
String returnString = halfpass.concat(lowcase);
System.out.print(returnString);
Random r = new Random();
String loweralphabet = "abcdefghijklmnopqrstuvwxyz";
int n = loweralphabet.length();
String upperalphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int N = upperalphabet.length();
int num = 0;
for (int i = 0; i < 3; i++) {
num = r.nextInt(9);
numcase = String.valueOf(num = r.nextInt(9));
return numcase;
}
for (int i = 0; i < 3; i++) {
lowcase = String.valueOf(loweralphabet.charAt(r.nextInt(n)));
return lowcase;
}
for (int i = 0; i < 3; i++) {
upcase = String.valueOf(upperalphabet.charAt(r.nextInt(N)));
return upcase;
}
return returnString;
}
}
答案 0 :(得分:0)
这是你在找什么?以下程序的输出类似于433raeWPV
和675croJWV
public static void main(String[] args) {
System.out.println("Your new password is: " + generateValidPassword());
}
static private String generateValidPassword() {
String pswd = "";
Random r = new Random();
String loweralphabet = "abcdefghijklmnopqrstuvwxyz";
int n = loweralphabet.length();
String upperalphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int N = upperalphabet.length();
int num = 0;
for (int i = 0; i < 3; i++) {
num = r.nextInt(9);
pswd += String.valueOf(num = r.nextInt(9));
}
for (int i = 0; i < 3; i++) {
pswd += String.valueOf(loweralphabet.charAt(r.nextInt(n)));
}
for (int i = 0; i < 3; i++) {
pswd += String.valueOf(upperalphabet.charAt(r.nextInt(N)));
}
return pswd;
}
答案 1 :(得分:0)
您可能想尝试在循环中删除返回语句,如下所示:
static private String generateValidPassword()
{
String numcase = "";
String lowcase = "";
String upcase = "";
String halfpass = "";
String returnString = "";
System.out.print(returnString);
Random r = new Random();
String loweralphabet = "abcdefghijklmnopqrstuvwxyz";
int n = loweralphabet.length();
String upperalphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int N = upperalphabet.length();
int num = 0;
for(int i=0;i<3;i++)
{
num=r.nextInt(9);
numcase = String.valueOf(num);
returnString += numcase;
}
for(int i=0;i<3;i++)
{
lowcase = String.valueOf(loweralphabet.charAt(r.nextInt(n)));
returnString += lowcase;
}
for(int i=0;i<3;i++)
{
upcase = String.valueOf(upperalphabet.charAt(r.nextInt(N)));
returnString += upcase;
}
return returnString;
}
这将为您提供类似637xiqHMR的输出。您将把每个循环生成的字母组合成一个字符串,然后在结束时返回整个字符串,而不是在第一个循环的第一次迭代时返回。
答案 2 :(得分:0)
好吧,确实有一些错误:
在分配numcase,upcase和lowcase
您在每个for循环的第一次迭代中返回一个值,第一个return语句将返回第一个numcase值。有关return语句如何工作的更多信息here
最后,永远不会到达returnString语句。
尝试这样的事情:
static private String generateValidPassword()
{
String numcase = "";
String lowcase = "";
String upcase = "";
Random r = new Random();
String loweralphabet = "abcdefghijklmnopqrstuvwxyz";
int n = loweralphabet.length();
String upperalphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int N = upperalphabet.length();
int num = 0;
for(int i=0;i<3;i++)
{
num=r.nextInt(9);
numcase += String.valueOf( num = r.nextInt(9));
}
for(int i=0;i<3;i++)
{
lowcase += String.valueOf(loweralphabet.charAt(r.nextInt(n)));
}
for(int i=0;i<3;i++)
{
upcase += String.valueOf(upperalphabet.charAt(r.nextInt(N)));
}
String halfpass = numcase.concat(upcase);
String returnString = halfpass.concat(lowcase);
System.out.print(returnString);
return returnString;
}
}
答案 3 :(得分:0)
好吧,看起来有些人在我工作的时候发现了这些错误,但是当我整理好整件事情时你可能想看一下这个解决大多数问题的工作版本。原:
import java.util.Random;
public class StillTesting {
public static void main( String[] args )
{
System.out.println("Your new password is: " + generateValidPassword());
}
static private String generateValidPassword()
{
StringBuilder password = new StringBuilder();
Random r = new Random();
final String loweralphabet = "abcdefghijklmnopqrstuvwxyz";
final String upperalphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i=0;i<3;i++)
{
password.append(String.valueOf(r.nextInt(10)));
}
for(int i=0;i<3;i++)
{
password.append(loweralphabet.charAt(r.nextInt(loweralphabet.length())));
}
for(int i=0;i<3;i++)
{
password.append(upperalphabet.charAt(r.nextInt(upperalphabet.length())));
}
return password.toString();
}
}