一.
import java.util.Scanner;
public class Person {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please input your name here:");
String name = sc.next();
System.out.println("Please input your age here:");
int age = sc.nextInt();
System.out.println("Please input your gender here:");
String gender = sc.next();
System.out.println("The computer recorded that:");
System.out.println("Your name is:" + name);
System.out.println("Your name is:" + age);
System.out.println("Your name is:" + gender);
}
}
二.
import java.util.Scanner;
public class Person {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一位整数:");
int num = sc.nextInt();
if(num%2==0){
System.out.println("是偶数");
}else{
System.out.println("是奇数");
}
}
}
三.
public class Person {
public static void main(String[] args) {
int[] array={10,30,50,70,20,80,60,90,40};
for (int i = 0; i < array.length; i++) {
for(int j = 0;j < array.length-1-i;j++){
if (array[j]>array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
四.
方法: 语法: 修饰符 方法的返回类型 方法名(参数列表){ 方法体 } 作用:就是我们常说的行为,再开发过程中一个的业务逻辑代码都要定义方法
构造方法(构造器): 语法: 修饰符 类名(参数列表){ 初始化成员变量 } 作用:用来进行初始化成员变量(也就是赋值)
package netclass;
public class Person {
String name;
int age;
public Person(String _name,int _age){
name = _name;
age = _age;
}
public Person(){
}
public void display(){
System.out.println("姓名" + name);
System.out.println("年龄" + age);
}
public static void main(String[] args) {
Person p = new Person();
p.name = "鱿鱼";
p.age = 780;
p.display();
}
}