我是Java的初学者,正在学习如何使用方法。我想做一个简单的计算器,有效地使用方法。我想回顾一下我的程序,我也想知道是否有更好的方法或快捷方式来更好地编写这个程序。
同时我也想知道好的编码风格。我在我的程序中练习了好的编码风格吗?我在哪里没有练习好的风格?我如何改进这个程序,使它在一个良好的编码风格?
import java.text.DecimalFormat;
import java.util.Scanner;
public class DoTheMath_A {
public static void main(String[] args) {
// First number storage
double num1 = getFirstNumber(); // Value - returning method.
// Second number storage
double num2 = getSecondNumber(); // Value - returning method.
// Displays Math Operations menu
showMenu(); // Void - returning method
// Math Operation choice storage
int choice = getSelection(); // Value - returning method
// Calculates the users numbers into an result. No output.
double result = doTheMath(choice, num1, num2); // Value - returning
// method
// Displays the output with formatting to show 2 decimal places.
displayResult(choice, result, num1, num2); // Void - returning method
} // end of main()
// Value - returning method =======================================
private static double getFirstNumber() {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = input.nextDouble(); // First number storage
return num1; // Returns First number to main
} // end of getFirstNumber()
// Value - returning method =======================================
private static double getSecondNumber() {
Scanner input = new Scanner(System.in);
System.out.print("\nEnter the second number - "
+ "(Cannot be zero for Divison): ");
double num2 = input.nextDouble(); // Second number storage
return num2;
} // end of getSecondNumber()
// Void - returning method ============================
private static void showMenu() {
System.out.print("\n1. Addition\n2. Subtract\n3. Multiply"
+ "\n4. Divide\n\nWhich math operation? "
+ "Enter your choice: ");
} // end of showMenu()
// Value - returning method =======================================
private static int getSelection() {
Scanner input = new Scanner(System.in);
int choice = input.nextInt(); // Reads User pick for math operation and
// stores it into 'choice'
switch (choice) { // Start of Switch
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
System.out.println("Illegal Operation");
} // end of Switch
return choice;
} // end of getSelection()
// Value - returning method =======================================
private static double doTheMath(int choice, double num1, double num2) {
double result;
if (choice == 1)
result = num1 + num2;
else if (choice == 2)
result = num1 - num2;
else if (choice == 3)
result = num1 * num2;
else
result = num1 / num2;
return result;
} // end of doTheMath(int choice, double num1, double num2)
// Void - returning method =======================================
private static void displayResult(int choice, double result, double num1, double num2) {
// formatting numbers to 1 decimal places
DecimalFormat df = new DecimalFormat("#,###,##0.0");
if (choice == 1)
System.out.println(num1 + " + " + num2 + " = " + df.format(result));
else if (choice == 2)
System.out.println(num1 + " - " + num2 + " = " + df.format(result));
else if (choice == 3)
System.out.println(num1 + " * " + num2 + " = " + df.format(result));
else
System.out.println(num1 + " / " + num2 + " = " + df.format(result));
} // end of displayResult(int choice, double result, double num1, double
// num2)
} // end of main()
/* Output ==========================
Enter the first number: 5
Enter the second number - (Cannot be zero for Divison): 2
1. Addition
2. Subtract
3. Multiply
4. Divide
Which math operation? Enter your choice: 4
5.0 / 2.0 = 2.5
*/发布于 2015-07-20 16:03:34
在这个Java代码上做得很好。它很整洁,很有条理。
像这样的评论:
// end of getFirstNumber()这是:
// Value - returning method =======================================这是:
// Returns First number to main完全没有必要。
我知道最后一个}是getFirstNumber的终结;通过阅读您的代码,我就可以看到这一点。我知道该方法返回一个值;我可以在这个注释下面看到这个值。我知道return语句是将一个数字返回给任何调用它的人。
然而,你的第二个评论是正确的。您应该更多地了解JavaDoc,因为在这里您可以描述返回值和方法的其他内容。
在您的方法getFirstNumber和getSecondNumber中,您创建了如下所示的新java.util.Scanner:
Scanner input = new Scanner(System.in);我不知道你是怎么想的,但对于我来说,这似乎有点不必要,我不得不为每个方法创建相同的东西。
与其在这些方法中创建这个Scanner,不如将其设置为该类的一个字段。然后,不是创建两个Scanners,您只是创建和使用一个。
让我们看看getFirstNumber和getSecondNumber的方法主体:
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = input.nextDouble(); // First number storage
return num1; // Returns First number to mainScanner input = new Scanner(System.in);
System.out.print("\nEnter the second number - "
+ "(Cannot be zero for Divison): ");
double num2 = input.nextDouble(); // Second number storage
return num2;这两个方法看起来是相同的,只是第二个方法在变量名的末尾添加了一个2,并添加了一个不同的消息。
由于它们非常相似,所以最好将这两个方法合并到一个方法中;毕竟,它们都从用户返回一个double输入。
现在,您可能会想,我该如何告诉用户不要输入0呢?嗯,您已经有了一个问题:您不需要检查用户是否真的输入了0。
因此,如果用户输入了0并试图将其除以0,您就可以将此消息排除在外,并向用户发送一个错误。
你的设计有问题。下面是发生的情况:
看到问题了吗?这是在步骤5:当然,您告诉用户他们输入了一个无效的操作,但是您没有对它做任何事情;您只是继续尝试执行操作,就好像什么都没有发生一样。
这显然不太好。您希望程序对此做一些事情,输入无效的输入。在这种情况下,与其继续执行代码,程序应该返回开始并再次提示用户。
这可以使用while循环来实现。
在代码(伪代码)中有一个类似的构造(伪代码):
if choice == 1
[code]
if choice == 2
[code]
if choice == 3
[code]
if choice == 4
[code]如果您使用这个叫做枚举的东西,您就不必写这三次了。
现在,您在post和post的标记中指定您是Java的开始,因此这可能非常令人困惑。但是,如果你读了我刚刚发布的链接,你应该可以跟随。
我以前发布过一个关于在计算器中使用枚举的答案,我建议您阅读它,因为我会为您的代码共享相同的解决方案。
https://codereview.stackexchange.com/questions/97473
复制相似问题