初学JAVA_输入函数

这篇博客介绍了JAVA编程中常用的输入函数,包括nextInt()用于读取整数,nextLine()用于读取字符串,nextDouble()用于读取双精度数,以及next()用于以空格为分隔符读取字符串。还特别提到了nextInt(int n)方法,它能生成[0, n)区间内的随机整数。" 109663358,8199791,"统计学习方法:决策树实现(ID3, C4.5, CART)
摘要由CSDN通过智能技术生成

使用import java.util.Scanner;
nextInt():输入整数 nextLine():输入字符串 nextDouble():输入双精度数 next():输入字符串(以空格作为分隔符)。

public static void main(String[] args) {
	System.out.println("-----请输入一个数字------");
	Scanner pd = new Scanner (System.in);
	int num = pd.nextInt();
	if(num%2==0)
		System.out.println("偶数");
	else
		System.out.println("奇数");
}

判断奇数偶数
nextInt(int n)方法作用是生成一个随机的int值,该值介于[0,n)的区间,也就是0到n之间的随机int值。

import java.util.Random;
import java.util.Scanner;

public class gn {
	public static void main(String[] args) {
		int randomNumber = new Random().nextInt(10);
		System.out.println("随机数已生成");
		System.out.println("------请输入您猜的数字------");
		Scanner pd = new Scanner(System.in);
		int enterNumber = pd.nextInt();
		while (enterNumber != randomN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值