JavaSE learning day04

从键盘获取输入

Scanner对象

​ **next()**不能得到带有空格的字符串

​ **nextline()**可以得到带有空格字符串


package com.huang.www;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接受");
        //判断用户有没有输入字符串
        if(scanner.hasNext()){
            //使用next方法接收
            String str = scanner.next();
            System.out.println("输出内容为:"+str);
        }
        scanner.close();//凡是属于i/o流的类如果不关闭会一直占用资源,养成良好习惯用完关掉
    }
}


使用next方式接受
helloworld
输出内容为:helloworld
package com.huang.www;

import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        System.out.println("用nextline进行接受:");
        if(scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("输出内容为:"+str);
        }
        scanner.close();
    }
}


用nextline进行接受:
hello world
输出内容为:hello world

  • 使用nextInt()接受整数/hasNextInt()判断接收

  • 使用nextFloat()接受整数/hasNextFloat()判断接收

    package com.huang.www;
    
    import java.util.Scanner;
    
    public class Demo3 {
        public static void main(String[] args) {
            Scanner scanner =new Scanner(System.in);
            System.out.println("请输入接收数据:");
            int i=0;
            double j=0.0;
    
            if(scanner.hasNextInt()){
                i=scanner.nextInt();
                System.out.println("接受的整数是:"+i);
            }
            else{
                System.out.println("接收的不是整数数据.");
            }
    
            scanner.close();
        }
    }
    
    请输入接收数据:
    10S
    接收的不是整数数据.
    

    小栗子

    • 输入i个数,输出总和和均值

      package com.huang.www;
      
      import java.util.Scanner;
      
      public class Demo4 {
          public static void main(String[] args) {
              Scanner scanner =new Scanner(System.in);
              int i=0;
              double sum=0.0;
              System.out.println("请输入第"+(i+1)+"个数据:");
              while(scanner.hasNextDouble()){
      
                  i++;
                  sum+= scanner.nextDouble();
                  System.out.println("第"+i+"个输入的数据后,目前数据总和是"+sum);
              }
              System.out.println("所有"+i+"个数据总和是"+sum);
              System.out.println("所有"+i+"个数据均值是"+sum/i);
              scanner.close();
          }
      }
      
      
      请输入第1个数据:
      10
      第1个输入的数据后,目前数据总和是10.0
      20
      第2个输入的数据后,目前数据总和是30.0
      30
      第3个输入的数据后,目前数据总和是60.0
      x
      所有3个数据总和是60.0
      所有3个数据均值是20.0
      

顺序结构


选择结构

String s = scanner.nextLine()
if(s.equals("hello")){}//如果字符串s的值是hello
Switch(var){
	case value1:
		//语句1
		break;
	case value2:
		//语句2
		break;
	case value3:
		//语句3
		break;
	default:
		//语句4
}

循环结构

  • while/do while结构

  • for结构

  • switch case结构


    增强for循环

    package com.huang.www;
    
    public class Demo7 {
        public static void main(String[] args) {
            int []numbers={10,20,30,50};
            //一般输出
            for(int i=0;i<4;++i){
                System.out.println(numbers[i]+"");
            }
            System.out.println("----------\n");
            //增强for循环
            for(int x:numbers){
                System.out.println(x+" ");
            }
        }
    
    }
    
    10
    20
    30
    50
    ----------
    
    10 
    20 
    30 
    50 
    

    跳转语句

    • continue /break

    • 带标签实现的goto;

      书写格式 outer:语句1

      ​ 条件语句;

      ​ continue outer;

      package com.huang.www;
      
      public class Demo8 {
          public static void main(String[] args) {
              //打印1~100质数
              outer:for(int i=2;i<100;++i){
                  for(int j=2;j<i/2;++j){
                      if(i % j == 0)
                          continue outer;
                  }
                  System.out.println(i);
              }
          }
      }
      
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值