继续随机将弦上的null保持在字符串上



它在这里是java启动器,所以我正在编码我的程序来制作密码,我使用随机对象具有一个随机数,该数字将选择什么字母或什么号码。P>

我不断获得数字和符号。.我得到的就是这样:

" 9 = 9null = 99"我不明白为什么有空。Java没有向我显示我的代码问题(不显示Null指针异常。)

这是我的代码:

    package project.Secureword.com;
import java.awt.*;

import java.awt.event.*;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.Random;
import javax.swing.*;

public class Main {
    private static JFrame start;
    private static TextField field;
    private static String password;
    private static String lastpass;
    public static Settings passsettings;
    public static Random r;
    public static void main(String[] args) {
        passsettings = new Settings();
        passsettings.setIfDots(true);
        passsettings.setIfLowercase(true);
        passsettings.setIfNum(true);
        passsettings.setIfUpprcase(true);
        Settings.changerun();
        start();
    }
    public static void start() {
        Font a = new Font(null, Font.BOLD, 0);
        Font size = a.deriveFont(20f);
        JButton createPass = new JButton();
        createPass.setText("<html> Click me to <br> Generate a Password! <html>");
        createPass.setPreferredSize(new Dimension(100,100));
        JLabel text = new JLabel();
        text.setText("<html> Welcome to SECUREPASS! <br> An Extreme Strong Password Generator <br> Developed by OfficialCode");
        text.setPreferredSize(new Dimension(150,150));
        text.setFont(size);


        field = new TextField(20);
        field.setSize(new Dimension(50,50));


        start = new JFrame("Secureword | Strong password Generator | Coded by OfficialCode");
        start.setPreferredSize(new Dimension(390,390));
        start.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        start.setLocationRelativeTo(null);
        start.setLayout(new BorderLayout());
        start.getContentPane().add(createPass, BorderLayout.WEST);
        start.getContentPane().add(text, BorderLayout.PAGE_START);
        start.getContentPane().add(field, BorderLayout.CENTER);
        start.pack();
        start.setVisible(true);
        password = "";

        createPass.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                field.setText(null);
                field.selectAll();
                allrandom();
            }});

}
    public static void allrandom() {
        String[] pass = new String[8];
        Random r = new Random();
    for(int i = 0 ; i < pass.length ; i++) {
        int ch = 0;
        ch = r.nextInt(3);
        switch(ch) {
        case 1:
            if(passsettings.isIfDots()) {
                String newchar = ""; 
                newchar = dotpass();
                pass[i] = newchar;
                break;
            }
        case 2:
            if(passsettings.isIfNum()) {
                String newchar2 = ""; 
                newchar2 = Numpass();
                pass[i] = newchar2;
                break;
            }
        case 3:
            String newchar3 = ""; 
            newchar3 = charchose();
            pass[i] = newchar3;
            break;
        }
    }
    for(int i = 0 ; i < pass.length ; i++) {
        String newpasschar = "";
        newpasschar = pass[i];
        password = password + newpasschar;
    }
    field.setText(password);
        // random() end
    }
    public static String uppercasepass() {
        Random r = new Random();
        String passletter = "";
        int rN3 = 0;
        rN3 = r.nextInt(26);
        switch(rN3) {
        case 1:
            passletter = "A";
        case 2:
            passletter = "B";
        case 3:
            passletter = "C";
        case 4:
            passletter = "D";
        case 5:
            passletter = "E";
        case 6:
            passletter = "F";
        case 7:
            passletter = "G";
        case 8:
            passletter = "H";
        case 9:
            passletter = "I";
        case 10:
            passletter = "J";
        case 11:
            passletter = "K";
        case 12:
            passletter = "L";
        case 13:
            passletter = "M";
        case 14:
            passletter = "N";
        case 15:
            passletter = "O";
        case 16:
            passletter = "P";
        case 17:
            passletter = "Q";
        case 18:
            passletter = "R";
        case 19:
            passletter = "S";
        case 20:
            passletter = "T";
        case 21:
            passletter = "U";
        case 22:
            passletter = "V";
        case 23:
            passletter = "W";
        case 24:
            passletter = "X";
        case 25:
            passletter = "Y";
        case 26:
            passletter = "Z";

    }
        return passletter;
    // uppercase() end
}
    public static String lowercasepass() {
        Random r = new Random();
        String passletter = "";
        int rN3 = 0;
        rN3 = r.nextInt(26);
        switch(rN3) {
        case 1:
            passletter = "a";
        case 2:
            passletter = "b";
        case 3:
            passletter = "c";
        case 4:
            passletter = "d";
        case 5:
            passletter = "e";
        case 6:
            passletter = "f";
        case 7:
            passletter = "g";
        case 8:
            passletter = "h";
        case 9:
            passletter = "i";
        case 10:
            passletter = "j";
        case 11:
            passletter = "k";
        case 12:
            passletter = "l";
        case 13:
            passletter = "m";
        case 14:
            passletter = "n";
        case 15:
            passletter = "o";
        case 16:
            passletter = "p";
        case 17:
            passletter = "q";
        case 18:
            passletter = "r";
        case 19:
            passletter = "s";
        case 20:
            passletter = "t";
        case 21:
            passletter = "u";
        case 22:
            passletter = "v";
        case 23:
            passletter = "w";
        case 24:
            passletter = "x";
        case 25:
            passletter = "y";
        case 26:
            passletter = "z";

    }
        return passletter;
    }
    public static String Numpass() {
        Random r = new Random();
        String passletter = "";
        int rN = 0;
        rN = r.nextInt(9);
        switch(rN) {
        case 1:
            passletter = "1";
        case 2:
            passletter = "2";
        case 3:
            passletter = "3";
        case 4:
            passletter = "4";
        case 5:
            passletter = "5";
        case 6:
            passletter = "6";
        case 7:
            passletter = "7";
        case 8:
            passletter = "8";
        case 9:
            passletter = "9";
        }
        return passletter;
    }
    public static String dotpass() {
        Random r = new Random();
        String passletter = "";
        int rN5 = 0;
        rN5 = r.nextInt(5);
        switch(rN5) {
        case 1:
            passletter = "_";
        case 2:
            passletter = "]";
        case 3:
            passletter = "$";
        case 4:
            passletter = "#";
        case 5:
            passletter = "=";

        }


        return passletter;
    }

    public static String charchose() {
        Random r = new Random();
        String line = "";
        int rN = 0;
        rN = r.nextInt(20);
        switch(rN) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
            if(passsettings.isIfLowercase()) {
                line = lowercasepass();
            }
        case 11:
        case 12:
        case 13:
        case 14:
        case 15:
        case 16:
        case 17:
        case 18:
        case 19:
        case 20:
            if(passsettings.isIfUpprcase()) {
                line = uppercasepass();
            }
        }



        return line;
    }


}

问题来自 Random.nextInt(n)返回0-n(独家)之间的值的事实。您的开关案例都是一个错误的。

这解释了您的空,因为您不为ch = 0做任何事情,因此String停留在null

switch(ch){
case 1: ... //symbol
case 2 ... //number
case 3: ... //letter
}

这应该是

switch(ch){
case 0: ... //symbol
case 1: ... //number
case 2: ... //letter
}

其他一些问题或进步:

  • 您不在case之后break,因此您始终执行最后一个案例(所有以下情况)。导致4个字符zZ9=的可能性(易于爆发!)
  • 您应该使用char[]代替String[],无需使用String像您一样存储一个字符。

而不是

String s = "a";

你可以

char c = 'a';

如果读取下一个点,则缩短代码的长度。

  • 您可以通过进行一些添加(例如'a' + r.nextInt(26))来减少开关,以在'a''z'
  • 之间获得一个值
  • 对于一个随机符号,而不是开关,用每个字符创建一个char[]的静态数组,然后使用此数组的大小获取一个随机值。

喜欢:

 final static char[] symbol = {'#', '@', '-', '\', '/', '*'};
 static getRadomSymbol(){
     Random r = new Random();
     return symbol[r.nextInt(symbol.length)];
 }

允许轻松添加字符(并再次减少行数;)

相关内容

  • 没有找到相关文章

最新更新