0

使用 switch case 将字母(AF 和任何其他应该说错误)转换为二进制,这是主要的:

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a letter :: ");
String letter = keyboard.next();
keyboard.getBinary();
keyboard.toString();

和班级:

import static java.lang.System.*; 
public class HexToBinary
{
private char hex;

public HexToBinary()
{
    hex=0;
}

public HexToBinary(char hexNum)
{

    hex=hexNum;
}

public void setHex(char hexNum)
{
    hex=hexNum;
}

public String getBinary()
{
    String letter = letter;
    switch(letter)
    {
        case A: letter = 1010;
        case B: letter = 1011;
        case C: letter = 1100;
        case D: letter = 1101;
        case E: letter = 1110;
        case F: letter = 1111;
        case default: letter = ERROR;
    }
    return "";
}

public String toString()
{
    System.out.println(letter+" is " getBinary + "in binary!")
    return "";
}

错误说它在“keyboard.getBinary();”期间找不到符号 不确定是什么问题

4

1 回答 1

2

错误说它在“keyboard.getBinary();”期间找不到符号 不确定是什么问题

Scanner 没有getBinary()API 会告诉您的方法:Scanner API

更重要
的是 但是你的 HexToBinary 类确实有这个方法。您将要创建一个 HexToBinary 变量,为其分配一个 HexToBinary 对象,然后在此变量上调用此方法。

所以不是:

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a letter :: ");
String letter = keyboard.next();
keyboard.getBinary();
keyboard.toString();

反而:

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a letter :: ");
String letter = keyboard.next();

// check that letter has only one char in it.
// convert your letter to a char. 
// create a HexToBinary variable and object here
// use its methods to convert the char to binary.
于 2013-10-16T18:43:36.110 回答