从用户获取值并将其转换为八进制等效值的java程序

时间:2015-03-08 22:48:07

标签: java

所以我试图构建一个接受用户输入的程序,然后如果该输入介于0和2097151之间,那么程序会将该int转换为它的八进制等效值,然后将其显示在结束。我在下面发布的程序工作得很好,但可能效率不高,我很好奇并试图找到如何使用反循环使用反控制重复实现同一程序的帮助,因为那是建议作为使用方法,但我没有看到如何做到这一点。

我问自己并且感到困惑的问题是:如何继续将数字除以8,继续将数字的模数除以8,并将该模数存储到单独的变量中,以便我可以将它们紧挨着打印出来吗?我不确定如何保持模数,直到我的数字达到0并使用while循环跟踪余数。

并澄清,这是一个学校作业,仅限于while循环,for循环,逻辑运算符和if / if else语句。我无法使用方法或内置方法来实现所需的最终结果。

import java.util.Scanner;
public class kwatke_OctalConversion {
    public static void main(String args[]) {
        Scanner input = new Scanner( System.in );
        int modulus1, modulus2, modulus3, modulus4, modulus5, modulus6, modulus7;
        int smallest=0;
        int Largest=2097151;
        System.out.println("The purpose of this program is to take a value from the "
            + "user and\n convert it from a base 10 number to a base 8 number and "
            + "display it\n for the user to see the converted octal number\n"
            + "Please enter a non-negative number between 0 and 2097151 to see it's "
            + "octal equivalent: " );
        int number = input.nextInt();
        if (number<smallest || number>Largest) {
            System.out.println ( "UNABLE TO CONVERT" );
        } else {
            modulus1 = (number % 8); //meat of our program, take number from user,
            number /= 8;             // take modulus of number, divide by 8, rinse and
            modulus2 = (number % 8); // repeat storing the modulus in a differnt var
            number /= 8;             // each time and continusing to divide the
            modulus3 = (number % 8); // leftover number by 8 until it reaches 0,
            number /= 8;             // storing modulus in a new variable each time.
            modulus4 = (number % 8);
            number /= 8;
            modulus5 = (number % 8);
            number /= 8;
            modulus6 = (number % 8);
            number /= 8;
            modulus7 = (number % 8);

            System.out.printf("%d%d%d%d%d%d%d\n", modulus7, modulus6, modulus5,
                modulus4, modulus3, modulus2, modulus1);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用:

    Scanner input = new Scanner(System.in);
    System.out.println("Please enter the number : ");
    int number = input.nextInt();
    String result = Integer.toOctalString(number);

See the source code here

答案 1 :(得分:0)

这会有用吗?

    int i=0;
    int a[] = new int[100];
    while (number>0) {
       a[i] = number % 8;
       number = number / 8;
       i++;
    }

    for(int j=i-1; j>=0; j--) {
       System.out.printf("%d", a[j]);
    }

根据评论中的建议,您还可以使用递归来查找八进制数。您可以对其进行调整以接受和验证用户输入。

class OctalNumbers
{
    static int a[] = new int[100];
    static int index = 0;

    public static void main (String[] args) throws java.lang.Exception
    {
        int number = 56;
        findOctal(number);
        for(int j = index-1; j>=0; j--) {
            System.out.printf("%d", a[j]);
        }
    }

    private static void findOctal(int number) {

        if(number != 0) {
            int rem = number % 8;
            a[index] = rem;
            number = number / 8;
            index++;
            findOctal(number);  
        }


    }
}