我正在学习面向对象编程,但我不知道该如何做。我只了解基本原理,却不知道怎么做。老师要求我们创建一个程序,将数字从10进制转换成2进制、8进制和16进制,然后再将其从2进制、8进制和16进制转回10进制。我从其他网站上收集了一些信息,并进行了一些编辑。
请帮忙!
我希望你们能够亲自制作并发送给我,但如果这样做太难了,请尽力指导我,因为我对Java不是很熟悉。到目前为止,我的代码如下:
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.