从用户那里获取 6 位数字输入并将每个数字存储在 Java 中的单个数组中
Take 6 digit input from user and store each digit in a single array in Java
我想使用扫描仪读取一个 6 位数字,例如 123456,并将每个数字存储在一个数字数组中。
System.out.println("Please enter a 6 digit code");
int code = keyboard.nextInt();
然后以某种方式将其放入:
int [] array = {1,2,3,4,5,6};
我需要数组是int类型的,因为我要找到序列中的奇数相加(1+3+5)和偶数相加(2+4+6)的乘积(根据程序的规范)然后我有一些条件告诉我将哪个数字连接到最后。
我可以通过预定义一个数字数组来实现这一点,但我希望这个数字来自用户。
除非有另一种不使用数组的方法吗?感谢您的指点。
这是我预定义数组及其值时的代码。
int code[] = {1,2,3,4,5,6};
//add the numbers in odd positions together
int sum_odd_position = 0;
for (int i=0; i<6; i+=2)
{
sum_odd_position += code[i];
}
//convert the initial code into strings
String string_code = "";
String seven_digit_code = "";
//add numbers in even positions together
int sum_even_position=0;
for (int i=1; i<6; i+=2)
{
sum_even_position += code [i];
}
//add them both together
int sum_odd_plus_even = sum_odd_position + sum_even_position;
//calculate remainder by doing our answer mod 10
int remainder = sum_odd_plus_even % 10;
System.out.println("remainder = "+remainder);
//append digit onto result
if (remainder==0) {
//add a 0 onto the end
for (int i=0; i<6; i+=2)
{
string_code += code [i];
}
}
else {
//subtract remainder from 10 to derive the check digit
int check_digit = (10 - remainder);
//concatenate digits of array
for (int i=0; i<6; i++)
{
string_code += code[i];
}
//append check digit to string code
}
seven_digit_code = string_code + remainder;
System.out.println(seven_digit_code);
}
}
首先从用户那里读入一个字符串。创建一个数组来放入数字。逐个字符地遍历字符串。将每个字符转换为合适的int值放入数组中。
String line = keyboard.nextLine();
int[] digits = new int[line.length()];
for (int i = 0; i < digits.length; ++i) {
char ch = line.charAt(i);
if (ch < '0' || ch > '9') {
throw new IllegalArgumentException("You were supposed to input decimal digits.");
}
digits[i] = ch - '0';
}
(另请注意,如果 ch-'0'
不适合您的目的,还有 various ways 将 char 解析为 int。)
向用户发送消息:
System.out.println("Hello, please enter a 6 digit number: ");
请求输入,扫描一次并设置分隔符:
// do this once for the entire app
Scanner s = new Scanner(System.in);
s.useDelimiter("\r?\n");
然后要东西:
int nextInt = s.nextInt();
String nextString = s.next();
选择您想要的任何数据类型,并为此调用正确的 nextX()
方法。
这听起来像你应该要求一个字符串而不是一个数字(因为大概 000000
是有效的输入,但它不是一个真正的整数),所以你会使用 next()
.
要检查它是否为 6 位数字,您可以使用多种方法。大概你需要把它变成一个数组,所以为什么不循环遍历每个字符:
String in = s.next();
if (in.length() != 6) throw new IllegalArgumentException("6 digits required");
int[] digits = new int[6];
for (int i = 0; i < 6; i++) {
char c = in.charAt(i); // get character at position i
int digit = Character.digit(c, 10); // turn into digit
if (digit == -1) throw new IllegalArgumentException("digit required at pos " + i);
digits[i] = digit;
}
如果你想减少 english/western-centric,或者你想添加对例如十六进制数字,在 Character 中有一些实用方法可以代替:
if (digit == -1) throw new IllegalArgumentException("digit required");
首先,为数字分配一个扫描器和一个数组
Scanner input = new Scanner(System.in);
int[] digits = new int[6];
- 提示输入整数。
- 使用
Math.log10
检查位数是否正确
- 否则,re-prompt
- 然后使用除法和取余运算符,用数字填充数组。反向填充以保持顺序。
for (;;) {
System.out.print("Please enter six digit integer: ");
int val = input.nextInt();
// a little math. Get the exponent of the number
// and assign to an int. This will determine the number of digits.
if ((int) Math.log10(val) != 5) {
System.out
.println(val + " is not six digits in size.");
continue;
}
for (int i = 5; i >= 0; i--) {
digits[i] = val % 10;
val /= 10;
}
break;
}
System.out.println(Arrays.toString(digits));
用于输入 123456
打印
[1, 2, 3, 4, 5, 6]
我想使用扫描仪读取一个 6 位数字,例如 123456,并将每个数字存储在一个数字数组中。
System.out.println("Please enter a 6 digit code");
int code = keyboard.nextInt();
然后以某种方式将其放入:
int [] array = {1,2,3,4,5,6};
我需要数组是int类型的,因为我要找到序列中的奇数相加(1+3+5)和偶数相加(2+4+6)的乘积(根据程序的规范)然后我有一些条件告诉我将哪个数字连接到最后。
我可以通过预定义一个数字数组来实现这一点,但我希望这个数字来自用户。
除非有另一种不使用数组的方法吗?感谢您的指点。
这是我预定义数组及其值时的代码。
int code[] = {1,2,3,4,5,6};
//add the numbers in odd positions together
int sum_odd_position = 0;
for (int i=0; i<6; i+=2)
{
sum_odd_position += code[i];
}
//convert the initial code into strings
String string_code = "";
String seven_digit_code = "";
//add numbers in even positions together
int sum_even_position=0;
for (int i=1; i<6; i+=2)
{
sum_even_position += code [i];
}
//add them both together
int sum_odd_plus_even = sum_odd_position + sum_even_position;
//calculate remainder by doing our answer mod 10
int remainder = sum_odd_plus_even % 10;
System.out.println("remainder = "+remainder);
//append digit onto result
if (remainder==0) {
//add a 0 onto the end
for (int i=0; i<6; i+=2)
{
string_code += code [i];
}
}
else {
//subtract remainder from 10 to derive the check digit
int check_digit = (10 - remainder);
//concatenate digits of array
for (int i=0; i<6; i++)
{
string_code += code[i];
}
//append check digit to string code
}
seven_digit_code = string_code + remainder;
System.out.println(seven_digit_code);
}
}
首先从用户那里读入一个字符串。创建一个数组来放入数字。逐个字符地遍历字符串。将每个字符转换为合适的int值放入数组中。
String line = keyboard.nextLine();
int[] digits = new int[line.length()];
for (int i = 0; i < digits.length; ++i) {
char ch = line.charAt(i);
if (ch < '0' || ch > '9') {
throw new IllegalArgumentException("You were supposed to input decimal digits.");
}
digits[i] = ch - '0';
}
(另请注意,如果 ch-'0'
不适合您的目的,还有 various ways 将 char 解析为 int。)
向用户发送消息:
System.out.println("Hello, please enter a 6 digit number: ");
请求输入,扫描一次并设置分隔符:
// do this once for the entire app
Scanner s = new Scanner(System.in);
s.useDelimiter("\r?\n");
然后要东西:
int nextInt = s.nextInt();
String nextString = s.next();
选择您想要的任何数据类型,并为此调用正确的 nextX()
方法。
这听起来像你应该要求一个字符串而不是一个数字(因为大概 000000
是有效的输入,但它不是一个真正的整数),所以你会使用 next()
.
要检查它是否为 6 位数字,您可以使用多种方法。大概你需要把它变成一个数组,所以为什么不循环遍历每个字符:
String in = s.next();
if (in.length() != 6) throw new IllegalArgumentException("6 digits required");
int[] digits = new int[6];
for (int i = 0; i < 6; i++) {
char c = in.charAt(i); // get character at position i
int digit = Character.digit(c, 10); // turn into digit
if (digit == -1) throw new IllegalArgumentException("digit required at pos " + i);
digits[i] = digit;
}
如果你想减少 english/western-centric,或者你想添加对例如十六进制数字,在 Character 中有一些实用方法可以代替:
if (digit == -1) throw new IllegalArgumentException("digit required");
首先,为数字分配一个扫描器和一个数组
Scanner input = new Scanner(System.in);
int[] digits = new int[6];
- 提示输入整数。
- 使用
Math.log10
检查位数是否正确 - 否则,re-prompt
- 然后使用除法和取余运算符,用数字填充数组。反向填充以保持顺序。
for (;;) {
System.out.print("Please enter six digit integer: ");
int val = input.nextInt();
// a little math. Get the exponent of the number
// and assign to an int. This will determine the number of digits.
if ((int) Math.log10(val) != 5) {
System.out
.println(val + " is not six digits in size.");
continue;
}
for (int i = 5; i >= 0; i--) {
digits[i] = val % 10;
val /= 10;
}
break;
}
System.out.println(Arrays.toString(digits));
用于输入 123456
打印
[1, 2, 3, 4, 5, 6]