1
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
這是對 「薩希德」 那麼,爲什麼打印 「0」 產生的?我怎樣才能做到這一點
打印出「m?0」或任何我的程序生成的隨機密碼?
應用您提出的更改後:你不認爲這仍然是錯誤的。 sortDescending函數需要交換lname和psw的元素以保持與輸入相同的順序。 – Gyan
@Gyanapriya是的,除非他試圖將隨機密碼分配給用戶,否則你是正確的。 – user2004685