如何通过方法返回新数字

时间:2013-11-22 18:07:24

标签: java

我是java的新手 我必须编写一个程序,根据这些条件,通过在现有目录号的左侧添加一个数字,将现有目录(3-5位)编号修改为新目录编号:

  1. 新号码将是最左边数字与最右边数字之间的最大数字。
  2. 如果最左边的数字等于最右边的数字,则新数字将为9.
  3. 输入应为10个数字,然后添加新数字。 问题是,现在,方法“newKatalogNumber”获取旧的目录号,并返回左侧数字,我喜欢该方法返回新的目录代码。例如,如果方法得到1234,她将返回41234,并将在主要结尾处打印。我还没有办法做到这一点。 有人知道如何做到这一点? 我会很感激。

    这是我的代码:

    import java.util.Scanner;   // This program gets an old catalog number between 3 to 5 digits and change it to a new catalog number
    // by adding a new digit to the left of the number
    
    public class Number
    {
        public static int newKatalogNumber(int num)  
        {
            while (num>=10) 
            {
                num /= 10;
            }
           return num;
        }
    
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in); 
    
            for (int oldNum=0; oldNum<10; oldNum++)   
            {
                System.out.print("Insert old catalog Number: ");//The user insert the old catalog number
                int catalogNum = input.nextInt();
                int mostRight = catalogNum % 10;   
                int mostLeft = newKatalogNumber(catalogNum);
    
                //Adding the new digit according to condition below: 
    
                if (mostRight>mostLeft)
                {
                    System.out.println("Old catalog number is:"+catalogNum);
                    System.out.println("New catalog number is"+mostRight+catalogNum);
                }
                else if (mostLeft>mostRight)
                {
                    System.out.println("Old catalog number is:"+catalogNum);
                    System.out.println("New catalog number is"+mostLeft+catalogNum);
                }
                else
                {
                    System.out.println("Old catalog number is:"+catalogNum);
                    System.out.println("New catalog number is"+9+catalogNum);
                }
            }
        }
    }
    

3 个答案:

答案 0 :(得分:2)

如果你有一个像1234这样的号码,并且你想在开头添加,比如说5那么你应该这样做:

1234 + 5 * 10000

如果你想在最后添加5,你应该这样做:

1234 * 10 + 5 

请注意,在第一种情况下,10000中的零数等于原始数字中的位数。

答案 1 :(得分:0)

试试这个

        public static int NewKatatlogNumber (int num)
    {

        int noOfDigitsInTheNumber = num + "".length();
        int[] digitsArray = new int[noOfDigitsInTheNumber];

        int index = digitsArray.length - 1;

        //Extract digit by digit and populate the digitarray from right to left.
        //If your num is 123 your array will [1,2,3]
        while (num > 0)
        {
            int remainder = num % 10;

            num = num / 10;

            digitsArray[index--] = remainder;

        }

        //Find the number to add from the digit array.Apply your logic here
        int numberToAddToTheLeft = getNumberToAdd(digitsArray);


        int newNum = numberToAddToTheLeft;
        //Construct the final token by prepending the digit you identified
        for (int i = 0; i < digitsArray.length; i++)
        {
            int j = digitsArray[i];

            newNum = newNum * 10 + i;

        }

        return newNum;
    }

答案 2 :(得分:-1)

将数字转换为字符串,然后在其前面添加“n”。以下是使用DrJava的交互选项卡的示例:

> Integer.parseInt("5" + Integer.toString(500))
5500

> Integer.parseInt(Integer.toString(5) + Integer.toString(500))
5500

但是有一种更简洁(但less efficient)方式,因为将""添加到int会将其转换为字符串:

> Integer.parseInt(5 + "" + 500)
5500

如果你需要处理负数,你可以这样做:

if (n>0)
    System.out.println(Integer.parseInt(p + "" + n));
else
    System.out.println(Integer.parseInt("-" + p + "" + -n));