Java Scanner radix() 方法



描述

java Scanner radix() 方法返回此掃描器的預設基數。

宣告

以下是 java.util.Scanner.radix() 方法的宣告

public int radix()

引數

返回值

此方法返回此掃描器的預設基數

異常

獲取字串示例中掃描器的預設基數

以下示例演示瞭如何使用 Java Scanner radix() 方法獲取掃描器物件的基數。我們使用給定的字串建立了一個掃描器物件。然後我們使用 nextLine() 列印字串,並使用 radix() 方法列印基數。最後,使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6.0 true ";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // print a line of the scanner
      System.out.println(scanner.nextLine());

      // print the default radix
      System.out.println(scanner.radix());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World! 3 + 3.0 = 6.0 true 
10

獲取字串示例中掃描器的自定義基數

以下示例演示瞭如何使用 Java Scanner radix() 方法獲取掃描器物件的基數。我們使用給定的字串建立了一個掃描器物件。我們使用 useRadix() 方法將基數設定為 4。然後我們使用 nextLine() 列印字串,並使用 radix() 方法列印基數。最後,使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6.0 true ";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);
	  
      // use a new radix
      scanner.useRadix(4);

      // print a line of the scanner
      System.out.println(scanner.nextLine());

      // print the default radix
      System.out.println(scanner.radix());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World! 3 + 3.0 = 6.0 true 
4

獲取使用者輸入示例中掃描器的預設基數

以下示例演示瞭如何使用 Java Scanner radix() 方法獲取掃描器物件的基數。我們使用 System.in 類建立了一個掃描器物件。然後我們使用 nextLine() 列印字串,並使用 radix() 方法列印基數。最後,使用 close() 方法關閉掃描器。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      // create a new scanner with System input
      Scanner scanner = new Scanner(System.in);

      // print a line of the scanner
      System.out.println(scanner.nextLine());

      // print the default radix
      System.out.println(scanner.radix());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:(我們輸入了 3。)

3
10
java_util_scanner.htm
廣告