流程

用户交互

Scanner

scanner是java里的一个类,用于输入输出

使用前需要new 一个scanner类的对象,通过对象来执行输入输出操作

Scanner scan = new Scanner(System.in)

获取输入

next()和nextLine()获取输入的字符串,读取前我们一般需要使用hasNext()和hasNextLine()判断是否还有输入的数据

next()
   	1.一定读取到有效字符才可以结束输入
    2.对输入有效字符之前遇到的空白,会自动将其去掉
    3.只有输入有效字符后才能将其后面输入的空白作为分隔符或者结束符
    4.next()不能得到带有空格的字符串
    
nextLine()
    1.以Enter为结束符,返回输入回车之前的所有字符
    2.可以获得空白
    
hasNextDouble
    如果扫描仪的输入中的下一个标记可以使用 nextDouble()方法将其解释为双重值,则返回true。

关闭

凡是关于IO流的类,如果不关闭就会一直占用资源,用完就关掉

对象.close();

增强for循环

for(声明语句:表达式)
{
	//代码语句
}

//Example
int[] numbers = {1,2,3,4,5};
for(int i:numbers)
{
	System.out.println(i);
}

打印三角形

public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 5-i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j <i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
posted @ 2024-07-14 20:30  SyzTak  阅读(5)  评论(0)    收藏  举报