Niuke Brushing Notes-Getting Started Training for Programming Beginners (Introduction)

BC24-Total score and average score calculation

import java.util.Scanner;
import java.text.DecimalFormat;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        float score1 = in.nextFloat();
        float score2 = in.nextFloat();
        float score3 = in.nextFloat();
        float sum = score1+score2+score3;
        float average = sum/3;
        System.out.println(new DecimalFormat("0.00").format(sum)+" "+new DecimalFormat("0.00").format(average));
    }
}
  1. Read keyboard input integer floating point numbers with nextInt, nextFloat...

  2. Use DecimalFormat to take a few digits after the decimal point: new DecimalFormat("0.##").format(...)

    方法2:String.format("%.2f",floatnumber);System.out.printf("%.2f",floatnumber);

BC42-Perfect Results

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner (System.in);
        int score;
        while(in.hasNext()){
            score = in.nextInt();
            if(score>=90||score<=100) System.out.println("Perfect");
        }
    }
}
  1. Loop judgment: loop in.hasNext() when there is input

  2. And || or &&

  3. print displays its parameters in the command window and positions the output cursor after the last character displayed.

    println displays its parameters in the command window and adds a newline character at the end to position the output cursor at the beginning of the next line.

    printf is a formatted output form.

BC46-Determine whether it is a vowel or a consonant

import java.util.Scanner;
public class Main{
    
    
    public static void main(String[] args){
    
    
        char letter;
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
    
    
            letter = in.next().charAt(0);
            if(letter=='A'||letter=='a'||letter=='E'||letter=='e'||letter=='I'||letter=='i'||letter=='O'||letter=='o'||letter=='U'||letter=='u')
                System.out.println("Vowel");
            else System.out.println("Consonant");
        }
    }
}
  1. Java reads a single character method: in.next().charAt(0) //Get the first character of the read string
  2. Single quotation mark for single character comparison value ''

But it feels a bit troublesome, please refer to other people's code.

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        String target = "AEIOUaeiou";
        while((str = br.readLine()) != null) {
            char ch = str.charAt(0);
            if (target.indexOf(ch) != -1) {
                System.out.println("Vowel");
            } else {
                System.out.println("Consonant");
            }
        }
        br.close();
    }
}
  1. The indexOf method returns an integer value indicating the starting position of the substring within the String object. If the substring is not found, -1 is returned.
  2. The definition of System.in is "static InputStream in;". Obviously, there are many limitations to using System.in directly, so we can wrap it and use InputStreamReader to convert Stream to Reader, so that it can be passed in units of characters. Then use BufferedReader to wrap it, so that you can use the readLine() method to read a whole line and save it in String. When reading user input directly from the standard input stream System.in, System.in reads a character every time the user inputs a character. In order to read the user's input one line at a time, a BufferedReader is used to buffer the characters input by the user. The readLine() method will pass in the entire line of the string again when the user's newline character is read. System.in is a bit stream. In order to convert to a character stream, you can use InputStreamReader to convert characters to it, and then use BufferedReader to add buffering functions to it. For example: BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

BC49-Judging the relationship between two numbers

import java.io.*;
public class Main {
    
    
    public static void main(String[] args) throws IOException {
    
    
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        String str;
        int t;
        while((str=br.readLine())!=null){
    
    
            t = Integer.parseInt(str);
            if(t>0) System.out.println("1");
            else if(t==0) System.out.println("0.5");
            else System.out.println("0");
        }
    }
}

Integer.parseInt(String) is to convert String character data into Integer integer data.

Integer.parseInt(String) will throw an exception when encountering some characters that cannot be converted to integers.

BC72-average height

import java.util.Scanner;
import java.text.DecimalFormat;
public class Main{
    
    
    public static void main(String[] args){
    
    
        float h[] = new float[5];
        float sum = 0;
        Scanner in = new Scanner(System.in);
        for(int i=0;i<5;i++){
    
    
            h[i]=in.nextFloat();
            sum = sum + h[i];
        }
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println(df.format(sum/5));
    }
}

There are two syntaxes for defining arrays in Java:
type arrayName[];
type[] arrayName;
type is any data type in Java, including basic types and composite types, arrayName is an array name, which must be a legal identifier, [ ] Indicates that the variable is an array type variable.

Usually, you can allocate space while defining, the syntax is: type arrayName[] = new type[arraySize];
For example: int demoArray[] = new int[3];

BC86-parity statistics

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        if(N%2==1)  System.out.println((N+1)/2+" "+(N-1)/2);
        else System.out.println(N/2+" "+N/2);
    }
}

Remainder (modulo) operation:%

Guess you like

Origin blog.csdn.net/qq_41536271/article/details/112056673