Java阶段学习笔记 一 (java基础练习)

1、控制台输入法判断奇偶数

public class Odd_Even_Check {
	public static void main(String[] args) {
		int num;
		Scanner in = new Scanner(System.in);
		for(int i = 0; i < 3; i++)
		{
			System.out.println("Enter a number : ");
			num = in.nextInt();
			if(num % 2 == 0)
			{
				System.out.println("The input number is Even ");
			}
			else
			{
				System.out.println("The input number is Odd ");
			}
		}
	}
}

2、输入任意三个整数判断最大数

public class Max3 {

	public static void main(String[] args)
	{
			Scanner in = new Scanner(System.in);
			System.out.println("Enter three numbers( space to identify) : ");
			int a, b, c;
			a = in.nextInt();
			b = in.nextInt();
			c = in.nextInt();
			int temp = 0;
			if(a > b)
			{
				temp = a;
			}
			else
			{
				temp = b;
			}
			if(temp < c)
			{
				temp = c;
			}
			System.out.println("The maxtum value is " + temp);
	}
}

3、整数除法及模除:求整数各位数字之和

public class Bit_Sum {
	public static void main(String[] args)
	{
		int temp, bit = 0;
		System.out.println("Enter an integer : ");
		Scanner in = new Scanner(System.in);
		temp = in.nextInt();
		bit = temp % 10;
		while(temp / 10 != 0)
		{  
			temp = temp / 10;
			bit += temp % 10;
		}
		System.out.println("The bit sum is : " + bit );
	}
}

4、素数判别

public class Sushu {
	public static void main(String[] args) {
		int temp, comp;
		System.out.println("Enter an integer number : ");
		Scanner in = new Scanner(System.in);
		temp = in.nextInt();
		comp = (int)temp / 2;
		int i;
		for(i = 2; i <= comp; i++)
		{
			if(temp % i == 0)
			{
				System.out.println(temp + " is not a SuShu ");
				break;
			}
		}
		if(i > comp)
		{
			System.out.println(temp + " is a SuShu . ");
		}
		
	}
}

5、创建Student类(name和分数成员)与Score分数类(课程名和分数成绩),编写测试程序。

//Score class
public class Score
{
	private String course;
	private int grade;
	
	public void setCourse(String cs, int g)
	{
		this.course = cs;
		this.grade = g;
	}
	
	public int getData()
	{
		return this.grade;
	}
	
	public String getCourse()
	{
		return this.course;
	}
}

//Student class
public class Student
{
	String name;
	Score score = new Score();
	
	public Student(String nm, String cs, int g)
	{
		this.name = nm;
		score.setCourse(cs, g);
	}
	
	public void getResult()
	{
		System.out.println("Student : " + name + "\t\tcourse : " + score.getCourse() + "    \tscore : " + score.getData());
	}
}

//Test 
public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s[] = new Student[3];
		s[0] = new Student("mike", "Math", 98);
		s[1] = new Student("Jermi", "Chinese", 94);
		s[2] = new Student("mili", "English", 97);
		for(int i = 0; i < 3; i++)
		{
			s[i].getResult();
		}
	}

practice makes perfects!

猜你喜欢

转载自blog.csdn.net/qq_37172182/article/details/86522345