当前位置: 首页>>代码示例>>Java>>正文


Java Scanner.nextInt方法代码示例

本文整理汇总了Java中java.util.Scanner.nextInt方法的典型用法代码示例。如果您正苦于以下问题:Java Scanner.nextInt方法的具体用法?Java Scanner.nextInt怎么用?Java Scanner.nextInt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.Scanner的用法示例。


在下文中一共展示了Scanner.nextInt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int number = in.nextInt();
    int[] array = new int[number];

    for (int i = 0; i < array.length; i++) {
        array[i] = in.nextInt();
    }

    int min1 = 2000000005, min2 = 2000000005, max1, max2;
    for (int i = 0; i < array.length; i++) {
        if (i - 1 >= 0)
            min1 = Math.abs(array[i] - array[i - 1]);
        if (i + 1 < array.length)
            min2 = Math.abs(array[i] - array[i + 1]);

        max1 = Math.abs(array[0] - array[i]);
        max2 = Math.abs(array[array.length - 1] - array[i]);

        System.out.println(Math.min(min1, min2) + " " + Math.max(max1, max2));
        min1 = 2000000005;
        min2 = 2000000005;
    }
}
 
开发者ID:AlbertHambardzumyan,项目名称:codeforces,代码行数:25,代码来源:Problem_567A.java

示例2: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()){
        int n = sc.nextInt();
        int minPower = 0;
        Queue<Integer> queue = new PriorityQueue<>();

        for(int i=0; i<n; i++){
            queue.add(sc.nextInt());
        }

        int first = 0, second = 0, temp = 0;
        while (queue.size() > 1){
            first = queue.poll();
            second = queue.poll();
            temp = first + second;
            queue.add(temp);

            minPower += temp;
        }
        System.out.println(minPower);
    }
}
 
开发者ID:mayuanucas,项目名称:51nod,代码行数:24,代码来源:Solution.java

示例3: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int T = scan.nextInt();
    while (T-- > 0) {
        int N = scan.nextInt();
        char [][] grid = new char[N][N];
        for (int i = 0; i < N; i++) {
            String str = scan.next();
            for (int j = 0; j < str.length(); j++) {
                grid[i][j] = str.charAt(j);
            }
        }
        System.out.println(canConvert(grid) ? "YES" : "NO");
    }
    scan.close();
}
 
开发者ID:rshaghoulian,项目名称:HackerRank_solutions,代码行数:17,代码来源:Solution.java

示例4: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    /* Save input */
    Scanner scan = new Scanner(System.in);
    int m = scan.nextInt();
    int n = scan.nextInt();
    String [] magazine = new String[m];
    for (int i = 0; i < m; i++) {
        magazine[i] = scan.next();
    }
    String [] ransom = new String[n];
    for (int i = 0; i < n; i++) {
        ransom[i] = scan.next();
    }
    scan.close();
    
    /* Attempt to create a ransom letter */
    if (magazine.length < ransom.length) {
        System.out.println("No");
    } else {
        canCreate(magazine, ransom);
    }
}
 
开发者ID:MohamedSondo,项目名称:ACE_HackerRank,代码行数:23,代码来源:Solution.java

示例5: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    Scanner x = new Scanner(System.in);
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
    int n = x.nextInt(), c;
    int a[] = new int[701];
    for (int i = 0; i < n; i++) {
        c = x.nextInt();
        a[c]++;
    }
    int s = 0;
    for (int i = 600; i < a.length; i++) {
        s += a[i] / 4;
    }
    writer.print(s);
    writer.close();
}
 
开发者ID:bakhodirsbox,项目名称:AlgoCS,代码行数:17,代码来源:FlatSpots_1617.java

示例6: 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 numRotations = scan.nextInt();
    int array[] = new int[size];
    for (int i = 0; i < size; i++) {
        array[i] = scan.nextInt();
    }
    scan.close();
    
    /* Rotate array (in place) using 3 reverse operations */
    numRotations %= size; // to account for numRotations > size
    int rotationSpot = size - 1 - numRotations;
    reverse(array, 0, size - 1);
    reverse(array, 0, rotationSpot);
    reverse(array, rotationSpot + 1, size - 1);
    
    /* Print rotated array */
    for (int i = 0; i < size; i++) {
        System.out.print(array[i] + " ");
    }
}
 
开发者ID:rshaghoulian,项目名称:HackerRank_solutions,代码行数:24,代码来源:Solution.java

示例7: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args)
{
	Scanner sc = new Scanner(System.in);
	System.out.println("请输入4位会员卡号:");
	int a = sc.nextInt();
	int gewei = a % 10;
	int shiwei = a / 10 % 10;
	int baiwei = a / 100 % 10;
	int qianwei = a / 1000;
	System.out.println("会员卡号" + a + "各位之和" + (gewei + shiwei + baiwei + qianwei));
	boolean cus = true;
	System.out.println("是幸运客户吗?");
	if ((gewei + shiwei + baiwei + qianwei) > 20)
	{
		System.out.println(cus);
	} else
	{
		cus = false;
		System.out.println(cus);
	}
}
 
开发者ID:JAVA201708,项目名称:Homework,代码行数:22,代码来源:Test5.java

示例8: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    //System.out.println("Enter the number of items");
    int N = sc.nextInt();
    //System.out.println("Enter the capacity of the knapsack");
    int C = sc.nextInt();
    int[] W = new int[N];
    int[] V = new int[N];
    int[] L = new int[N];

    //System.out.println("Enter the weight, value and upper bound of each item");
    for (int i = 0; i < N; i++) {
        W[i] = sc.nextInt();
        V[i] = sc.nextInt();
        L[i] = sc.nextInt();
    }

    boolean optimize = true;
    int solution = convertToZeroOne(N, W, V, L, C, optimize);

    //System.out.println("The maximum value that the knapsack can store is: ");
    System.out.println(solution);
}
 
开发者ID:kowshik-sundararajan,项目名称:CS3230,代码行数:24,代码来源:BoundedKnapsack.java

示例9: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    int n, k = 0;
    Scanner x = new Scanner(System.in);
    n = x.nextInt();
    int a[][] = new int[n][n];
    for (int i = 0; i < 2 * n - 1; i++) {
        for (int j = 0; j <= i; j++) {
            if (j < n && n - i + j - 1 >= 0 && n - i + j - 1 < n) a[j][n - i + j - 1] = ++k;
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print(a[i][j] + " \t");
        }
        System.out.println();
    }
}
 
开发者ID:bakhodirsbox,项目名称:AlgoCS,代码行数:18,代码来源:Hotel_1319.java

示例10: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[]args){
boolean sc;

Scanner input = new Scanner(System.in);
System.out.println("请输入4位会员卡号:\n");
int member = input.nextInt();

int gewei = member%10;
int shiwei = member/10%10;
int baiwei = member/100%10;
int qianwei =member/1000;

member = gewei+shiwei+baiwei+qianwei;
sc = member > 20;
System.out.println("是幸运顾客吗?"+sc);
}
 
开发者ID:JAVA201708,项目名称:Homework,代码行数:17,代码来源:lucky.java

示例11: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int total = sc.nextInt();
    int[] arr = new int[total];

    for(int i=0; i<total; i++){
        arr[i] = sc.nextInt();
    }
    Arrays.sort(arr);

    for(int i=0; i<total; i++){
        System.out.println(arr[i]);
    }
    
}
 
开发者ID:mayuanucas,项目名称:51nod,代码行数:16,代码来源:Solution.java

示例12: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) 
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.close();
System.out.println(factorial(n));
}
 
开发者ID:Kvaibhav01,项目名称:HackerRank-in-Java,代码行数:8,代码来源:Recursion.java

示例13: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int L = scan.nextInt();
    int R = scan.nextInt();
    scan.close();
    
    int xored  = L ^ R;
    int significantBit = 31 - Integer.numberOfLeadingZeros(xored);
    int result = (1 << (significantBit + 1)) - 1;

    System.out.println(result);
}
 
开发者ID:MohamedSondo,项目名称:ACE_HackerRank,代码行数:13,代码来源:Solution.java

示例14: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int T = scan.nextInt();
    while (T-- > 0) {
        int A = scan.nextInt();
        int B = scan.nextInt();
        int numSquares = (int) (Math.floor(Math.sqrt(B)) - Math.ceil(Math.sqrt(A)) + 1);
        System.out.println(numSquares);
    }
    scan.close();
}
 
开发者ID:rshaghoulian,项目名称:HackerRank_solutions,代码行数:12,代码来源:Solution.java

示例15: acceptChoice

import java.util.Scanner; //导入方法依赖的package包/类
private static Integer acceptChoice(Scanner reader){
	System.out.println("************Advanced Calculator************");
	System.out.println("1. Prime Number check");
	System.out.println("2. Even Number check");
	System.out.println("3. Sum of N Primes");
	System.out.println("4. Sum of N Evens");
	System.out.println("5. Sum of N Odds");
	System.out.println("6. Exit");
	System.out.println("Enter the number to choose operation");
	return reader.nextInt();
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Cookbook,代码行数:12,代码来源:Calculator.java


注:本文中的java.util.Scanner.nextInt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。