eclipse 你如何让它在你输入一个数字时在每个整数之间加一个空格

beq87vna  于 2022-11-23  发布在  Eclipse
关注(0)|答案(3)|浏览(143)
import java.util.Scanner;

public class Digits {

    public static void main(String[] args) {
        /*
         * 
    count = 1 
    temp = n 
    while (temp > 10) 
        Increment count. 
        Divide temp by 10.0. 
   */
        
        //Assignment: fix this code to print: 1 2 3 (for 123)
        //temp = 3426 -> 3 4 2 6
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int count = 1;
        int temp = input.nextInt();
        while(temp >= 10){
            count++;
            temp = temp / 10;
            System.out.print(temp + " ");
        }
    }

}

需要修复代码的帮助。示例:当你键入123时,它变成123。

7lrncoxx

7lrncoxx1#

你的代码每次都是除以10,这可以用来反向打印值。要向前打印它,你需要一个包含对数的位more math。有时像这样,

Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int temp = input.nextInt();
while (temp > 0) {
    int p = (int) (Math.log(temp) / Math.log(10));
    int v = (int) (temp / Math.pow(10, p));
    System.out.print(v + " ");
    temp -= v * Math.pow(10, p);
}

或者,读取一行输入。去掉所有非数字,然后打印每个字符,用空格隔开。例如,

String temp = input.nextLine().replaceAll("\\D", "");
System.out.println(temp.replaceAll("(.)", "$1 "));
oug3syen

oug3syen2#

你的大部分代码都是正确的,你要做的是除以10,然后打印出值--这可能应该是一个取模运算%,以获得运算的余数并打印出来--但这是一个很好的思考方式。
尽管如此。
您可以只使用一个字符串,然后在每个字符上拆分该字符串

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");

        // we know that we are going to get some input - so we will just grab it as a String
        // whilst we are expecting an int - we will test this later.
        // we are doing this as it makes it easier to split the contents of a string
        String temp = input.next();

        // is this an int? - we will test this first
        try {
            // if this parsing fails - then it will throw a java.lang.NumberFormat exception 
            // see the catch block below
            int test = Integer.parseInt(temp);
            
            

            // at this point it is an int no exception was thrown- so let's go 
            // through and start printing out each character with a space after it

            // the temp(which is a string).toCharArray returns a char[] which we
            // can just iterate through and set the variable of each iteration to 'c'
            for (char c : temp.toCharArray()) {
                
                // now we are going to print out the character with a space after it
                System.out.print(c + " ");
            }

        } catch (NumberFormatException ex){
            // this is not an int as we got a number format exception...

            System.out.println("You did not enter an integer. :(");
        }

        // be nice and close the resource
        input.close();
    }
mitkmikd

mitkmikd3#

您可以使用以下单行代码来单独回答您的问题。

int test = 123;
System.out.println(String.join(" ", Integer.toString(test).split("")));

输出为:1 2 3

相关问题