你能帮我改进一下这段代码吗? (JAVA) [英] Can you help me improve this code? (Java)

查看:56
本文介绍了你能帮我改进一下这段代码吗? (JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是新来的,所以如果我在错误的类别中发布这个,我很抱歉。我是一名初学者,我正努力变得更好。现在我正在写一个解决数学问题的小工具。如果你能告诉我我做错了什么以及如何改进我的代码(该工具正在运行),我将非常感激。该项目尚未完成,到目前为止我只做了一个简单的计算器。但是我打算像这个简单的计算器一样构建其他功能,所以任何反馈都会很好。



我尝试了什么:



这是当前的代码,整个事情在while循环中的原因是因为我希望用户能够多次使用计算器工具而无需每次都要运行应用程序。



Hello everyone, i am new here so i am sorry if i am posting this in the wrong category. I'm a begginer programmer and i am trying to get better. Right now i am writing a small tool that solves mathematical problems. I would very much appreciate it if you could tell me what i'm doing wrong and how i could improve my code (the tool is working). The project is not finished yet and thus far i have only made a simple calculator. However i intend to build the other features the same way as this simple calculator so any feedback would be nice.

What I have tried:

This is the current code, the reason the whole thing is inside a while loop is because i want the user to be able to use the calculator tool multiple times without having to run the app every time.

import java.util.Scanner;
public class NumberHeron
{
	public static void main(String[] args)
	{
		System.out.println("Choose your prefered tool\n");
		System.out.println("1. Simple Calculator\n");
		Scanner scanner = new Scanner(System.in);
		int tool = scanner.nextInt();
		switch(tool)
		{
			case 1:
		    while(tool == 1)
			{
			try {
			System.out.println("Enter two numbers");
			double numOne = scanner.nextDouble();
			double numTwo = scanner.nextDouble();
			System.out.println("Enter an operation, valid ones are: +, -, *, /");
			System.out.println("Type (q) to exit");
			Scanner s = new Scanner(System.in);
			String operation = s.nextLine();
			if (operation.equals("+"))
			{
				SimpleCalculator simpleCalc = new SimpleCalculator();
				double result = simpleCalc.add(numOne, numTwo);
				System.out.println("The result is " + result);
			}
			else if (operation.equals("-"))
			{
				SimpleCalculator simpleCalc = new SimpleCalculator();
				double result = simpleCalc.substract(numOne, numTwo);
				System.out.println("The result is " + result);
			}
			else if (operation.equals("*"))
			{
				SimpleCalculator simpleCalc = new SimpleCalculator();
				double result = simpleCalc.multiply(numOne, numTwo);
				System.out.println("The result is " + result);
			}
			else if (operation.equals("/"))
			{
				SimpleCalculator simpleCalc = new SimpleCalculator();
				double result = simpleCalc.divide(numOne, numTwo);
				System.out.println("The result is " + result);
			}
			else if (operation.equals("q"))
			{
				break;
			}
		  } catch(Exception ex) { ex.printStackTrace(); break; }
		}
		}
		scanner.close();
	}
}
class SimpleCalculator 
{
	public static double add(double valueOne,double valueTwo)
	{
		double result = valueOne + valueTwo;
		return result;
	}
	public static double substract(double valueOne, double valueTwo)
	{
		double result = valueOne - valueTwo;
		return result;
	}
	public static double multiply(double valueOne, double valueTwo)
	{
		double result = valueOne * valueTwo;
		return result;
	}
	public static double divide(double valueOne, double valueTwo)
	{
		double result = valueOne / valueTwo;
		return result;
	}
}

推荐答案

if (operation.equals("+"))
{
	SimpleCalculator simpleCalc = new SimpleCalculator();
	double result = simpleCalc.add(numOne, numTwo);
	System.out.println("The result is " + result);
}
else if (operation.equals("-"))
{
	SimpleCalculator simpleCalc = new SimpleCalculator();
	double result = simpleCalc.substract(numOne, numTwo);
	System.out.println("The result is " + result);
}



您可以通过删除冗余的代码行来简化此块。并且您不需要 SimpleCalculator 的实例,因为它的所有方法都是 static


You coluld simplify this block by removing the redundant lines of code. And you do not need an instance of SimpleCalculator as all its methods are static.

double result =0.0;
if (operation.equals("+"))
{
    result = SimpleCalculator.add(numOne, numTwo);
}
else if (operation.equals("-"))
{
    result = SimpleCalculator.substract(numOne, numTwo);
}
// etc for other operations
System.out.println("The result is " + result);





为了更好地使用Java进行培训,我建议 Java™教程 [ ^ ]。


这篇关于你能帮我改进一下这段代码吗? (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆