當前位置: 首頁>>代碼示例>>Java>>正文


Java Scanner.close方法代碼示例

本文整理匯總了Java中java.util.Scanner.close方法的典型用法代碼示例。如果您正苦於以下問題:Java Scanner.close方法的具體用法?Java Scanner.close怎麽用?Java Scanner.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.Scanner的用法示例。


在下文中一共展示了Scanner.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    /* Read and save input as LocalDates */
    Scanner scan = new Scanner(System.in);
    LocalDate returnDate = readDate(scan);
    LocalDate expectDate = readDate(scan);
    scan.close();
    
    /* Calculate fine */
    int fine;
    if (returnDate.isEqual(expectDate) || returnDate.isBefore(expectDate)) {
        fine = 0;
    } else if (returnDate.getMonth() == expectDate.getMonth() && returnDate.getYear() == expectDate.getYear()) {
        fine = 15 * (returnDate.getDayOfMonth() - expectDate.getDayOfMonth());
    } else if (returnDate.getYear() == expectDate.getYear()) {
        fine = 500 * (returnDate.getMonthValue() - expectDate.getMonthValue());
    } else {
        fine = 10000;
    }
    System.out.println(fine);
}
 
開發者ID:MohamedSondo,項目名稱:ACE_HackerRank,代碼行數:21,代碼來源:Solution.java

示例2: readFile

import java.util.Scanner; //導入方法依賴的package包/類
public static String readFile(String pathname) throws IOException {
	// NOTE: drops newlines
	StringBuilder fileContents = new StringBuilder();
	InputStream in = Thread.currentThread().getContextClassLoader()
			.getResourceAsStream(pathname);
	if (in == null) {
		throw new IOException("no resource found at " + pathname);
	}
	Scanner scanner = new Scanner(in);
	try {
		while (scanner.hasNextLine()) {
			fileContents.append(scanner.nextLine());
		}
		return fileContents.toString();
	} finally {
		scanner.close();
	}
}
 
開發者ID:DreamBlocks,項目名稱:DreamBlocks,代碼行數:19,代碼來源:StockMethods.java

示例3: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    /* Save input */
    Scanner scan = new Scanner(System.in);
    int size     = scan.nextInt();
    int k        = scan.nextInt();
    int q        = scan.nextInt();
    int [] array = new int[size];
    for (int i = 0; i < size; i++) {
        array[i] = scan.nextInt();
    }
    
    /* Rotate array (in place) using 3 reverse operations */
    k %= size; // to account for k > size
    reverse(array, 0, array.length - 1);
    reverse(array, 0, k - 1);
    reverse(array, k, array.length - 1);
    
    /* Print output */
    while (q-- > 0) {
        int m = scan.nextInt();
        System.out.println(array[m]);
    }
    scan.close();
}
 
開發者ID:MohamedSondo,項目名稱:ACE_HackerRank,代碼行數:25,代碼來源:Solution.java

示例4: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    
    /* Initialize BitSet */
    BitSet bitset = new BitSet(NUM_ELEMENTS);
    bitset.set(0, NUM_ELEMENTS);

    /* Create a BitSet for each rock. "AND" it with our original BitSet */
    for (int r = 0; r < N; r++) {
        String rock = scan.next();
        BitSet currBitSet = new BitSet(NUM_ELEMENTS);
        for (int i = 0; i < rock.length(); i++) {
            currBitSet.set(rock.charAt(i) - 'a');
        }
        bitset.and(currBitSet);
    }
    
    scan.close();
    System.out.println(bitset.cardinality());
}
 
開發者ID:MohamedSondo,項目名稱:ACE_HackerRank,代碼行數:22,代碼來源:Solution.java

示例5: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    /* Save input */
    Scanner scan = new Scanner(System.in);
    int s = scan.nextInt();
    int n = scan.nextInt();
    int m = scan.nextInt();
    int[] keyboards = new int[n];
    for (int i = 0; i < n; i++) {
        keyboards[i] = scan.nextInt();
    }
    int[] drives = new int[m];
    for (int i = 0; i < m; i++) {
        drives[i] = scan.nextInt();
    }
    scan.close();
    
    /* Calculate result */
    int moneySpent = getMoneySpent(keyboards, drives, s);
    System.out.println(moneySpent);
}
 
開發者ID:rshaghoulian,項目名稱:HackerRank_solutions,代碼行數:21,代碼來源:Solution.java

示例6: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int s = scan.nextInt();
    int[] array = new int[s];
    for (int i = 0; i < s; i++) {
        array[i] = scan.nextInt(); 
    }
    scan.close();
    insertionSortShifts(array);
}
 
開發者ID:MohamedSondo,項目名稱:ACE_HackerRank,代碼行數:11,代碼來源:Solution.java

示例7: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    /* Save input grid */
    Scanner scan = new Scanner(System.in);
    rows = scan.nextInt();
    cols = scan.nextInt();
    int [][] grid = new int[rows][cols];
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            grid[row][col] = scan.nextInt();
        }
    }
    scan.close();

    System.out.println(largestRegion(grid));
}
 
開發者ID:MohamedSondo,項目名稱:ACE_HackerRank,代碼行數:16,代碼來源:Solution.java

示例8: getBankList

import java.util.Scanner; //導入方法依賴的package包/類
/**
 * @return Bank List JSON in String format
 * @throws IOException
 */
public String getBankList() throws IOException {
	String responseBody = null;
	URL url = new URL(BASE_URL + "/listbanks");
	connection = (HttpURLConnection) url.openConnection();
	connection.setRequestMethod("GET");
	/*
	 * connection.setUseCaches(false); connection.setDoOutput(true);
	 */
	InputStream response = connection.getInputStream();
	Scanner scanner = new Scanner(response);
	responseBody = scanner.useDelimiter("\\A").next();
	scanner.close();
	return responseBody;
}
 
開發者ID:arjunnayak0705,項目名稱:java-indian-bank-api-wrapper,代碼行數:19,代碼來源:BankDetails.java

示例9: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) throws FileNotFoundException {
    File ogrencilerDosya = new File(".\\config\\Ogrenciler.txt"); 
    // Mac: "./config/Ogrenciler.txt"
    
    if(!ogrencilerDosya.exists() || !ogrencilerDosya.canRead()
            || !ogrencilerDosya.isFile() ) {
        System.out.println("Dosya yok ya da okuma hakkı yok." 
                + ogrencilerDosya.getAbsolutePath() );
        return;
    }
    
    Scanner dosyaOkuyucu = new Scanner(ogrencilerDosya);

    File ciktiDosyasi = new File(".\\config\\Ogrenciler_cikti.txt"); 
    // Mac: "./config/Ogrenciler_cikti.txt"
    
    if (!ciktiDosyasi.canWrite()) {
        System.out.println("Yazma hakki yok");
        return;
    }
    
    PrintWriter dosyaYazici = new PrintWriter(ciktiDosyasi);

    // Dosyaya ek yapmak icin:
    // FileOutputStream fos = new FileOutputStream(ciktiDosyasi);
    // PrintWriter dosyaYazici2 = new PrintWriter(fos, true);
    for (int i = 0; dosyaOkuyucu.hasNextLine(); i++) {
        String mevcutSatir = dosyaOkuyucu.nextLine();
        System.out.println(mevcutSatir);
        dosyaYazici.println("Satir " + (i + 1) + ": " + mevcutSatir);
    }

    dosyaYazici.close();
    dosyaOkuyucu.close();
}
 
開發者ID:ozkansari,項目名稱:MyCourses,代碼行數:36,代碼來源:DosyaOkumaOrnegiSade.java

示例10: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int testCases = scan.nextInt();
    scan.nextLine(); // gets rid of the pesky newline.
    while (testCases-- > 0) {
       String pattern = scan.nextLine();
       try {
           Pattern.compile(pattern);
           System.out.println("Valid");
       } catch (PatternSyntaxException exception) {
           System.out.println("Invalid");
       }
    }
    scan.close();
}
 
開發者ID:rshaghoulian,項目名稱:HackerRank_solutions,代碼行數:16,代碼來源:Solution.java

示例11: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    int K = scan.nextInt();
    ArrayList<Integer> contest = new ArrayList<>(N);
    int savedLuck = 0;
    for (int i = 0; i < N; i++) {
        int luck = scan.nextInt();
        int importance = scan.nextInt();
        if (importance == 0) {
            savedLuck += luck; // lose every non-important contest
        } else {
            contest.add(luck);
        }
    }
    scan.close();
    
    /* Compete in "important" contests */
    quickselect(contest, contest.size() - K);
    for (int i = 0; i < contest.size(); i++) {
        if (i < contest.size() - K) {
            savedLuck -= contest.get(i); // win contest
        } else {
            savedLuck += contest.get(i); // lose contest
        }
    }
    System.out.println(savedLuck);
}
 
開發者ID:MohamedSondo,項目名稱:ACE_HackerRank,代碼行數:29,代碼來源:Solution.java

示例12: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int p = scan.nextInt();
    while (p-- > 0) {
        int n = scan.nextInt();
        System.out.println(isPrime(n) ? "Prime" : "Not prime");
    }
    scan.close();
}
 
開發者ID:rshaghoulian,項目名稱:HackerRank_solutions,代碼行數:10,代碼來源:Solution.java

示例13: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    in.nextLine();

    for (int i = 0; i < N; i++) {
        String string = in.nextLine();
        char[] charArray = string.toCharArray();

        for (int j = 0; j < charArray.length; j++) {
            if (j % 2 == 0) {
                System.out.print(charArray[j]); //For even
            }
        }

        System.out.print(" ");

        for (int j = 0; j < charArray.length; j++) {
            if (j % 2 != 0) {
                System.out.print(charArray[j]); //For odd
            }
        }

        System.out.println();
    }

    in.close();
}
 
開發者ID:Kvaibhav01,項目名稱:HackerRank-in-Java,代碼行數:29,代碼來源:Solution.java

示例14: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    int [] array = new int[n];
    for (int i = 0; i < n; i++) {
        array[i] = scan.nextInt();
    }
    scan.close();
    medianTracker(array);
}
 
開發者ID:rshaghoulian,項目名稱:HackerRank_solutions,代碼行數:11,代碼來源:Solution.java

示例15: main

import java.util.Scanner; //導入方法依賴的package包/類
public static void main(String[] args) 
{
	Scanner sc = new Scanner(System.in);

	String isVip;
	double money;
	double discount;

	System.out.println("請輸入是否是會員:是(y)/否(其他字符)");
	isVip = sc.next();
	System.out.println("請輸入購物金額:");
	money = sc.nextDouble();
	if (isVip.equals("y")) 
	{
		if (money >= 200) 
		{
			discount = 0.75; 
		}
		else 
		{
			discount = 0.8;
		}
	}
	else if (money >= 100) 
	{
		discount = 0.9;
	}
	else 
	{
		discount = 1;
	}
	System.out.println("實際支付:" + money * discount);

	sc.close();


}
 
開發者ID:JAVA201708,項目名稱:Homework,代碼行數:38,代碼來源:Pay.java


注:本文中的java.util.Scanner.close方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。