简体   繁体   English

如何将占位符添加到随机 Int 然后从 Java 中的 Int 中提取一个数字?

[英]How can I add a placeholder to a random Int then pull a single digit from that Int in Java?

I am new to Java and programming all together.. I am trying to make a program that ciphers a number for the user.我是 Java 和编程的新手。我正在尝试制作一个为用户加密数字的程序。 The user inputs 5 digits separately so I add them together to get a total.用户分别输入 5 位数字,所以我将它们加在一起得到总数。 I need to pull the first digit and second digit of the total and enter it into (firstDigit+key)%10 and (secondDigit+key)%10.我需要提取总数的第一个数字和第二个数字并将其输入 (firstDigit+key)%10 和 (secondDigit+key)%10。 Then need to combine the answer to each equation together.然后需要将每个方程的答案组合在一起。

  • My total needs to be two digits, so even if the user enters all 1's which would total to be 5, I need it to be displayed and seen as 05 so that both equations have a digit to use.我的总数需要是两位数,所以即使用户输入全为 1 的总数为 5,我也需要将其显示并视为 05,以便两个方程都有一个数字可供使用。 It needs to be two digits.它必须是两位数。 I cant seem to figure how to enter a place holder.我似乎无法弄清楚如何输入占位符。 I was thinking about trying to use:我正在考虑尝试使用:

     if (total < 10)

but then what?但是然后呢?

  • Secondly, the method I used below seems like a terrible way to pull a single digit from a number.其次,我在下面使用的方法似乎是从数字中提取一位数的可怕方法。 I think I changed the int total into a string so I can use .substring to pull the digits, then converted back to an int.我想我将 int total 更改为一个字符串,这样我就可以使用 .substring 来提取数字,然后转换回一个 int。 Surprisingly it works.令人惊讶的是它有效。 Is there a better way to do this knowing that the number is random?知道这个数字是随机的,有没有更好的方法来做到这一点?

     String totalString = Integer.toString(total); String stringDigit1 = totalString.substring(0,1); String stringDigit2 = totalString.substring(1,2); int firstDigitInt1 = Integer.parseInt(stringDigit1); int firstDigitInt2 = Integer.parseInt(stringDigit2); int encodedDigit1 = (firstDigitInt1+key)%10; int encodedDigit2 = (firstDigitInt2+key)%10; System.out.println("Your encoded Number is: " + encodedDigit1 + encodedDigit2);

Your method for obtaining the individual digits is good, and if you want to maintain it I believe your intuition is correct, it should suffice to say:您获取单个数字的方法很好,如果您想维护它,我相信您的直觉是正确的,只需说:

if (total < 10) {
    firstDigitInt1 = 0
}

This will work out with your following math.这将解决您的以下数学问题。

Your method with substrings is far from terrible, but in case you wanted something more sleek you can instead say:您使用子字符串的方法远非可怕,但如果您想要更时尚的东西,您可以说:

int Digit1 = total / 10;
int Digit2 = total % 10;

Here, you can take advantage of integer truncation (where an integer won't remember decimal places) to get the first digit, which also solves the first problem: 5 / 10 = 0 (in terms of ints).在这里,您可以利用整数截断(其中一个整数不会记住小数位)来获取第一个数字,这也解决了第一个问题:5 / 10 = 0(就整数而言)。 For the second digit, it suffices to say modulo 10, as it is the remainder once the total is divided by 10.对于第二个数字,可以说模 10 就足够了,因为它是总数除以 10 后的余数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM