當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Scanner radix()用法及代碼示例


java.util.Scanner類的radix()方法返回此掃描器的默認基數。

用法:

public int radix()

返回值:此函數返回此掃描儀的默認基數。


以下示例程序旨在說明上述函數:

示例1:

// Java program to illustrate the 
// radix() method of Scanner class in Java 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        String s = "Geeksforgeeks has Scanner methods"; 
  
        // create a new scanner 
        // with the specified String Object 
        Scanner scanner = new Scanner(s); 
  
        // print a line of the scanner 
        System.out.println("Scanner String:\n"
                           + scanner.nextLine()); 
  
        // print the default radix 
        System.out.println("Default Radix value: "
                           + scanner.radix()); 
  
        // close the scanner 
        scanner.close(); 
    } 
}
輸出:
Scanner String:
Geeksforgeeks has Scanner methods
Default Radix value: 10

示例2:

// Java program to illustrate the 
// radix() method of Scanner class in Java 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        String s = "Geeksforgeeks"; 
  
        // create a new scanner 
        // with the specified String Object 
        Scanner scanner = new Scanner(s); 
  
        // print a line of the scanner 
        System.out.println("Scanner String: "
                           + scanner.nextLine()); 
  
        // print the default radix 
        System.out.println("Default Radix value: "
                           + scanner.radix()); 
  
        // change the radix of this scanner 
        scanner.useRadix(30); 
  
        System.out.println("Radix changed to 30"); 
  
        // print the default radix 
        System.out.println("Default Radix value: "
                           + scanner.radix()); 
  
        // close the scanner 
        scanner.close(); 
    } 
}
輸出:
Scanner String: Geeksforgeeks
Default Radix value: 10
Radix changed to 30
Default Radix value: 30

參考: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#radix()



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Scanner radix() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。