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


Java Scanner.hasNextInt方法代码示例

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


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

示例1: scannerDemo1

import java.util.Scanner; //导入方法依赖的package包/类
public static void scannerDemo1() {
	Scanner scan = new Scanner(System.in);
	// 从键盘接收数据
	int i = 0;
	float f = 0.0f;
	System.out.print("输入整数:");
	if (scan.hasNextInt()) {
		// 判断输入的是否是整数
		i = scan.nextInt();
		// 接收整数
		System.out.println("整数数据:" + i);
	} else {
		// 输入错误的信息
		System.out.println("输入的不是整数!");
	}
	System.out.print("输入小数:");
	if (scan.hasNextFloat()) {
		// 判断输入的是否是小数
		f = scan.nextFloat();
		// 接收小数
		System.out.println("小数数据:" + f);
	} else {
		// 输入错误的信息
		System.out.println("输入的不是小数!");
	}
}
 
开发者ID:leon66666,项目名称:JavaCommon,代码行数:27,代码来源:ScannerDemo.java

示例2: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    //get input
    Scanner sc = new Scanner(System.in).useDelimiter(" ");
    ArrayList<Integer> input = new ArrayList<>();
    //add input to ArrayList
    while (sc.hasNextInt()) {
        input.add(sc.nextInt());
    }
    //sort the list
    Collections.sort(input);
    //initialize closest values, with the first two elements in the list
    int closest = input.get(1)-input.get(0);
    //check all elements in the list
    for(int i = 2; i < input.size(); i++) {
        //change closest if we find two elements with a shorter distance.
        closest = Math.min(closest, input.get(i)-input.get(i-1));
    }
    //print the shortest distance
    System.out.println(closest);
}
 
开发者ID:BaReinhard,项目名称:Hacktoberfest-Data-Structure-and-Algorithms,代码行数:21,代码来源:Closest.java

示例3: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter your age in years: ");
    double age = input.nextDouble();
    System.out.print("Enter your maximum heart rate: ");
    double rate = input.nextDouble();
    double fb = (rate - age) * 0.65;
    System.out.println("Your ideal fat-burning heart rate is " + fb);
    
    System.out.print("\nPlease enter an Integer: ");
    while(!input.hasNextInt()) {
        input.nextLine();
        System.out.print("Invalid integer; please enter an integer: ");
    }
    int i = input.nextInt();
    System.out.println("Done");
}
 
开发者ID:Driveron,项目名称:Notes,代码行数:18,代码来源:InputExample.java

示例4: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    while (in .hasNextInt()) {
        int n = in .nextInt();
        int p = in .nextInt();
        MyCalculator my_calculator = new MyCalculator();
        try {
            System.out.println(my_calculator.power(n, p));
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
 
开发者ID:rshaghoulian,项目名称:HackerRank_solutions,代码行数:14,代码来源:Solution.java

示例5: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
    Scanner x = new Scanner(System.in);
    while (x.hasNextInt()) {
        int d = x.nextInt();
        if (d != 0) {
            func(d);
        } else {
            System.exit(0);
        }
    }
}
 
开发者ID:bakhodirsbox,项目名称:AlgoCS,代码行数:12,代码来源:Maximum_1079.java

示例6: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
	ProgressFrame pf = new ProgressFrame();
	pf.showProgress();
	
	Scanner s = new Scanner(System.in);
	while (s.hasNextInt()) {
		pf.setProgress(s.nextInt()/100d, "test");
	}
	s.close();
	System.exit(0);
}
 
开发者ID:CalebKussmaul,项目名称:GIFKR,代码行数:12,代码来源:ProgressFrame.java

示例7: main

import java.util.Scanner; //导入方法依赖的package包/类
/**
    * this class is the main function of the program
    * it gets input of the number of the resources and the costumer threads
    * and it creates the universe where the thread's actions take place
    *
    * @param args
    */
   public static void main(String[] args) {
   	//taking two inputs and checks if the second one
   	//which are the number of the costumers are bigger than the number
   	//of the resources
   	sc = new Scanner(System.in);
       System.out.print("Enter number of resources: ");
       while (!sc.hasNextInt()) sc.next();
       int resourcesNum = sc.nextInt();
       int costumersNum;
       System.out.print("Enter number of costumers: ");
       do {
           while (!sc.hasNextInt()) sc.next();
           costumersNum = sc.nextInt();
       } while (costumersNum < resourcesNum);
       System.out.println(resourcesNum + " " + costumersNum);
   	
//after taking the inputs
       //create the universe with these parameters
   	try{
   		Rap_universe universe = new Rap_universe(resourcesNum, costumersNum);
           universe.start();
   	}catch (Exception e) {
   		System.err.println("InterruptedException occured");
	}
       
   }
 
开发者ID:vamartid,项目名称:R.A.P.,代码行数:34,代码来源:Rap.java

示例8: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args)
{
	int intergral;
	Scanner sc = new Scanner(System.in);
	System.out.print("请输入会员积分:");
	
	if (sc.hasNextInt() ) 
	{
		intergral = sc.nextInt();
		if (intergral < 0) 
		{
			System.out.print("请输入正确的会员积分");
		}
		else if (intergral < 2000) 
		{
			System.out.print("该会员享受的折扣是:0.9");
		}
		else if (intergral < 4000) 
		{
			System.out.print("该会员享受的折扣是:0.8");
		}
		else if (intergral < 8000) 
		{
			System.out.print("该会员享受的折扣是:0.7");
		}
		else 
		{
			System.out.print("该会员享受的折扣是:0.6");
		}

	}
	else   
	{
		System.out.print("请输入正确的会员积分");
	}
	sc.close();


}
 
开发者ID:JAVA201708,项目名称:Homework,代码行数:40,代码来源:Discount.java

示例9: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args)
{
	int integral;
	double discount;
	Scanner sc = new Scanner(System.in);
	System.out.print("请输入会员积分:");
	if (sc.hasNextInt())
	{
		integral = sc.nextInt();
		if(integral >= 8000)
		{
			discount = 0.6;
			System.out.println("该会员享受的折扣是:" + discount);
		}
		else if(integral < 8000 && integral >= 4000)
		{
			discount = 0.7;
			System.out.println("该会员享受的折扣是:" + discount);
		}
		else if(integral < 4000 && integral >= 2000)
		{
			discount = 0.8;
			System.out.println("该会员享受的折扣是:" + discount);
		}
		else
		{
			discount = 0.9;
			System.out.println("该会员享受的折扣是:" + discount);
		}
	}
	else
	{
		System.out.println("请输入正确的数字!");
	}
	sc.close();
}
 
开发者ID:JAVA201708,项目名称:Homework,代码行数:37,代码来源:MemberDiscount.java

示例10: main

import java.util.Scanner; //导入方法依赖的package包/类
public static void main(String[] args) {
	// TODO Auto-generated method stub
	//DECLARACIÓN DE VARIABLES
	Scanner teclado = new Scanner(System.in);
	int num=0;
	double result=0;
	boolean control=true;
	
	//CONTROL DEL VALOR INTRODUCIDO
	while (control) {
		System.out.println("Hola, introduce un valor");
		if (teclado.hasNextInt()) {
			num = teclado.nextInt();
			//CONTROL VALOR POSITIVO
			if (num>=0) {
				//OPERACIÓN RAÍZ CUADRADA
				result = Math.sqrt(num);
				System.out.println("El resultado es "+result);
				control = false;
			}else {
				System.out.println("El valor introducido tiene que ser positivo");
			}	
		}else {
			System.out.println("El valor introducido tiene que ser numérico.");
			teclado.next();
		}
	}
	teclado.close();
}
 
开发者ID:xaviercamps,项目名称:Initial,代码行数:30,代码来源:ValidarDatos.java

示例11: Instructions

import java.util.Scanner; //导入方法依赖的package包/类
Instructions(String programFilename) throws FileNotFoundException{
    File file = new File(programFilename);
    Scanner scanner = new Scanner(file);
    String nextLine;
    instructions = new LinkedList<CopyJumpInstruction>();
    while(scanner.hasNextInt()){
        instructions.add(
                new CopyJumpInstruction(
                    new CopyInstruction(scanner.nextInt(), scanner.nextInt()),
                    new JumpInstruction(scanner.nextInt(), scanner.nextInt())
                )
        );
    }
}
 
开发者ID:arkenidar,项目名称:copyjumpvm,代码行数:15,代码来源:Instructions.java

示例12: main

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

 while ( in .hasNextInt()) {
  int n = in .nextInt();
  int p = in .nextInt();
  MyCalculator my_calculator = new MyCalculator();
  try {
   System.out.println(my_calculator.power(n, p));
  } catch (Exception e) {
   System.out.println(e);
  }
 }
}
 
开发者ID:hell-sing,项目名称:hacker-rank,代码行数:15,代码来源:Exception_Handling.java

示例13: input

import java.util.Scanner; //导入方法依赖的package包/类
/**
 * 读取输入
 * @return
 */
public static Comparable[] input(){
    Scanner scanner = new Scanner(Sort.class.getClassLoader().getResourceAsStream("tinysort.txt"));
    int count = 0;
    List<Integer> arrlist = new ArrayList<>();
    while (scanner.hasNextInt()){
        arrlist.add(scanner.nextInt());
        count++;
    }
    scanner.reset();
    Integer[] arr = new Integer[arrlist.size()];
    arrlist.toArray(arr);
    return arr;
}
 
开发者ID:wz12406,项目名称:accumulate,代码行数:18,代码来源:Sort.java

示例14: evaluate

import java.util.Scanner; //导入方法依赖的package包/类
public static int evaluate(String expression)
{
	Scanner sc = new Scanner(expression);
	StackInterface<Integer> stack = new LinkedStack<Integer>();  

	int value;
	String operator;
	int operand1, operand2;
	int result = 0;

	while (sc.hasNext())
	{
		if (sc.hasNextInt())
		{
			// Process operand.
			value = sc.nextInt();
			if (stack.isFull())
				throw new PostFixException("Too many operands-stack overflow");
			stack.push(value);
		}
		else
		{
			// Process operator.
			operator = sc.next();

			// Check for illegal symbol
			if (!(operator.equals("/") || operator.equals("*") ||
					operator.equals("+") || operator.equals("-")))
				throw new PostFixException("Illegal symbol: " + operator);

			// Obtain second operand from stack.
			if (stack.isEmpty())
				throw new PostFixException("Not enough operands-stack underflow");
			operand2 = stack.peek();
			stack.pop();

			// Obtain first operand from stack.
			if (stack.isEmpty())
				throw new PostFixException("Not enough operands-stack underflow");
			operand1 = stack.peek();
			stack.pop();

			// Perform operation.
			if (operator.equals("/"))
				result = operand1 / operand2;
			else
				if(operator.equals("*"))
					result = operand1 * operand2;
				else
					if(operator.equals("+"))
						result = operand1 + operand2;
					else
						if(operator.equals("-"))
							result = operand1 - operand2;

			// Push result of operation onto stack.
			stack.push(result);
		}
	}


	// Obtain final result from stack. 
	if (stack.isEmpty())
		throw new PostFixException("Not enough operands-stack underflow");
	result = stack.peek();
	stack.pop();

	// Stack should now be empty.
	if (!stack.isEmpty())
		throw new PostFixException("Too many operands-operands left over");

	// Return the final.
	return result;
}
 
开发者ID:YC-S,项目名称:CS401,代码行数:75,代码来源:PostFixEvaluator.java

示例15: run

import java.util.Scanner; //导入方法依赖的package包/类
@Override
public boolean run(DiscordBot bot, GuildMessageReceivedEvent event, String args) {
    Scanner messageIterator = new Scanner(args);

    Member member = event.getMember();
    Message message = event.getMessage();
    TextChannel channel = event.getChannel();
    Guild guild = event.getGuild();
    Member selfMember = guild.getSelfMember();

    if (!member.hasPermission(Permission.MESSAGE_MANAGE)) {
        DiscordUtils.failMessage(bot, message, "You don't have enough permissions to execute this command! Required permission: Manage Messages");
        return false;
    }

    if (!selfMember.hasPermission(Permission.MESSAGE_MANAGE)) {
        DiscordUtils.failMessage(bot, message, "I don't have enough permissions to do that!");
        return false;
    }

    if (!messageIterator.hasNextInt()) {
        DiscordUtils.failReact(bot, message);
        return true;
    }

    int messageCount = messageIterator.nextInt();

    if (messageCount < 1) {
        DiscordUtils.failMessage(bot, message, "You can't delete zero or negative messages.");
        return false;
    } else if (messageCount > 100) {
        DiscordUtils.failMessage(bot, message, "You can't delete more than 100 messages at once.");
        return false;
    }

    String targetArgument;
    User targetUser = null;

    if (!messageIterator.hasNext()) {
        targetArgument = "";
    } else if (messageIterator.hasNext(DiscordUtils.USER_MENTION_PATTERN)) {
        targetUser = message.getMentionedUsers().get(0);
        targetArgument = "user";
    } else {
        targetArgument = messageIterator.next();
    }

    List<Message> messages;
    switch (targetArgument) {
        case "":
            messages = fetchMessages(channel, messageCount, true, false, false, null);
            break;
        case "bot":
            messages = fetchMessages(channel, messageCount, false, true, false, null);
            break;
        case "user":
            if (targetUser != null) {
                messages = fetchMessages(channel, messageCount, true, false, true, targetUser);
                break;
            }
        default:
            DiscordUtils.failMessage(bot, message, "Invalid target, please try mentioning a user or writing `bot`.");
            return false;
    }

    Pair<List<Message>, List<Message>> seperatedMessages = seperateMessages(messages);
    bulkDelete(seperatedMessages, channel);
    DiscordUtils.successReact(bot, message);

    return false;
}
 
开发者ID:Samoxive,项目名称:SafetyJim,代码行数:72,代码来源:Clean.java


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