本文介绍了10个基本立足在java中2,8,16转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个面向对象的类,但我只是不知道如何做到这一点。我知道的基本原理,但像这样的事情。有人问我创建#的基地10转换为基地2个,基地8和基座16然后用一个程序后​​,我们被要求将其转换回从2,8,16立足10.我已经收集了一些资料从其他网站,我实际上结束了编辑一点吧。
请帮忙!
我想preFER,如果你们实际上帮助和编辑自己,并把它交给我,但如果是过分的要求,请尝试引导我通过它,因为我不知道很多的Java。
到目前为止,我有:

 进口java.util.Scanner中;公共类baseconverterr
{公共静态无效的主要(字串[] args){      //从用户读取转换的选择      的System.out.println(选择1或2或3:);      的System.out.println(1:从基部10转换至基部2);      的System.out.println(2:从基体10转换至基部8);      的System.out.println(3:从基部10转换至基部16);
      //你想1,2,或3千万?你有你的选择
      扫描仪在=新的扫描仪(System.in);      INT选择= in.nextInt();      串串= in.nextLine();      //阅读数转换并执行转换      字符串输出=;      的System.out.println(请输入数字转换:​​);      INT输入= in.nextInt();      如果(选择== 1)
      //如果用户选择的选择#1,它将从基体10转换至基部2
          输出= Integer.toString(输入,2);      否则,如果(选择== 2)          输出= Integer.toString(输入,8);    //如果用户选择的选择#2,它将从基体10转换为8基
     否则,如果(选择== 3)          输出= Integer.toString(输入,16);
   //如果用户选择的选择#3,它将从基体10转换至基部16
  其他      的System.out.println(无效条目);
      //一切,它是无效的
      的System.out.println(最后的输出=+输出);
      //这个打印最终输出。


解决方案

有关十进制X(基数为10),您可以分别使用二进制,八进制,十六进制转换

Integer.toString(X,2)

Integer.toString(X,8)

Integer.toString(X,16)

然后将其分别从二进制,八进制转换回十进制,十六进制转换

Integer.valueOf(binary_value,2)

Integer.valueOf(octal_value,8)

Integer.valueOf(hex_value,16)

在code,更改以下内容:

 输出= Integer.toString(输入,16)//取代16十六进制,8进制,2二进制

I'm in this Object Oriented class, but I just don't know how to do this. I know basic fundamentals but nothing like this. I am asked to create a program that converts #'s base 10 to base 2, base 8,and base 16. Then, after we are asked to convert it back from 2,8,16 to base 10. I have gathered some information from other websites and I actually ended up editing a bit of it. Please help!I would prefer it if you guys actually helped and edited yourselves and sent it to me, but if that is too much to ask, please try to guide me through it as I do not know much of Java.So far I have:

import java.util.Scanner;

public class baseconverterr
{

public static void main(String[] args) {

      // Read the conversion choice from the user

      System.out.println("Choose 1 or 2 or 3:");

      System.out.println("1: conversion from base 10 to base 2 ");

      System.out.println("2: conversion from base 10 to base 8");

      System.out.println("3: conversion from base 10 to base 16");
      // do you want 1, 2 , or 3? you have your choice
      Scanner in = new Scanner(System.in);

      int choice = in.nextInt();

      String string = in.nextLine();

      // Read in the number to be converted and do the conversion

      String output= "";

      System.out.println("Please enter the number to be converted:");

      int input = in.nextInt();

      if (choice == 1)
      // if the user chooses choice #1, it will convert from base 10 to base 2
          output = Integer.toString(input, 2);

      else if (choice == 2)

          output = Integer.toString(input, 8);

    // if the user chooses choice #2, it will convert from base 10 to base of 8 
     else if (choice == 3)

          output = Integer.toString(input, 16);
   // if the user chooses choice #3, it will convert from base 10 to base 16
  else

      System.out.println("invalid entry");
      // everything else, it is invalid
      System.out.println("final output=" + output);
      // this prints the final output.
解决方案

for a decimal x (base 10) you can use respectively for binary, octal, hex conversion

Integer.toString(x, 2),

Integer.toString(x, 8)

Integer.toString(x, 16).

then to convert it back to decimal, respectively from binary, octal, hex conversion

Integer.valueOf(binary_value, 2)

Integer.valueOf(octal_value, 8)

Integer.valueOf(hex_value, 16)

in your code, change the following:

output = Integer.toString(input, 16) //replace 16 for hex, 8 for octal, 2 for binary 

这篇关于10个基本立足在java中2,8,16转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 03:33