Java realizes the input of student number, name, age and outputs it

Table of contents

I. Introduction

Two, the code part

3. Program running results (the panel pops up) 

4. Codes of knowledge points involved


I. Introduction

1. This code was written by me when I was in school. There are some places that have not been perfectly implemented. Please forgive me and give me more advice!

2. This pop-up window interface can be input according to simple requirements, and display whether it is correct. The code setting of this article is to realize the input of student number, name, and age in the code, and output it. At the same time, custom settings can be realized;

3. Realize the input of student number, name, and age, and output them. The code uses a custom exception class to judge. Before the judgment, the age limit is adopted. If it is not needed, you can choose to delete it. It is necessary to enter the student number, name, and age at the beginning of the judgment, and then output according to the input information;

4. The system can only run on the console (eclipse and other versions), and needs to be matched with the jdk environment;

5. Here is a special note, if the complete code package name to be pasted is inconsistent with mine, it is specified inconsistently, please change it manually;

Two, the code part

 1. Realize the code for inputting student number, name, age and outputting it

package com.edu.p1;

import java.util.Scanner;

class TestException extends Exception{//自定义的异常类
	public TestException(String msg){
		super(msg);
	}
	
}
class Age{
	public void TestAgen(int age) throws TestException{
		if(age<10|age>30)
			throw new TestException("年龄不在10~30岁之间");
	}
}
public class Test403 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		System.out.println("请输入学生的学号、姓名、年龄:");
		Scanner in=new Scanner(System.in);
		String num=in.next();
		String name=in.next();
		int age=in.nextInt();
		try{
			Age a=new Age();
			a.TestAgen(age);
			System.out.println("学号:"+num+",姓名:"+name+",年龄:"+age);
		}catch(TestException e){
			e.printStackTrace();
		}

	}

}

3. Program running results (the panel pops up) 

1. Display the result 

4. Codes of knowledge points involved

1. Custom exception class

class TestException extends Exception{//custom exception class
    public TestException(String msg){         super(msg);     }     }


2. Instantiate the object

Scanner in=new Scanner(System.in); 

Guess you like

Origin blog.csdn.net/weixin_59042297/article/details/129783695