本文整理汇总了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);
}
示例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();
}
}
示例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();
}
示例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());
}
示例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);
}
示例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);
}
示例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));
}
示例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;
}
示例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();
}
示例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();
}
示例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);
}
示例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();
}
示例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();
}
示例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);
}
示例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();
}