Java练习 SDUT-1133_模拟计算器

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

Time Limit: 1000 ms Memory Limit: 8192 KiB

Problem Description

简单计算器模拟:输入两个整数和一个运算符,输出运算结果。

Input

第一行输入两个整数,用空格分开;
第二行输入一个运算符(+、-、*、/)。
所有运算均为整数运算,保证除数不包含0。

Output

输出对两个数运算后的结果。

Sample Input

30 50
*

Sample Output

1500

一个简单的模拟题,不过注意Java的严谨性,所以方法一定要有返回值。

import java.util.*;

public class Main{
	public static void main(String[] args) {
		int a,b;
		char c;
		node t;
		Scanner cin = new Scanner(System.in);
		a = cin.nextInt();
		b = cin.nextInt();
		c = cin.next().charAt(0);
		t = new node(a,b,c);
		System.out.println(t.f());
		cin.close();
	}
}

class node
{
	int a,b;
	char c;
	node (int x,int y,char z)
	{
		a = x;
		b = y;
		c = z;
	}
	int f()
	{
		if(c=='+')
			return a + b;
		else if(c=='-')
			return a - b;
		else if(c=='*')
			return a * b;
		else if(c=='/')
			return a / b;
		return 0;
	}
}
原文地址:https://www.cnblogs.com/luoxiaoyi/p/9758591.html