如何在print里面多重展示想表现的内容等
1、hello java
public class Demo01 {
public static void main(String[] args) {
helloFun();
}
public static void helloFun() {
System.out.println("hello java!!!");
}
}
2、从键盘输入,以及如何在System.out.println中使用+号
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("�������һ��ֵ��");
int a = sc.nextInt();
System.out.print("�������һ��ֵ��");
int b = sc.nextInt();
int s = sum(a, b);
System.out.println(a +" + "+ b +" = "+ s);
}
public static void showInfo(String name, int age, String address) {
System.out.println("�ҵ����ֽ�����"+ name) ;
System.out.println("�ҽ��꣺"+ age +" ��") ;
System.out.println("�Ҽ�ס��"+ address) ;
}
public static int sum(int a, int b) {
int result = a + b;
return result;
}}
3、布尔值
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("������һ���������жϸ����Ƿ���������");
int a = sc.nextInt();
boolean prime = isPrime(a);
if (prime) {
System.out.println("����");
} else {
System.out.println("����");
}
}
public static boolean isPrime(int num) {
for (int i = 2; i < (num / 2 + 1); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
4、构造函数
import java.util.Scanner;
public class Demo04 {
public static void test() {
int a = 10;
System.out.println(a);
System.out.println(a + 100);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("������һ���������жϸ����Ƿ���������");
int layer = sc.nextInt();
printTriangle(layer);
// test();
//System.out.println(a);
}
public static void printTriangle(int layer) {
for (int i = 1; i <= layer; i++) {
// ����ո�����
int spaceNum = layer - i;
for (int j = 1; j <= spaceNum; j++) {
System.out.print(" ");
}
int starNum = 2 * i - 1;
for (int j = 1; j <= starNum; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
5、多态
class Demo05 {
public static void main(String[] args) {
add(10, 20);
byte a = 20;
add(a, 30);
add(100);
}
public static int add(int x, int y) {
return x + y;
}
public static int add(byte a, int b) {
return a + b;
}
public static int add(int a) {
return a + 10;
}
public static double add(double a, int b) {
return a + b;
}
}