如何获得要正确显示的密码数,而不是显示太多密码

  • 本文关键字:显示 密码 太多 何获得 java
  • 更新时间 :
  • 英文 :

import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Main
{
public static void main(String[] args) {
try {
//Introduce Program
// System.out.println("**********************************************************");
//System.out.println("Welcome to a simple password generator.");
//System.out.println("The password will include lowercase, uppercase, & numbers.");

File file = new File("passcode.txt");
if (!file.exists()) {
file.createNewFile();
}

while(true) {
Scanner input = new Scanner(System.in);
System.out.print("nEnter a password length (6 or more): ");
int length = input.nextInt();

if(length < 6) {
System.out.println("n  Password length too short. Please try again.");
} else {
String randomPassword = "";
for(int j = 0; j < length; j++) {
randomPassword += randomCharacter();
}
FileWriter fw = new FileWriter("passcode.txt", true);
fw.write(randomPassword);
fw.write("n");
fw.close();

System.out.println("nA password has been sent to text file");
System.out.print("Would you like to generate another password?  Y/N ");
char choice = input.next().charAt(0);

if(choice == 'n') {
System.out.println("nThank you for using the Pass CodeGenerator.n");
System.out.print("Here are your randomly generated codes:");

**int count = 0;
Scanner myReader = new Scanner(file);
while (myReader.hasNextLine()) {
count += 1;
String data = myReader.nextLine();
System.out.format("n%d.  %s", count, data);**
}
myReader.close();

break;
} else if(choice == 'y') {
continue;
}
}
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

public static char randomCharacter() {
int rand = (int)(Math.random()*62);
//0-61 inclusive = all possible values of rand
//0-9 inclusive = 10 possible numbers/digits
//10-35 inclusive = 26 possible uppercase letters
//36-61 inclusive = 26 possible lowercase letters
//If rand is between 0 (inclusive) and 9 (inclusive), then it's a number/digit
//If rand is between 10 (inclusive) and 35 (inclusive), then it's an uppercase letter
//If rand is between 36 (inclusive) and 61 (inclusive), then it's a lowercase letter
**if(rand <= 9) {
int number = rand + 48;
return (char)(number);
} else if(rand <= 35) {
int uppercase = rand + 55;
return (char)(uppercase);
} else {
int lowercase = rand + 61;
return (char)(lowercase);**
}
} }

所以,如果我选择做3个密码,1个有8个字母,一个有6个字母,还有一个有7个字母,它就会打印出来。

Thank you for using the Pass Code Generator.
Here are your randomly generated codes:
1.  EY0ZSJq
2.  VmJxglEp
3.  bily6wxBX4
4.  L2MvxzHx
5.  lLPAQC
6.  CtnlU4
7.  CB7z1ZIc
8.  XIIm71ILe
9.  JGmc79
10.  ElwRG8Tf
11.  13DJFi
12.  loCZlIQ
13.  oY7JhlHy
14.  VmqD00
15.  47HQf3GT
16.  lNvR6z
17.  nM45JvA
18.  eXs0vkcP9
19.  UO6G6sCQ
20.  MrOsjvaZ
21.  Gr028GvMM
22.  cxBK6x
23.  JSgXn6k9
24.  A4miDf
25.  oCeS54o

您的代码运行良好。

我认为问题是,您多次执行代码,而字符串被写入同一个文件。

当您在第一次运行时创建3个密码时,文件中有3行。当您在第二次运行中创建3个密码时,它会添加3行,文件中有6行。

  • 三代->文件中有3行
  • 3代->文件中有6行(第一次运行后有3行(

你可以做的是,像这样在开始时删除文件:

File file = new File("passcode.txt");
if (!file.exists()) {
file.createNewFile();
} else {
file.delete();
}

请注意,第一次运行生成的字符串在这里会丢失。

最新更新