家庭作业挑战研究,随机生成的字符串+数字比较

时间:2019-07-07 09:34:30

标签: java if-statement random java.util.scanner

概念是创建用户名1和用户名2,分配字符串值name1,name2,然后是唯一整数,例如01,02(分别) 然后比较这些值。

我写了尽可能多的代码。

        public static void main(String[] args) {
        Scanner usernameone = new Scanner(System.in);
        System.out.println("Enter Username");
        String username1 = usernameone.nextLine();

        int usernameNumber1;
        Scanner usernameNumberOne = new Scanner(System.in);
        System.out.println("Enter in your age.");
        usernameNumber1 = usernameNumberOne.nextInt();

        Scanner usernametwo = new Scanner(System.in);
        System.out.println("Enter Username");
        String username2 = usernametwo.nextLine();

        int usernameNumber2;
        Scanner usernameNumberTwo = new Scanner(System.in);
        System.out.println("Enter in your age.");
        usernameNumber2 = usernameNumberTwo.nextInt();

        if (username1 <= 0) { // Generate random string for name.
        }
        if (usernameNumber1 <= 0) { // Generate random int for number.     
        }
        if (username2 <= 0) { // Generate random string for name.
        }
        if (usernameNumber2 <= 0) { // Generate random int for number.     
        }
        if (username1 == username2) {
            System.out.println("Username 1 and 2 are same.");
        }
        if (usernameNumber1 == usernameNumber2) {
            System.out.println("Usernumber 1 and 2 are same.");
        }

如果用户未在名称1或name2或int 01,02中输入数字,我就会陷入困境。该程序应该为这些变量随机生成一个数字。

//Then I must compare them. 
 //Name 1 == Name 2? 
 //Number 1 == number2? 
 //Name 1 & Number 1 == Name 2 & Number2.

1 个答案:

答案 0 :(得分:0)

您可以了解随机类here以及如何比较字符串here

您说过,如果用户不输入任何内容,则应生成该内容。

因此,整个代码是错误的:

if (username1 <= 0) { // Generate random string for name. // Checking for null strings you check with the null keyword, not 0
}
if (usernameNumber1 <= 0) { // Generate random int for number.
}
if (username2 <= 0) { // Generate random string for name. // and here
}
if (usernameNumber2 <= 0) { // Generate random int for number.
}
if (username1 == username2) { // Wrong String comparing
    System.out.println("Username 1 and 2 are same.");
    }
if (usernameNumber1 == usernameNumber2) {
    System.out.println("Usernumber 1 and 2 are same.");
}

您必须做的是:检查变量是否为null,以及是否为它们生成随机值

static String genRandomString() { // A helpful method
    Random r = new Random();
    String randomString = "";
    int size = r.nextInt(7) + 3; // 3 is the minimum size and 10 (7 + 3) is the maximum size for the strings
    char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; // available characters for the string
    for (int i = 0; i < size; i++) {
        randomString += alphabet[r.nextInt(alphabet.length - 1)]; // get random char
    }
    return randomString;
}

// after inputs
Random r = new Random();
if (username1.length() == 0) // check if the string's size equals 0
    username1 = genRandomString();
if (username2.length() == 0)
    username2 = genRandomString();
if (usernameNumber1 == 0)
    usernameNumber1 = r.nextInt(50) + 10; // get a random age from 10 to 60
if (usernameNumber2 == 0)
    usernameNumber2 = r.nextInt(50) + 10; // get a random age from 10 to 60
// now check if the usernames are same
if (username1.equals(username2))
    System.out.println("Username 1 and 2 are same.");
// check for ages
if (usernameNumber1 == usernameNumber2)
    System.out.println("Usernumber 1 and 2 are same.");