zoukankan      html  css  js  c++  java
  • 输入、输出(iostream)

    在一个程序当中输入和输出都扮演着重要的角色,所以掌握基本输入输出是入门一门语言所必不可少的。本文主要简单叙述java的输入和输出。

    package ios;
    import java.util.Scanner;//在用到输入的时候必须引入此条语句不然会编译报错
    public class Ios {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);//输入时候的固定语句,scan可以换成别的变量名
            System.out.print("请输入今天星期几:");
            
            int week = scan.nextInt();//nextInt()从后面的Int可以看出这个只能读取int类型的数据
                                    //将空格看作是两个输入的数据的间隔
            switch (week) {
            case 1:
                System.out.println("Monday");scan.close();//输出星期换行,关闭输入,不加scan.close()会有警告
                break;
            case 2:
                System.out.println("Tuesday");scan.close();
                break;
            case 3:
                System.out.println("Wednesday");scan.close();
                break;
            case 4:
                System.out.println("Thursday");scan.close();
                break;
            case 5:
                System.out.println("Friday");scan.close();
                break;
            case 6:
                System.out.println("Saturday");scan.close();
                break;
            case 7:
                System.out.println("Sunday");scan.close();
                break;
            default:
                System.out.println("Sorry,I don't konw");scan.close();
                break;
            }
        }
    
    }

    常见的输出语句格式:

    System.out.println("10");//输出10自动换行

    System.out.print(10+” “);//输出10换行,‘+’是连接符

    System.out.printf(”%d “,10);//输出10换行,沿袭c语言的printf

    常见输入语句格式:

    英文解释:

    nextInt(): it only reads the int value, nextInt() places the cursor in the same line after reading the input.

    next(): read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same lineafter reading the input.

    nextLine():  reads input including space between the words (that is, it reads till the end of line ). Once the input is read, nextLine() positions the cursor in the next line.

    Scanner scan = new Scanner(System.in);

    scan.nextInt();//返回int类型的值,空格或者换行作为分隔符

    scan.nextDouble();//返回double类型的值,空格或者换行作为分隔符

    scan.nextLine();/返回String类型的值,换行作为分隔符

    scan.next();/返回String类型的值,空格或者换行作为分隔符

    如果想要了解具体实例的话:https://blog.csdn.net/m0_37695351/article/details/79254221

    不一样的烟火
  • 相关阅读:
    [北京.NET俱乐部]征集T恤设计方案
    [新功能]个人Blog首页分页浏览
    [公告]关于用户资料的保密
    奇怪的邮件与MSN密码
    文章发布功能改动
    [活动]北京.NET俱乐部首次活动照片及讲课资料
    [活动公告]上海.NET俱乐部首次活动预告
    [征询意见]关于开设.NET 2.0专题
    [北京.NET俱乐部活动]参加者签名并谈一下感受
    CSS3 transform 2D实验(1)
  • 原文地址:https://www.cnblogs.com/cstdio1/p/11227799.html
Copyright © 2011-2022 走看看