2015-06-08 60 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."); 
    } 
} 

Zadanie: znaleźć odpowiedni typ danych wejściowych próbki: 5Znalezienie odpowiedniego Java Typ danych

-150 
150000 
1500000000 
213333333333333333333333333333333333 
-100000000000000 

próbki wyjściowe:

-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 

Błąd 1:

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

Błąd 2:

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

Błąd 3:

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

'int liczba = input.nextInt();' nie może powrócić coś większego niż int – varren

+0

Było '' bitlength' bitLength' nie, mam stałe to w mojej odpowiedzi. –

+0

Nie publikuj tutaj całego rozwiązania, ponieważ ułatwi to innym osobom skopiowanie rozwiązania. Powinieneś zostawić tutaj wystarczająco dużo, aby rozwiązać jedną część problemu, tj. Twoje pierwotne pytanie, jak policzyć liczbę wymaganych bitów. –

Odpowiedz

0

Błąd: nielegalny charakter: \ 8211 przed każdym razie stwierdzenie

Ujmując tę ​​postać w kodować nam \u8211

Jeśli instrukcja i jak wprowadzi dane wejściowe numeru, który nie może być zakwaterowani w dowolnym typie danych?

Należy użyć typu danych, które można uwzględnić i numeru.

Spróbuj tego zamiast tego.

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. 

Uczyniwszy problem, jest dużo więcej do zrobienia, aby uzyskać to do pracy, ale stosując BigInteger.bitLength() jest bardziej eleganckie rozwiązanie.

nie może odnaleźć symbol if (bitLength < = Int.SIZE)

Nie ma typ Int w Javie, jest Integer.

1

Przeczytaj numer linii po linii. Policz bit za pomocą BigInteger i podziel go przez 8, aby uzyskać uproszczenie skrzynek switch. Spójrz na poniższy kod:

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

Możesz po prostu umieścić warunki z zakresu typów danych i sprawdzić, czy numer wejściowy przypada w którym typie danych.

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

zawierają końcowy "}" wewnątrz kodu ... :) –