• 1001 害死人不偿命的(3n+1)猜想Java


    在这里插入图片描述

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    java中Scanner的简单用法

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

    2.创建Scanner类的对象

    Scanner sc=new Scanner(System.in);
    
    • 1

    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]);
    	    	}
    	    }
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    使用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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

  • 相关阅读:
    记录贴 Elasticsearch的RestClient进行DSL查询
    【FPGA教程案例63】硬件开发板调试3——vio虚拟IO核的应用
    HTML基础
    Java如何安装https证书
    力扣hot100——第6天:32最长有效括号、33搜索旋转排序数组、34在排序数组中查找元素的第一个和最后一个位置
    Android调用相机拍照,展示拍摄的图片
    GROMACS Tutorial 5: Protein-Ligand Complex 中文实战教程
    【数据库系统概论】关系数据理论、范式
    统一SQL 支持Oracle到LightDB-Oracle特性转换
    人机交互复习专题
  • 原文地址:https://blog.csdn.net/qq_45159762/article/details/125555102