来自Deitel" Java How To Program"第10版:
2.30(以整数分隔数字)编写一个应用程序,输入一个由用户五位数字组成的数字,将数字分成各自的数字,并将每个数字彼此分开的数字打印出来。例如,如果用户输入数字42339,程序应该打印
4 2 3 3 9
假设用户输入正确的位数。输入超过五位数的数字会发生什么?当您输入少于五位的数字时会发生什么?[提示:您可以使用本章中学到的技巧进行此练习。您需要同时使用分区和提醒操作来选择"选择"每个数字。]
有人可以向我解释我应该如何去挑选"挑选"使用除法和提醒操作符的个别整数?
编辑:控制结构(if / else等)尚未被允许,这些将在以后的章节中进行探讨。仅变量,算术运算符和比较运算符。
答案 0 :(得分:2)
你知道这个数字是5位数。
怎么样?
number / 10 000 to retrieve the first digit.
number = reminder
number / 1000 to retrieve the second digit.
number = reminder
number / 100 to retrieve the third digit.
number = reminder
number / 10 to retrieve the fourth digit.
and reminder is the 5th one.
答案 1 :(得分:1)
我对分裂的小暗示 - 你可以除以10然后看一下,那个操作是什么给... 更多,你必须推断出你是否想学习编程。
答案 2 :(得分:1)
尝试一下:
import java.util.*;
public class DigitsDisplay
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int digit;
System.out.print("Enter a positive number: ");
digit = input.nextInt();
int power = 1;
while (power <= digit) {
power *= 10;
}
power /= 10;
while (power > 0) {
System.out.println(digit/power);
digit %= power;
power /= 10;
}
}
}
答案 3 :(得分:0)
尝试这样的事情
String str1 = "";
int a = 12345;
while(a>0)
{
int b = a%10;
str1 = str1 + String.valueOf(b+" ");
a = a/10;
}
StringBuilder str = new StringBuilder(str1);
System.out.println(str.reverse());
答案 4 :(得分:0)
我也刚刚开始使用java和本书,这是为我做的tric的代码。请原谅我,如果我做了一些奇怪的事情......)
import java.util.Scanner;
public class SeparateNumber
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int nr, nr1, nr2, nr3, nr4, nr5;
System.out.print("Enter a number with 5 digits: ");
nr = in.nextInt();
nr1 = nr / 10000;
nr2 = (nr % 10000) / 1000;
nr3 = ((nr % 10000) % 1000) / 100;
nr4 = (((nr % 10000) % 1000) % 100) / 10;
nr5 = (((nr % 10000) % 1000) % 100) % 10;
System.out.printf("%d%s%d%s%d%s%d%s%d%n", nr1, " ", nr2, " ", nr3, " ", nr4, " ", nr5);
}
}
答案 5 :(得分:0)
下面的代码处理在整数范围内的任何int上的拆分。
步骤:
示例代码:
import java.util.*;
public class SplitDigits{
public static void main(String args[]) throws Exception {
System.out.println("Enter number:");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
System.out.println("You have entered:"+number);
String str = String.valueOf(number);
int length = str.length();
if ( length > 0){
int[] digits = new int[length];
for ( int i=0; i < length; i++){
digits[i] = Integer.parseInt(String.valueOf(str.charAt(i)));
}
for ( int i=0; i < digits.length; i++){
System.out.println("Digits:"+digits[i]);
}
}
}
}
输出:
java SplitDigits
Enter number:
123456789
You have entered:123456789
Digits:1
Digits:2
Digits:3
Digits:4
Digits:5
Digits:6
Digits:7
Digits:8
Digits:9
答案 6 :(得分:0)
import java.util.Scanner;
public class DigitSeparator{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a 5 digit number: ");
int number = input.nextInt();
System.out.print((number / 10000)+ " ");
int divider = 10000;
int mod = number % 10000;
for(int i = 1; i <= 4; i++){
divider /= 10;
System.out.print( mod/divider +" ");
mod %= divider;
}
System.out.println();
}
}
答案 7 :(得分:0)
以下是按输入顺序显示数字的答案
import java.util.Scanner;
public class oddMethod {
public static void main(String[] args) {
System.out.println("Enter start number:");
int number=getNumber();
int last=Reverse1stTime(number);
System.out.println("Digit You Enter:"+last);
}
private static int getNumber() {
Scanner sr = new Scanner(System.in);
return sr.nextInt();
}
private static int Reverse1stTime(int number)
{ int digit=0;
int reverse=0;
while(number>0)
{
digit=number%10;
reverse=reverse*10+digit;
number=number/10;
}
return reverseAgain(reverse);
}
private static int reverseAgain(int number)
{ int digit=0;
int reverse=0;
while(number>0)
{
digit=number%10;
System.out.println(digit);
reverse=(reverse*10+digit);
number=number/10;
}
return reverse;
}
}
Input 12345
Output 12345
答案 8 :(得分:-1)
package chapter1;
import java.util.Scanner;
public class Exercise3 {
public static void main (String[] args){
Scanner scan = new Scanner (System.in);
System.out.print ("Enter a number that consist of 5 digits: ");
int num =scan.nextInt();
int digit1 =num / 10000;
int digit2 =(( num % 10000) / 1000 ); // the modulus answer of num/10000 is divided by 1000
int digit3 =( ( num % 1000) / 100 );
int digit4 = ( ( num% 100 ) / 10 );
int digit5 = ( num % 10);
System.out.print (digit1+"\t");
System.out.print(digit2+"\t");
System.out.print(digit3+"\t");
System.out.print(digit4+"\t");
System.out.println(digit5+"\t");
}
}
答案 9 :(得分:-2)
答案是
//your package name
import java.util.Scanner; //for input the userin
public class SeperateDigit {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.printf("Enter a 5 digit number: ");
int number = input.nextInt();
int number1 = number/100000;
int arrange = number%10000;
int number2 = arrange/1000;
int number3 = (arrange % 1000) /100;
int number4 =(( arrange % 1000) % 100) / 10;
int number5 = ((arrange % 1000) % 100) % 10;
System.out.printf("%d %d %d %d %d", number1,number2,number3,number4, number5);
}
}
结果是
Enter a 5 digit number: 422339
4 2 3 3 9