In the time of my roommate's two songs, I actually completely understood the input and output sentences

foreword

       When writing Java topics recently, I often encounter problems such as inputting data from the keyboard, multiple sets of loop input, etc.;

       As it happens, the input and output statements in Java are slightly different from those in the C language;

       Although, the corresponding data may not be directly entered in the console in the future (a giant said that way is too low);

       However, at this stage , we still need to know this knowledge;

       So, I have summarized some relevant knowledge points. I hope you can take the time for two songs, quietly take a few glances, and give a like and follow by the way;

       The following text begins...


1. From output to console

       As the name implies, input data to the console through println, print, and printf operations.

       Among them, the usage of printf in Java is the same as that in C language, and has the same format (but except for some specific needs, it is generally not printed in this way, because: trouble);

       The usage of println is much simpler than that of printf. You can directly input whatever you want to input, and use println to automatically bring a newline function;

       The usage of print is the same as that of println, except that there is no newline function.

       It's useless to say more, you can understand more thoroughly with the code example directly:

public class TestDemo {
    public static void main(String[] args) {
        System.out.println("我知道你很帅");
        //println 输出以后自带换行

        System.out.print("我知道你很帅");
        //print 输出以后没有带换行

        System.out.printf("%d\n",90);
        //类似于C语言的输入
        
    }
}

 


2. Input from the keyboard

       Usually when writing practice questions (OJ questions), you will often encounter multiple sets of input, output examples, etc., so it is the type of input from the keyboard.

2.1 Use Scanner to read strings, integers, floating-point numbers

       First of all, in Java, if you want to use a scanf() statement similar to C language for input, you need to use Scanner to read;

       At the same time, you must import an until package  import java.util.Scanner; (similar to #include<stdio.h> in C language)

Directly on the code example:

       In fact, the usage of reading strings, integers, and floating-point numbers is basically similar:

import java.util.Scanner;

public class TestDemo_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name =scanner.next();
        System.out.println("姓名:"+name);
    }
}

       It is indeed possible to see this situation, but it should be noted that  the next() in  String name =scanner.next(); above the code has such a feature: the reading ends when a space is encountered .

       If you don't believe me, try typing something with spaces:

However, some people just want to say, I'm going to print content with spaces?

——Then we can replace next() with nextLine(). The function of nextLine() is to read one line. If the input is only one line, it can be read no matter what it is :

       Next up are integers and floats:

import java.util.Scanner;

public class TestDemo_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入你的姓名:");
        String name =scanner.nextLine();
        System.out.println("姓名:"+name);

        System.out.println("请输入你的年龄:");
        int age = scanner.nextInt();
        System.out.println("年龄:"+age);

        System.out.println("请输入你的身高:");
        float high = scanner.nextFloat();
        System.out.println("身高:"+high);
    }
}

       Of course, the same is true for double, etc., and the types also need to be in one-to-one correspondence;

       Knowing the input and output of strings, integers, and floating-point numbers can solve most problems;

       If you want to know more methods, you can go to the help manual:

2.2 Multiple groups of inputs

       Smart friends will find that the above input statement can only occur once, and after the input is completed, the program will be exited directly;

       So, now to introduce the method of multiple sets of input: 

       In fact, it is very simple: like the C language, you need to operate in while:

Scanner 变量1 = new Scanner(System.in);

while(变量1.hasNext()){

  类型 变量2 = hasnextXXX;
  ......

}

//就是那种 XXX和变量2之前的类型要相匹配 的那种意思

The following example can help you better understand the above content:

 


end

       This is a blog that shares the input and output statements that are needed when writing questions;

       If it is helpful to you, can you send a free like or follow~

Guess you like

Origin blog.csdn.net/qq_53362595/article/details/123910650