使用扫描仪和开关案例无法找到符号

时间:2013-10-16 18:42:47

标签: java java.util.scanner

使用开关案例将字母(A-F和任何其他人应该说错误)转换为二进制文件 这是主要的:

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();”期间找不到符号 不确定是什么问题

1 个答案:

答案 0 :(得分:2)

  

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

扫描程序没有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.