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.计算体重指数
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.计算三角形的周长和面积
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));
}
}