求Java大佬帮忙
2020-01-09
public class Test{
public static void main(String[] args) {
Student stu=new Student("张三",22);
stu.show();
stu.setInfo("李四", 19);
stu.show();
//乘法表
for(int i=1;i<10;i++) {
for(int j=1;j<=i;j++) {
System.out.print(j+"x"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
}
class Student {
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
public void setInfo(String name,int age) {
this.name=name;
this.age=age;
}
public void show() {
System.out.println("Name:"+name+";Age:"+age);
}
}
public class Student{
private String name;
private int age;
public void setInfo(String name,int age){
this.name = name;
this.age=age;
}
public void show(){
System.out.println("我叫"+this.name+",我今年"+this.age+"岁!");
}
}
Test类:
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
try { //输入异常处理
Student s = new Student();
System.out.print("请输入你的姓名:");
String str = new Scanner(System.in).next();
System.out.print("请输入你的年龄:");
int num = new Scanner(System.in).nextInt();
while (num<=0) {
System.out.println("年龄必须大于0!\n");
System.out.print("请输入你的年龄:");
num = new Scanner(System.in).nextInt();
}
s.setInfo(str, num);
s.show();
} catch (Exception e) {
System.out.println("年龄输入有误,程序结束!");
}
}
}