牛客刷题笔记-编程初学者入门训练(入门篇)

BC24-总成绩和平均分计算

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. 读取键盘输入整数浮点数用nextInt、nextFloat…

  2. 取小数点后几位用DecimalFormat:new DecimalFormat(“0.##”).format(…)

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

BC42-完美成绩

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. 循环判断:有输入时循环in.hasNext()

  2. 与|| 或&&

  3. print将它的参数显示在命令窗口,并将输出光标定位在所显示的最后一个字符之后。

    println 将它的参数显示在命令窗口,并在结尾加上换行符,将输出光标定位在下一行的开始。

    printf是格式化输出的形式。

BC46-判断是元音还是辅音

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读入单个字符方法:in.next().charAt(0) //获取读入字符串的首字符
  2. 单个字符比较值用单引号’ ’

但是感觉有点麻烦,参考一下别人的代码。

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. indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。
  2. System.in的定义为 “static InputStream in;”。很显然直接使用System.in的话有很多局限性,因此我们对其进行包装,可以收先用InputStreamReader将Stream转化为Reader,这样可以以字符为单位进行传递。再使用BufferedReader将其进行包装,这样可以使用readLine()方法来读取一整行并保存在String中。从标准输入流System.in中直接读取使用者输入时,使用者每输入一个字符,System.in就读取一个字符。为了能一次读取一行使用者的输入,使用了BufferedReader来对使用者输入的字符进行缓冲。readLine()方法会在读取到使用者的换行字符时,再一次将整行字符串传入。System.in是一个位流,为了转换为字符流,可使用InputStreamReader为其进行字符转换,然后再使用BufferedReader为其增加缓冲功能。例如:BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

BC49-判断两个数的大小关系

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)就是将String字符类型数据转换为Integer整型数据。

Integer.parseInt(String)遇到一些不能被转换为整型的字符时,会抛出异常。

BC72-平均身高

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));
    }
}

Java 中定义数组的语法有两种:
type arrayName[];
type[] arrayName;
type 为Java中的任意数据类型,包括基本类型和组合类型,arrayName为数组名,必须是一个合法的标识符,[ ] 指明该变量是一个数组类型变量。

通常,你可以在定义的同时分配空间,语法为:type arrayName[] = new type[arraySize];
例如:int demoArray[] = new int[3];

BC86-奇偶统计

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);
    }
}

取余(取模)运算:%

猜你喜欢

转载自blog.csdn.net/qq_41536271/article/details/112056673
今日推荐