Task 1: Determine the Data Type of Inputs

Write a complete computer program to determine the data type of inputs. The program should:
1. Ask the user for an input.
2. Determine whether the input is a data type byte, short, int, long, float and/or double.
3. The program should keep waiting for user input until the user inputs “exit” (ignore case).

//package lab7;
import java.util.Scanner;

import javax.sound.midi.MidiDevice.Info;

public class task_1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while(in.hasNext()) {
            String input=in.next();
            String m =input.toLowerCase( );
            if(m.equals("exit")) {
                break;
            }
            try //
            {
                Byte.parseByte(input);
                System.out.println(input+" can be converted into a type byte!");
            }
            catch (Exception e) {
                System.out.println(input+" cannot be converted into a type byte!");
            }
            try //
            {
                Short.parseShort(input);
                System.out.println(input+" can be converted into a type short!");
            }
            catch (Exception e) {
                System.out.println(input+" cannot be converted into a type short!");
            }
            try //
            {
                Integer.parseInt(input);
                System.out.println(input+" can be converted into a type int!");
            }
            catch (Exception e) {
                System.out.println(input+" cannot be converted into a type int!");
            }
            try //
            {
                Float.parseFloat(input);
                System.out.println(input+" can be converted into a type float!");
            }
            catch (Exception e) {
                System.out.println(input+" cannot be converted into a type float!");
            }
            try //
            {
                 Double.parseDouble(input);
                System.out.println(input+" can be converted into a type double!");
            }
            catch (Exception e) {
                System.out.println(input+" cannot be converted into a type double!");
            }
        }
    }

}

Task 2: Octal to Hexadecimal

Write a complete program that converts octal numbers to hexadecimal numbers. The program
should:
1. Ask the user to input an octal number;
2. Check if the inputted number is a valid octal number, if it is not, the program will print a
message “Please input a valid Octal Number:” on the screen to promote the user to input
another valid octal number.
3. Convert the octal number to hexadecimal format using the methods learned in the class.
4. Keep waiting for user input until the user inputs “exit” (Ignore cases).
5. Print the hexadecimal number on the screen.

package test;

import java.util.Scanner;

public class Octal2Hex {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String octal = sc.next();
            if (octal.equalsIgnoreCase("exit")) {
                sc.close();
                break;
            }
            boolean validOctal = true;
            for (int i = 0; i < octal.length(); i++) {
                if (octal.charAt(i) > '7' || octal.charAt(i) < '0') {
                    validOctal = false;
                    break;
                }
            }
            if (validOctal) {
                String bin = "";
                for (int i = 0; i < octal.length(); i++) {
                    switch (octal.charAt(i)) {
                    case '0':
                        bin = "000";
                        break;
                    case '1':
                        bin += "001";
                        break;
                    case '2':
                        bin += "010";
                        break;
                    case '3':
                        bin += "011";
                        break;
                    case '4':
                        bin += "100";
                        break;
                    case '5':
                        bin += "101";
                        break;
                    case '6':
                        bin += "110";
                        break;
                    case '7':
                        bin += "111";
                        break;
                    }
                }
                String hex = "";
                if (bin.length() % 4 != 0) {
                    int missing = 4 - bin.length() % 4;
                    for (int i = 0; i < missing; i++) {
                        bin = "0" + bin;
                    }
                }
                for (int i = bin.length(); i >= 3; i -= 4) {
                    switch (Integer.parseInt(bin.substring(i - 4, i))) {
                    case 0:
                        hex = "0" + hex;
                        break;
                    case 1:
                        hex = "1" + hex;
                        break;
                    case 10:
                        hex = "2" + hex;
                        break;
                    case 11:
                        hex = "3" + hex;
                        break;
                    case 100:
                        hex = "4" + hex;
                        break;
                    case 101:
                        hex = "5" + hex;
                        break;
                    case 110:
                        hex = "6" + hex;
                        break;
                    case 111:
                        hex = "7" + hex;
                        break;
                    case 1000:
                        hex = "8" + hex;
                        break;
                    case 1001:
                        hex = "9" + hex;
                        break;
                    case 1010:
                        hex = "A" + hex;
                        break;
                    case 1011:
                        hex = "B" + hex;
                        break;
                    case 1100:
                        hex = "C" + hex;
                        break;
                    case 1101:
                        hex = "D" + hex;
                        break;
                    case 1110:
                        hex = "E" + hex;
                        break;
                    case 1111:
                        hex = "F" + hex;
                        break;
                    }
                }
                for (int i = 0; i < hex.length(); i++) {
                    if (hex.charAt(i) == '0')
                        hex = hex.substring(1);
                }
                System.out.println(hex);
            } else {
                System.out.println("Please input a valid Octal Number:");
            }
        }
    }
}
最后修改日期:2019年12月26日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。