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

    不一样的烟火
  • 相关阅读:
    echarts labelLayout
    可视化学习及实战记录
    VS2008提示无法打开包括文件:“afxcontrolbars.h”解决办法
    原码、补码和反码
    第一篇
    vc2008编译就提示找不到msvcr90d.dll
    Vue H5 与 APP 交互 (IOS为例)
    VS Code中小程序与Vue常用插件合集(前端合集)
    如何在Element 使用正则表达式校验
    分享CSS公共类库(能在项目快捷使用CSS类)
  • 原文地址:https://www.cnblogs.com/cstdio1/p/11227799.html
Copyright © 2011-2022 走看看