1001 害死人不偿命的(3n+1)猜想Java_java中Scanner的简单用法

在这里插入图片描述

package com.zrd.patyi;
import java.util.Scanner;

public class PAT1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;
        while(n!=1){
            if (n%2==0){ n/=2; count++; }
            else{ n=(3*n+1)/2; count++; }
        }
        System.out.println(count);
    }
}

java中Scanner的简单用法

1.先导入Java.util.Scanner包
import java.util.Scanner;

2.创建Scanner类的对象

Scanner sc=new Scanner(System.in);

3.创建一个变量来接收数据

int a=sc.nextInt(); //读取int型
double b=sc.nextDouble(); //读取浮点数
float c=sc.nextFloat();  //读取浮点数
String s1=sc.nextLine();  //读取一行数据 包括空格 读取了回车结束
String s=sc.next(); //Java中next()只能获取空格之前的数据
//为了获得所有数据,我们修改输入数据的分隔符
//添加sc.useDelimiter("\n");

//导入包//
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
	    Scanner  sc=new Scanner(System.in);//从键盘接收数据//
	    sc.useDelimiter("\n"); //修改输入数据的分隔符//
	    String s=sc.next();
	    System.out.println(s);
	} 
}


//一维数组
 
import java.util.Scanner;//导入包//
public class Main {
	public static void main(String[] args) {
    	Scanner  sc=new Scanner(System.in);//从键盘接收数据//
  		int m=sc.nextInt();  //定义一维数组//
    	int []a=new int [m];
	    for(int i=0;i<m;i++)//输入一维数组//
    	{
    		a[i]=sc.nextInt();
   		}
    	for(int i=0;i<m;i++)//输出一维数组//
    	{   
    		System.out.println(a[i]);
    	}
	}
}

//二维数组:

 
import java.util.Scanner;//导入包//
public class Main {
	public static void main(String[] args) {
    	Scanner  sc=new Scanner(System.in);//从键盘接收数据// 
   		int m=sc.nextInt();
    	int n=sc.nextInt();
    	int [][]a=new int [m][n]; //定义二维数组//
	    for(int i=0;i<m;i++)  //输入二维数组//
	    {
	    	for(int j=0;j<n;j++)
	    	{
	  		  	a[i][j]=sc.nextInt();
	    	}
	    }
	    for(int i=0;i<m;i++) //输出二维数组//
	    {
	    	for(int j=0;j<n;j++)
	    	{
	    		System.out.println(a[i][j]);
	    	}
	    }
	}
}

4、Scanner中的检验
在Scanner中输入之前最好用hasNextXxx()方法进行检验

博主参考

//以#为结束符号
//单用while(sc.hasNext())的话,
import java.util.*;
public class ScannerKeyBoardTest
{
    public static void main(String[] args)
    {
        System.out.println("请输入若干单词,以空格作为分隔");
        Scanner sc = new Scanner(System.in);
        while(!sc.hasNext("#"))  //匹配#返回true,然后取非运算。即以#为结束符号
        {
            System.out.println("键盘输入的内容是:"+ sc.next());
        }
        System.out.println("会执行的");
    }
}

//整型
System.out.println("输入一个整数");
if(sc.hasNextInt()){//hasNext...()是做判断的,判断输入的数据是不是整型数据,如果是则输出,如果不是则不执行
    int num2=sc.nextInt();//next...()是用来做接收的
    System.out.println("num2:"+num2);
}

使用Scanner循环读取N个数字/字符串
当程序开始之后,会一直循环输入并打印一个数字,直到Ctrl+d结束程序
在这里sc.hasNextInt()的结果是一个boolean的类型,当结果为false是结束。
注意:
Ctrl+d用来结束循环输入多个数据

import java.util.Scanner;
public class SannerDemo {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        while (sc.hasNextInt()){
            int i = sc.nextInt();//输入数字i
            System.out.println(i);//打印数字i
        }
    }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DDouble-

你的鼓励是我最大的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值