import java.util.*;
import java.io.*;
public class A3 {
public static void main(String args[])
{
Accept inputScanner = new Accept();
Assign3 sortObj = new Assign3();
String lname[] = {"","","","",""};
String psw[] = {"","","","",""};
String input;
do
{
System.out.println("Password Generator");
System.out.println("Please enter 5 last names:");
for (int index = 0; index < lname.length; index++)
{
System.out.print("Please enter last name: ");
lname[index] = inputScanner.acceptInputString();
psw[index] = sortObj.generatePassword(lname, index);
}
sortObj.sortArrayDescending(lname);
sortObj.arrayDisplay(lname, psw);
Screen.scrollscreen(70, 1, '=');
System.out.print("Please enter name to search (e or E to exit):");
input = inputScanner.acceptInputString();
int password = sortObj.binSrch(lname, input);
if(password >= 0)
{
System.out.println("Name: " + input + " ===> " + "password: " + password);
System.out.println("++++++++++++++++++++++++++++++++++");
}
else
{
System.out.println(input + " is not found");
System.out.println("++++++++++++++++++++++++++++++++++");
}
}while(input.equals("e") && input.equals("E"));
}
public void sortArrayDescending(String[] lnameArray)
{ String temp;
for(int j = 1; j < lnameArray.length - 1; j++)
{
for(int index = 0; index < lnameArray.length - 1; index++)
{
if(lnameArray[index].trim().compareTo(lnameArray[index+1].trim())<0)
{
temp = lnameArray[index];
lnameArray[index] = lnameArray[index + 1];
lnameArray[index + 1] = temp;
}
}
}
}
public void arrayDisplay(String lnameContent[], String pswContent[])
{
for(int i = 0; i < lnameContent.length; i ++)
{
System.out.println(lnameContent[i] + " " + pswContent[i]);
}
}
public int binSrch(String strArr[], String search)
{
int mid = -1;
int first = 0;
int last = strArr.length - 1;
boolean found = false;
while(first <= last)
{
mid = (first + last) / 2;
if(strArr[mid].equalsIgnoreCase(search))
{
found = true;
break;
}
else if(strArr[mid].compareToIgnoreCase(search) < 0)
{
last = mid - 1; //use lower half
}
else
{
first = mid + 1; //use upper half
}
}
if(!found)
{
mid = -1;
}
return mid;
}
public String generatePassword(String[] str, int index)
{
String name = str[index];
char first = name.charAt(0);
char last = name.charAt(name.length()-1);
int mid = 0;
if(first != last)
{
mid = ( ((int)(first) + ((int)(last)) / 2) );
}
else
{
mid = ( ((int)(first) + ((int)(last)) / 3) );
}
Random rNum = new Random();
int randomNum = rNum.nextInt(5);
String password = (first + "" + (char)(mid) + "" + randomNum).toLowerCase();
return password;
}
public void duplicateCheck(String[] password)
{
}
}
输出
Password Generator
Please enter 5 last names:
Please enter last name: magnum
Please enter last name: bauer
Please enter last name: sahid
Please enter last name: austen
Please enter last name: reese
sahid m?0
reese b?0
magnum s¥4
bauer a?0
austen r¤0
======================================================================
Please enter name to search (e or E to exit):sahid
Name: sahid ===> password: 0
++++++++++++++++++++++++++++++++++
//this line is supposed to print out "m?0" because it is the password
这是为“sahid”生成的,那么为什么它打印“0”?我怎样才能做到这一点
打印出“m?0”或我的程序为其生成的任何随 secret 码?
您的代码是正确的。请记住,password
是 psw[]
字符串数组的索引,而不是数组的实际内容。
您只需在以下行中写入 psw[password]
而不是 password
:
System.out.println("Name: " + input + " ===> " + "password: " + password);
/* Correction Here : Replace password --> psw[password] */
更正的代码片段:
if(password >= 0)
{
System.out.println("Name: " + input + " ===> " + "password: " + psw[password]);
System.out.println("++++++++++++++++++++++++++++++++++");
}
现在,您应该得到如下输出:
Name: sahid ===> password: m?0
编辑:此外,正如 @Gyanapriya 提到的,除了 lname[]
之外,您还必须对 psw[]
数组进行排序,除非您尝试将随 secret 码分配给用户。
我是一名优秀的程序员,十分优秀!