英文:
Creating a calculator that calls the methods from a separate class using scanner
问题
我正在创建一个计算器,其中每个计算器功能必须在单独的类中,并且使用扫描器输入从主方法中调用。
- 添加
- 减法
- 乘法
- 除法
- 平方
- 幂
- 取模操作
- 阶乘
- 退出
我必须创建每个方法由一个对象调用。计算器需要一个主类(具有main()方法)和一个用户定义的类,其中包含上述的计算方法。程序必须允许用户选择一个操作(上述计算之一)和操作数(数字)。某些计算需要两个操作数(例如,A + B,AB),而某些计算只需要一个操作数(例如,A^2,N!)。
额外的功能:
- 让计算函数继续,直到用户想退出此程序。
- 当完成一个计算时,让用户选择另一个操作。
- 当用户选择结束选项时,程序终止。
- 实现所有操作。不要使用Java库的数学方法。
我有一个使用switch语句和case的程序代码,但是我们班还没有学过这些。我已经在网上搜索了两天,但仍然无法弄清楚,所以如果解决方案很简单,我深感抱歉,先感谢所有的帮助。以下是我目前的程序代码:
// 这是你的Java程序代码,它实现了你所描述的计算器功能。
// 如果你需要任何翻译或解释,请告诉我。
请让我知道如果你需要关于这个程序的任何帮助或解释。
英文:
I'm creating a calculator where each of the calculator's functions have to be in a separate class and called from the main method using scanner input.
1.Add
2.Subtract
3.Multiplication
4.Division
5.Square
6.Power
7.Mod operation
8.Factorial
0.Quit
I must create each method called by an object. The calculator requires one main class (has main () method) and one user defined class which has above calculating methods. The program must let the user choose an operation (one of above calculations) and operand(s) (numbers). Some calculations require two operands. (e.g. A + B, AB) Some calculations require one operand. (e.g. A2, N!) Additional functions:
•Let the calculation function continues until user wants to exit this program.
•When one calculation is done, let user choose another operation.
•This program terminates when user selects END option.
•Implement all operations. Do not use Java library math methods.
I have the code for a program that is using switch statement and case, but our class hasn't even learned any of that. I have looked everywhere online for the past two days and just can't figure it out, so apologies if the solution is simple, and thank you to all help in advance. Here is the code to my program so far.
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int choice;
do
{
System.out.println("[1] Add ");
System.out.println("[2] Subtract ");
System.out.println("[3] Multiply ");
System.out.println("[4] Division ");
System.out.println("[5] Square ");
System.out.println("[6] Power ");
System.out.println("[7] Mod Operation ");
System.out.println("[8] Factorial ");
System.out.println("[0] Quit ");
System.out.println("Please enter your choice: ");
choice = s.nextInt();
int num1, num2;
switch(choice)
{
case 1 : System.out.println("Enter two numbers to add: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println("The sum of " + num1 + " and " + num2 +
" is: " + add(num1, num2));
break;
case 2 : System.out.println("Enter two numbers to subtract: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println("The difference of " + num1 + " and " + num2 +
" is: " + diff(num1, num2));
break;
case 3 : System.out.println("Enter two numbers to multiply: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println("The product of " + num1 + " and " + num2 +
" is: " + prod(num1, num2));
break;
case 4 : System.out.println("Enter two numbers to divide: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println("The quotient of " + num1 + " and " + num2 +
" is: " + quo(num1, num2));
break;
case 5 : System.out.println("A number to square: ");
num1 = s.nextInt();
System.out.println("The square of " + num1 + " is: " + square(num1));
break;
case 6 : System.out.println("Enter the base and the exponent: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println("The power of " + num1 + " to the " + num2 +
"th power is: " + power(num1, num2));
break;
case 7 : System.out.println("Enter two numbers to get the interger remainder of (modulo): ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println("The modulo of " + num1 + " and " + num2 +
" is: " + mod(num1, num2));
break;
case 8 : System.out.println("Enter a number to get the factorial of: ");
num1 = s.nextInt();
System.out.println("The factorial of " + num1 + " is: " + factorial(num1));
break;
case 0: System.out.println("Thank you for using my program...good bye!");
System.exit(0);
}
}
while(choice != 0);
s.close();
}
public static int add(int num1, int num2)
{
return num1 + num2;
}
public static int diff(int num1, int num2)
{
return num1 - num2;
}
public static int prod(int num1, int num2)
{
return num1 * num2;
}
public static double quo(int num1, int num2)
{
return (double)num1 / num2;
}
public static int mod(int num1, int num2)
{
return num1 % num2;
}
public static long power(int base, int exp)
{
long result = 1;
while (exp != 0)
{
result *= base;
--exp;
}
return result;
}
public static int square(int num)
{
return num * num;
}
public static int factorial(int base)
{
if (base == 0)
return 1;
else
return(base * factorial(base - 1));
}
}
答案1
得分: 1
你可以使用一个名为Functions的单独类,其中包含静态方法
public class Functions {
public static int add(int num1, int num2) {
return num1 + num2;
}
public static int diff(int num1, int num2) {
return num1 - num2;
}
public static int prod(int num1, int num2) {
return num1 * num2;
}
public static double quo(int num1, int num2) {
return (double) num1 / num2;
}
public static int mod(int num1, int num2) {
return num1 % num2;
}
public static long power(int base, int exp) {
long result = 1;
while (exp != 0) {
result *= base;
--exp;
}
return result;
}
public static int square(int num) {
return num * num;
}
public static int factorial(int base) {
if (base == 0)
return 1;
else
return (base * factorial(base - 1));
}
}
然后你可以调用这些方法 Functions.add(num1, num2))
我不确定这是否是你想要的。
英文:
you can use a separate class called Functions with static methods
public class Functions{
public static int add(int num1, int num2)
{
return num1 + num2;
}
public static int diff(int num1, int num2)
{
return num1 - num2;
}
public static int prod(int num1, int num2)
{
return num1 * num2;
}
public static double quo(int num1, int num2)
{
return (double)num1 / num2;
}
public static int mod(int num1, int num2)
{
return num1 % num2;
}
public static long power(int base, int exp)
{
long result = 1;
while (exp != 0)
{
result *= base;
--exp;
}
return result;
}
public static int square(int num)
{
return num * num;
}
public static int factorial(int base)
{
if (base == 0)
return 1;
else
return(base * factorial(base - 1));
}
}
and you call these methods Functions.add(num1, num2))
I'm not sure if that what you want
答案2
得分: 1
以下是您提供的代码的翻译部分:
这是一种可能对您有用的方法。 我已经实现了加法和减法,您也可以按照相同的方式进行其他操作。 您可能还想为单操作数操作创建抽象类。
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int choice;
do {
System.out.println("[1] 加法 ");
System.out.println("[2] 减法 ");
System.out.println("[3] 乘法 ");
System.out.println("[4] 除法 ");
System.out.println("[5] 平方 ");
System.out.println("[6] 幂运算 ");
System.out.println("[7] 取模运算 ");
System.out.println("[8] 阶乘 ");
System.out.println("[0] 退出 ");
System.out.println("请输入您的选择: ");
choice = s.nextInt();
int num1, num2;
switch (choice) {
case 1:
Addition addition = new Addition(s);
addition.performOperation();
break;
case 2:
Subtraction subtraction = new Subtraction(s);
subtraction.performOperation();
break;
case 3:
System.out.println("输入两个数字进行乘法运算: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println(num1 + " 与 " + num2 + " 的乘积是: " + prod(num1, num2));
break;
case 4:
System.out.println("输入两个数字进行除法运算: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println(num1 + " 除以 " + num2 + " 的商是: " + quo(num1, num2));
break;
case 5:
System.out.println("要求平方的数字: ");
num1 = s.nextInt();
System.out.println(num1 + " 的平方是: " + square(num1));
break;
case 6:
System.out.println("输入底数和指数: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println(num1 + " 的 " + num2 + " 次幂是: " + power(num1, num2));
break;
case 7:
System.out.println("输入两个数字以获取整数余数(模运算): ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println(num1 + " 和 " + num2 + " 的模运算结果是: " + mod(num1, num2));
break;
case 8:
System.out.println("输入一个数字以获取阶乘: ");
num1 = s.nextInt();
System.out.println(num1 + " 的阶乘是: " + factorial(num1));
break;
default:
System.out.println("您的选择应在0到8之间!");
break;
}
}
while (choice != 0);
s.close();
System.out.println("感谢使用我的程序... 再见!");
System.exit(0);
}
public static int prod(int num1, int num2) {
return num1 * num2;
}
public static double quo(int num1, int num2) {
return (double) num1 / num2;
}
public static int mod(int num1, int num2) {
return num1 % num2;
}
public static long power(int base, int exp) {
long result = 1;
while (exp != 0) {
result *= base;
--exp;
}
return result;
}
public static int square(int num) {
return num * num;
}
public static int factorial(int base) {
if (base == 0)
return 1;
else
return (base * factorial(base - 1));
}
}
abstract class OperationWithTwoOperands {
protected String prompt;
private Scanner scanner;
public OperationWithTwoOperands(Scanner scanner) {
this.scanner = scanner;
}
public void performOperation() {
System.out.println(prompt);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
operation(num1, num2);
}
abstract protected void operation(int operand1, int operand2);
}
class Addition extends OperationWithTwoOperands {
public Addition(Scanner scanner) {
super(scanner);
prompt = "输入两个数字进行加法运算:";
}
@Override
protected void operation(int num1, int num2) {
System.out.println(num1 + " 与 " + num2 + " 的和是: " + (num1 + num2));
}
}
class Subtraction extends OperationWithTwoOperands {
public Subtraction(Scanner scanner) {
super(scanner);
prompt = "输入两个数字进行减法运算:";
}
@Override
protected void operation(int num1, int num2) {
System.out.println(num1 + " 与 " + num2 + " 的差是: " + (num1 - num2));
}
}
请注意,这只是您提供的代码的翻译,不包括代码的完整性和功能。如果需要更多帮助或解释,请随时提出。
英文:
Here is an approach, which may be useful to you. I have done implementation for Addition and Subtraction, you can follow the same thing for other operations as well. You may want to have abstract class for single operand operations as well.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int choice;
do {
System.out.println("[1] Add ");
System.out.println("[2] Subtract ");
System.out.println("[3] Multiply ");
System.out.println("[4] Division ");
System.out.println("[5] Square ");
System.out.println("[6] Power ");
System.out.println("[7] Mod Operation ");
System.out.println("[8] Factorial ");
System.out.println("[0] Quit ");
System.out.println("Please enter your choice: ");
choice = s.nextInt();
int num1, num2;
switch (choice) {
case 1:
Addition addition = new Addition(s);
addition.performOperation();
break;
case 2:
Subtraction subtraction = new Subtraction(s);
subtraction.performOperation();
break;
case 3:
System.out.println("Enter two numbers to multiply: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println("The product of " + num1 + " and " + num2 +
" is: " + prod(num1, num2));
break;
case 4:
System.out.println("Enter two numbers to divide: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println("The quotient of " + num1 + " and " + num2 +
" is: " + quo(num1, num2));
break;
case 5:
System.out.println("A number to square: ");
num1 = s.nextInt();
System.out.println("The square of " + num1 + " is: " + square(num1));
break;
case 6:
System.out.println("Enter the base and the exponent: ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println("The power of " + num1 + " to the " + num2 +
"th power is: " + power(num1, num2));
break;
case 7:
System.out.println("Enter two numbers to get the interger remainder of (modulo): ");
num1 = s.nextInt();
num2 = s.nextInt();
System.out.println("The modulo of " + num1 + " and " + num2 +
" is: " + mod(num1, num2));
break;
case 8:
System.out.println("Enter a number to get the factorial of: ");
num1 = s.nextInt();
System.out.println("The factorial of " + num1 + " is: " + factorial(num1));
break;
default:
System.out.println("Your choices should be 0 to 8!");
break;
}
}
while (choice != 0);
s.close();
System.out.println("Thank you for using my program...good bye!");
System.exit(0);
}
public static int prod(int num1, int num2) {
return num1 * num2;
}
public static double quo(int num1, int num2) {
return (double) num1 / num2;
}
public static int mod(int num1, int num2) {
return num1 % num2;
}
public static long power(int base, int exp) {
long result = 1;
while (exp != 0) {
result *= base;
--exp;
}
return result;
}
public static int square(int num) {
return num * num;
}
public static int factorial(int base) {
if (base == 0)
return 1;
else
return (base * factorial(base - 1));
}
}
abstract class OperationWithTwoOperands {
protected String prompt;
private Scanner scanner;
public OperationWithTwoOperands(Scanner scanner) {
this.scanner = scanner;
}
public void performOperation() {
System.out.println(prompt);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
operation(num1, num2);
}
abstract protected void operation(int operand1, int operand2);
}
class Addition extends OperationWithTwoOperands {
public Addition(Scanner scanner) {
super(scanner);
prompt = "Enter two numbers to add:";
}
@Override
protected void operation(int num1, int num2) {
System.out.println("The sum of " + num1 + " and " + num2 + " is: " + (num1 + num2));
}
}
class Subtraction extends OperationWithTwoOperands {
public Subtraction(Scanner scanner) {
super(scanner);
prompt = "Enter two numbers to subtract:";
}
@Override
protected void operation(int num1, int num2) {
System.out.println("The difference of " + num1 + " and " + num2 + " is: " + (num1 - num2));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论