首页 > 编程语言 >Java基础语法

Java基础语法

时间:2023-04-02 14:44:26浏览次数:41  
标签:Java String int 基础 System 语法 println public out

用户交互Scanner

imige

实验

import java.util.Scanner;
public class Dome01 {
    public static void main(String[] args) {
        Scanner scanner =new Scanner (System.in);
        System.out.println("使用Next方式接受");
        if(scanner.hasNext()){
            String str = scanner.next();
            System.out.println("输出的内容为:"+str);
        }

    }
}

import java.util.Scanner;
public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next line方式接收:");
        if(scanner.hasNextLine()){
            String str =scanner.nextLine();
            System.out.println("输出内容为:"+str);
        }
    }
}


![]

package control;
import java.util.Scanner;
public class Dome04 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int x = 0;
        float f = 0.0f;
        System.out.println("请输入整数:");
        if (s.hasNextInt()) {
            x = s.nextInt();
            System.out.println("你输入的整数是:" + x);
        } else {
            System.out.println("你输入的不是整数");
        }
        System.out.println("请输入小数:");
        if(s.hasNextFloat()){
           f= s.nextFloat();
        System.out.println("你输入的小数是:"+f);
        }else{
        System.out.println("你输入的不是小数");
             }
    s.close();
    }

}


package control;
import java.util.Scanner;
public class Demo03 {
    public static void main(String[] args) {
        System.out.println("请依次输入数字:");
        Scanner s  = new Scanner(System.in);
        int x=0;
        double sum=0;
        int c=0;
        while(s.hasNextDouble()){
            double a = s.nextDouble();
            x=x+1;
            sum=sum+a;
            System.out.println("你输入的第"+x+"的和为"+sum);
        }
        c=1+x;
        System.out.println("结束原因是你输入的第"+c+"个数不是数字");
        System.out.println("你总共输入"+x+"个数的平均值为:"+sum/x);
        s.close();
    }
}

顺序结构

            System.out.println("hello1");//顺序结构
            System.out.println("hello2");
            System.out.println("hello3");
            System.out.println("hello4");
            System.out.println("hello5");
            System.out.println("hello6");

选择结构


package control;
import java.util.Scanner;
public class ShunZeDemo {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String str=s.nextLine();
        if(str.equals("hello")){//"equals":是判断字符串是否相等;
            System.out.println(str);
        }else{
            System.out.println("错误");
        }
        s.close();

    }
}



package control;
import java.util.Scanner;
public class ShunZeDome02 {
    public static void main(String[] args) {
        //考试分数大于60就及格,小于60就不及格;
        Scanner s = new Scanner(System.in);
        System.out.println("请输入你的分数:");
        int score =s.nextInt();
        if(score>60){//score:成绩;
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }
        s.close();
    }
}




package control;
import java.util.Scanner;
public class ShunZeDemo03 {
    public static void main(String[] args) {
        //考试满分为100,输出“恭喜你满分”,90~100为A级;80~90为B级;80~70为C级;70~60为D级;0~59为不及格;
        Scanner s=new Scanner(System.in);
        System.out.println("请输入你的分数:");
        int score = s.nextInt();
        if(score ==100){
            System.out.println("恭喜你满分");
        } else if (score <100&&score >90) {
            System.out.println("A级");
        } else if (score <90&&score >80) {
            System.out.println("B级");
        } else if (score <80&&score >70) {
            System.out.println("C级");
        } else if (score <70&&score >60) {
            System.out.println("D级");
        } else if (score <60&&score >=0) {
            System.out.println("不及格");
        } else {
            System.out.println("你输入的不是本次的分数");
        }
        s.close();
    }
}





package control;

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade = 'f';
        switch (grade) {
            case 'A' -> System.out.println("优秀");
            case 'B' -> System.out.println("良好");
            case 'C' -> System.out.println("及格");
            case 'D' -> System.out.println("再接再厉");
            case 'E' -> System.out.println("挂科");
            default  -> System.out.println("无");
        }
    }
}

package control;

public class SwitchDemo02 {
    public static void main(String[] args){
        String name = "部落冲突";
        switch(name){
            case "部落冲突":
                System.out.println("部落冲突");
                break;
            case "超哥":
                    System.out.println("超哥");
                break;
            default:{
                System.out.println("弄啥嘞!");
            }
        }
    }
}

循环结构



package control;

public class WhileDome01 {
    public static void main (String[] args){
        //输出0~100;
        int i = 0;
        while(i<100){
            i++;
            System.out.println(i);
        }
    }
}

package control;

public class WhileDemo02 {
    public static void main(String[] args){
        //1+2+......+100
        int i = 0;
        int sum = 0;
        while(i<100){
            i++;
            sum=sum+i;
        }
        System.out.println("1+2+3+......+100="+sum);
    }

}

package control;

public class DoWhileDemo01 {
    public static void main(String[] args){
        int i = 0;
        int sum = 0;
        do{
            i++;
            sum=sum+i;
        }while(i<100);
        System.out.println("1+2+3+.......+100="+sum);
    }
}

package control;

public class DoWhileDemo02{
    public static void main(String[] argc){
        int i = 0;
        while(i<0){
            System.out.println(i);
        }
        System.out.println("--------------");
        do{
            System.out.println(i);
            i++;
        }while(i<0);

    }
}



package control;

public class ForDemo01 {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i+=2;
            System.out.println(i);
        }
        System.out.println("----------------");
        for (int i1 = 0; i1 < 100; i1++) {
            System.out.println(i1);
        }
    }
}

package control;

public class ForDemo02 {
    public static void main(String[] args) {
        //计算0~100奇数与偶数和分别是多少?
        int oddsum=0;
        int evensum=0;
        for (int i = 0; i <= 100; i++) {
            if(i%2!=0){
                oddsum+=i;
            }else{
                evensum+=i;
            }
        }
        System.out.println("0~100的奇数和为:"+oddsum);
        System.out.println("0~100的偶数和为:"+evensum);
    }
}

package control;

public class ForDemo03 {
    public static void main(String[] args) {
        for (int i = 0; i <= 1000; i++) {
            if(i%5==0){
                System.out.print(i+"\t");
            }
           if(i%15==0){
               System.out.println();
           }
        }
    }
}

package control;

public class ForDemo04 {
    public static void main(String[] args) {
        /*
         * 1*1=1	
         * 1*2=2	2*2=4	
         * 1*3=3	2*3=6	3*3=9	
         * 1*4=4	2*4=8	3*4=12	4*4=16	
         * 1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
         * 1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
         * 1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
         * 1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
         * 1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	
         */
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <=j; i++) {
                System.out.print(i + "*" + j + "=" + (i * j) + "\t");
            }
            System.out.println();
        }
    }
}



package control;

public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6};
        for (int i : numbers) {
            System.out.println(i );
        }
        System.out.println("==================");

        for (int i1 = 0; i1 < 6; i1++) {
            System.out.println(numbers[i1]);
        }
    }
}

break&continue




package control;

public class BreakDemo01 {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
            if(i==35){
                break;
            }
        }
        System.out.println("888");
    }
}


package control;

public class ContinueDemo01 {
    public static void main(String[] args) {
        int i = 0;
        while(i<100){
            i++;
            if(i%5==0){
                System.out.println();
                continue;
            }
            System.out.print(i+"\t ");

        }
        System.out.println("999");
    }
}

练习


package struct.Test;

public class TestDemo01 {
    public static void main(String[] args) {
        //打印三角形
        for (int i = 1; i <= 10; i++) {
            for (int j = 10; j >=i; j--) {
                System.out.print(" ");
            }
            for(int j =2;j<=i;j++){
                System.out.print("*");
            }
            for(int j =2;j<i;j++){
                System.out.print("*");
            }
            System.out.println();

        }
    }
}

标签:Java,String,int,基础,System,语法,println,public,out
From: https://www.cnblogs.com/1802ming/p/17280456.html

相关文章

  • java——spring boot集成kafka——spring boot集成kafka
    引入依赖:  <dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId></dependency>          编写配置文件:    erver:port:8080spring:kafka:bootstrap-se......
  • Java IO面试题
    JavaIO概览JavaIO流的40多个类都是从如下4个抽象类基类中派生出来的。InputStream/Reader:所有的输入流的基类,前者是字节输入流,后者是字符输入流。OutputStream/Writer:所有输出流的基类,前者是字节输出流,后者是字符输出流。InputStream字节输入流InputStream用于......
  • Java:分数运算(类与对象)
    题目内容:设计一个表示分数的类Fraction。这个类用两个int类型的变量分别表示分子和分母。这个类的构造函数是:Fraction(inta,intb)    构造一个a/b的分数。这个类要提供以下的功能:doubletoDouble();    将分数转换为doubleFractionplus(Fractionr);    将自己的分......
  • 光纤光缆的基础知识
      光 纤  光纤,完整名称叫做光导纤维,英文名是OPTICFIBER。它是一种由玻璃或塑料制成的纤维,可作为光传导工具。光纤的主要用途,是通信。目前通信用的光纤,基本上是石英系光纤,其主要成分是高纯度石英玻璃,即二氧化硅(SiO2)。光纤通信系统,就是利用光纤来传......
  • java面向对象编程-对象的创建分析
    对象的创建分析类与对象的关系类是一种抽象的数据类型,他是对某一类事物整体描述/定义,但是并不能代表某一个具体的事物动物,植物,手机,电脑Person类,Pet类等,这些类都是用来描述/定义某一类具体的事物应该具备的特点和行为对象是抽象概念的具体事例张三就是人的一个具......
  • jQuery基本语法
             ......
  • JavaIO流:主要知识点
    JavaIO流:主要知识点File类介绍:java.io.File类:文件和文件目录路径的抽象表示形式,与平台无关。File能新建、删除、重命名文件和目录,但File不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。想要在Java程序中表示一个真实存在的文件或目录,那么必......
  • 物联网技术基础及应用绪论
    物联网技术基础及应用绪论  任课教师:北京化工大学毕超课程链接:https://www.bilibili.com/video/BV1ha411g7H2/?spm_id_from=333.999.0.0&vd_source=e66dd25b0246f28e772d75f11c80f03c课程版本:2022版涉及语言:Android、Html/CSS/JavaScript、PHP、C涉及工具:MySQL(Linux)、N......
  • 【THM】Windows Fundamentals 2(Windows基础知识2)-学习
    本文相关的TryHackMe实验房间链接:https://tryhackme.com/room/windowsfundamentals2x0x本文介绍:本文所涉及的内容是Windows基础模块的第2部分,了解有关系统配置、UAC设置、资源监控、Windows注册表等更多信息。简介在WindowsFundamentals1中,我们已经介绍了Windows的桌面......
  • java面向对象编程
    面向对象编程java的核心思想就是OOP面向过程&面向对象面向过程:步骤清晰简单,第一步做什么,第二步做什么···适合处理较为简单的问题面向对象:物理类聚,分类的思维模式,思考问题首先解决问题需要哪些分类,然后对这些分类进行单独思考,最后对某个分类下的细节进行面向过......