Java面向对象1

版权声明:本文为原创文章QWQ,如果有错误欢迎指正,转载请注明作者且附带网址 https://blog.csdn.net/Mercury_Lc/article/details/82998574

QWQ请假一节课,错过一章内容,只能求助qsh了。

C/C++训练1---最大公约数与最小公倍数(SDUT 1131)

import java.util.*;

class Number {
	int a, b;

	Number(int n, int m) {
		a = n;
		b = m;
	}

	int getGcd() {
		int n = a, m = b;
		while (m > 0) {
			int x = n;
			n = m;
			m = x % m;
		}
		return n;
	}

	int getLcm() {
		int x = getGcd();
		return a * b / x;
	}

	void Print() {
		System.out.println(getGcd() + "\n" + getLcm());
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Number num = new Number(sc.nextInt(), sc.nextInt());
		num.Print();
		sc.close();
	}
}

C/C++经典程序训练3---模拟计算器

import java.util.Scanner;
class Number
{
	int a, b;
	String c;
	Number(int n, int m, String k)
	{
		a = n;
		b = m;
		c = k;
	}
	int getAns()
	{
		int ans = 0;
		if(c.equals("+"))ans = a + b;
		else if(c.equals("-"))ans = a - b;
		else if(c.equals("*"))ans = a * b;
		else ans = a / b;
		return ans;
	}
	void Print()
	{
		System.out.println(getAns());
	}
}
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a, b;
		String c;
		a = sc.nextInt();
		b = sc.nextInt();
		sc.nextLine();
		c = sc.nextLine();
		Number p = new Number(a, b, c);
		p.Print();
		sc.close();
	}
}

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/82998574