2015-06-08 19 views
6
public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 

    while (input.hasNextLine()) { 
     BigInteger number = new BigInteger(input.nextLine()); 

     int bitLength = number.bitlength(); 
     if (bitLength <= Bytes.SIZE) 
      System.out.println("\u8211 byte"); 
     if (bitLength <= Short.SIZE) 
      System.out.println("\u8211 short"); 
     if (bitLength <= Int.SIZE) 
      System.out.println("\u8211 int"); 
     if (bitLength <= Long.SIZE) 
      System.out.println("\u8211 long"); 

     if (bitLength > Long.SIZE) 
      System.out.println(number + " can't be fitted anywhere."); 
    } 
} 

Nhiệm vụ: tìm một kiểu dữ liệu Sample Input phù hợp: 5tìm thích hợp Java Datatype

-150 
150000 
1500000000 
213333333333333333333333333333333333 
-100000000000000 

Sample Output:

-150 can be fitted in: 
short 
int 
long 

150000 can be fitted in: 
int 
long 

1500000000 can be fitted in: 
int 
long 
213333333333333333333333333333333333 can't be fitted anywhere. 

-100000000000000 can be fitted in: 
long 

Lỗi 1:

error: cannot find symbol 
    int bitLength = number.bitlength(); 
        ^

Lỗi 2:

symbol: method bitlength() 
location: variable number of type BigInteger 

Lỗi 3:

error: cannot find symbol 
    if (bitLength <= Int.SIZE) 
       ^
    symbol: variable Int 
    location: class Solution 
+0

'int number = input.nextInt(); 'không thể trở về một cái gì đó lớn hơn int – varren

+0

Đó là' bitLength' không 'bitlength', tôi đã cố định này trong câu trả lời của tôi. –

+0

Không đăng toàn bộ giải pháp ở đây vì nó sẽ làm cho nó quá dễ dàng cho người khác chỉ cần sao chép giải pháp của bạn. Bạn nên để đủ ở đây để giải quyết một phần của vấn đề, tức là câu hỏi ban đầu của bạn về cách đếm số bit yêu cầu. –

Trả lời

0

Lỗi: ký tự không hợp: \ 8211 trước mỗi Nếu tuyên bố

Để đưa nhân vật này trong bạn mã hóa chúng \u8211

Nếu tuyên bố và cách nhập dữ liệu vào số không thể được cung cấp trong bất kỳ loại dữ liệu nào?

Bạn cần sử dụng kiểu dữ liệu có thể cung cấp và số.

Hãy thử thay vào đó.

while (input.hasNextLine()) { 
    BigInteger number = new BigInteger(input.nextLine()); 

    int bitLength = number.bitLength() + 1; 
    if (bitLength <= Bytes.SIZE) 
     System.out.println(" \u8211 byte"); 

    if (bitLength <= Short.SIZE) 
     System.out.println(" \u8211 short"); 

    // more checks. 

    if (bitLength > Long.SIZE) 
     // too big. 

Đã giải quyết được vấn đề, còn nhiều việc phải làm để làm việc này nhưng sử dụng BigInteger.bitLength() là giải pháp thanh lịch hơn.

không thể tìm thấy biểu tượng if (bitLength < = Int.SIZE)

Không có loại Int trong Java, nó là Integer.

1

Đọc từng dòng một. Đếm bit bằng cách sử dụng BigInteger và chia nó cho 8 cho switch đơn giản hóa trường hợp. Hãy xem mã bên dưới:

Scanner input = new Scanner(new File("so/input.txt")); 
    while (input.hasNextLine()) { 
     BigInteger number = new BigInteger(input.nextLine().trim()); 
     int bitLength = number.bitLength(); 
     int len = bitLength/8; 
     StringBuilder output = new StringBuilder(number.toString() + " can be fitted in:\n"); 
     switch (len) { 
      case 0: 
       output.append(" byte"); 
      case 1: 
       output.append(" short"); 
      case 2: 
      case 3: 
       output.append(" int"); 
      case 4: 
      case 5: 
      case 6: 
      case 7: 
       output.append(" long"); 
       System.out.println(output); 
       break; 
      default: 
       System.out.println(number.toString() + " can't be fitted anywhere."); 
     } 
    } 
0

Bạn chỉ cần đặt điều kiện với phạm vi loại dữ liệu và kiểm tra xem số đầu vào có thuộc loại dữ liệu nào không.

class FindDataType { 
public static void main(String[] argh) { 
    Scanner sc = new Scanner(System.in); 
    //no. of input values 
    int t = sc.nextInt(); 
    for (int i = 0; i < t; i++) { 
     try { 
      //Take input as long data type 
      long x = sc.nextLong(); 
      System.out.println(x + " can be fitted in:"); 
      //Putting conditions to check the data type 
      if (x >= -128 && x <= 127) { 
       System.out.println("* byte"); 
       System.out.println("* short"); 
       System.out.println("* int"); 
       System.out.println("* long"); 
      } else if (x >= -32768 && x <= 32767) { 
       System.out.println("* short"); 
       System.out.println("* int"); 
       System.out.println("* long"); 
      } else if (x >= -2147483648 && x <= 2147483647) { 
       System.out.println("* int"); 
       System.out.println("* long"); 
      } else if (x >= -9223372036854775808l 
        && x <= 9223372036854775807l) { 
       System.out.println("* long"); 
      } 
     } catch (Exception e) { 
      //Printing exception if no data type matches. 
      System.out.println(sc.next() + " can't be fitted anywhere."); 
     } 

    } 
    sc.close(); 
}} 
+0

bao gồm dấu gạch chéo '}' bên trong ... :) –

Các vấn đề liên quan