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

    不一样的烟火
  • 相关阅读:
    最终项目 XMessenger Servers
    Linux下patch的制作和应用
    谈移动互联网入口
    绑定服务后台播放音频的简单示例
    MOQL操作数(Operand) (三)
    浅析Hibernate映射(二)——关系映射(3)
    【Extjs优化二】 Form表单提交通用
    Delphi Dll(1)
    用Groovy思考 第三章 Groovy开发环境
    JUnit单元测试入门(三)--JUnit简单实例
  • 原文地址:https://www.cnblogs.com/cstdio1/p/11227799.html
Copyright © 2011-2022 走看看