牛客编程初学者人门训练第一部分

1.实践出真知

题目描述:

public class Main{
	
	public static void main(String[] args) {
		System.out.print("Practice makes perfect!");
	}
}

2.我是大V
在这里插入图片描述
方法一:直接用输出语句暴力输出,但是要注意空格数,避免出错。

public class Main {
    public static void main(String[] args){
        System.out.print("v   v"+'\n'+" v v"+'\n'+"  v");
    }
}

方法二:通过for循环整体输出v,难点在于找准if(j == i || j == 2*n-2-i)来决定输出的是v还是空格。

public class Main {
	public static void main(String[] args) {
		int n = 3;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < 2*n-1; j++){
				if(j == i || j == 2*n-2-i){
					System.out.print("v");
				}else{
					System.out.print(" ");
				}
			}
			System.out.print('\n');
		}
	}
}

方法三:先通过for循环输出前两行的v,在输出最后一行的v。

public class Main {
	public static void main(String[] args) {
		int a,b=1,c;
	    for(a=0;a<2;a++){
	       if(b<=a){
	    	   System.out.print(" ");
	       }
	       System.out.print("v");
	       for(c=3-2*a;c>0;c--){
	    	   System.out.print(" ");
	        }
	        System.out.print("v\n");
	    }
	    System.out.print("  v  ");
	}
}

3.有容乃大
在这里插入图片描述

public class Main{
    public static void main(String[] args){
        System.out.println("The size of short is "+Short.BYTES+" bytes.");
        System.out.println("The size of int is "+Integer.BYTES+" bytes.");
        System.out.println("The size of long is "+Long.BYTES+" bytes.");
        System.out.println("The size of long long is "+Long.BYTES+" bytes.");
    }
}

4.小飞机
在这里插入图片描述
方法一:利用switch语句将图案分层处理,结合for语句和if语句判断空格和星号的位置。

public class Main {

	public static void main(String[] args) {
		for(int i = 1; i <= 6; i++) {
			switch(i) {
			case 1:
			case 2:
				for(int j = 1; j <= 12; j++) {
					if(j == 6 || j == 7) {
						System.out.print("*");
					}else {
						System.out.print(" ");
					}
				}
				System.out.print('\n');
				break;
			case 3:
			case 4:
				System.out.print("************");
				System.out.print('\n');
				break;
			case 5:
			case 6:
				for(int j = 1; j <= 12; j++) {
					if(j == 5 || j == 8) {
						System.out.print("*");
					}else {
						System.out.print(" ");
					}
				}
				System.out.print('\n');
				break;
			}
		}
	}
}

方法二:暴力求解,直接用输出语句输出小飞机图案。

public class Main {

	public static void main(String[] args) {
		System.out.println("     **     ");
		System.out.println("     **     ");
		System.out.println("************");
		System.out.println("************");
		System.out.println("    *  *    ");
		System.out.println("    *  *    ");
	}
}

5.缩短二进制
在这里插入图片描述

public class Main {

	public static void main(String[] args) {
		System.out.printf("0%o 0X%x",1234,1234);
	}
}

6.十六进制转十进制
在这里插入图片描述

public class Main {

	public static void main(String[] args) {
		System.out.printf("%15d",0XABCDEF);
	}
}

7.printf的返回值
在这里插入图片描述

public class Main {

	public static void main(String[] args) {
		String a="Hello world!";
		System.out.println(a);
		System.out.println(a.length());
	}
}

8.计算表达式的值
在这里插入图片描述

public class Main {

	public static void main(String[] args) {
		int b, a = 40, c = 212;
		b = (22-8)*a-10+c/2;
		System.out.print(b);
	}
}

9.学生基本信息输入输出
在这里插入图片描述

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		//使用;或者,分割元素
		String[] split = scan.nextLine().split("[;,]");
		//Integer.parseInt()将数据类型转换为Integer整型数组
		int ID = Integer.parseInt(split[0]); 
		Double Chinese = Double.parseDouble(split[1]);
		Double English = Double.parseDouble(split[2]);
		Double Mathe = Double.parseDouble(split[3]);
		System.out.printf("The each subject score of  No. %s is %.2f, %.2f, %.2f.", split[0], Chinese, English, Mathe);
	}
}

10.字符金字塔
在这里插入图片描述

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String ch = scan.nextLine();
		for(int j = 1; j <= 5; j++) { //将金字塔分层处理
			for(int i = 1; i <= 5-j; i++) { //输出空格部分
				System.out.print(" ");
			}
			for(int k = 1; k <= j; k++) { //输出字符以及字符间的空格
				System.out.printf(ch);
				System.out.print(" ");
			}
			System.out.print('\n'); //实现换行
		}
	}
}

11.ASCII码
在这里插入图片描述

public class Main {
	
	public static void main(String[] args) {
		int[] array = {73, 32, 99, 97, 110, 32, 100, 111, 32, 105,116, 33};
		for(int i = 0; i < array.length; i++){
			System.out.print((char)array[i]);
		}
	}
}

12.时间转换
在这里插入图片描述

import java.util.Scanner;
public class Jichu {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int secends = scan.nextInt();
		int min, hour;
		hour = secends/3600;
		min = (secends/60)%60;
		secends = secends%60;
		System.out.print(hour + " " + min + " " + secends);
	}
}

13.计算体重指数题目描述:计算BMI指数(身体质量指数)。BIM指数的计算公式为:体重公斤数除以身高米数平方得出的数字。

import java.util.Scanner;
public class Main{
    
    public static void main(String[] args){
        Scanner br = new Scanner(System.in);
        String[] str = br.nextLine().split(" ");
        double weight = Double.parseDouble(str[0]);
        double height = Double.parseDouble(str[1])/100;
        double bmi = 0;
        bmi = weight / (height * height);
        System.out.printf("%.2f",bmi);
    }
}

14.计算三角形的周长和面积题目描述:根据三角形给出的3条边a, b, c(0<a, b, c<100000),计算三角形的周长和面积。

import java.util.Scanner;
public class Main {
	/*知道三角形三边长度求面积,要用到海伦公式
	 * p = (a + b + c)/2
	 * S=sqrt(p*(p-a)*(p-b)*(p-c)
	 * */
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
        String[] str = scan.nextLine().split(" ");
		double a = Double.parseDouble(str[0]);
		double b = Double.parseDouble(str[1]);
		double c = Double.parseDouble(str[2]);
		double per, p, area;
		per = a + b + c;
		p = (a + b + c)/2;
		area = Math.sqrt(p*(p-a)*(p-b)*(p-c));
		System.out.println("circumference=" + String.format("%.2f", per) + " area=" + String.format("%.2f", area));
	}
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值