可以多次调用以下代码中的构造函数创建的对象。我是否通过这样做严重编写代码我希望它写入适当的约定
import java.util.Scanner;
/**
* this class uses the scanner to take input from the keyboard
* test if the input is a variable of type double if it is store in a
* variable called numOne then take next input test again for type double
* store in variable numTwo, then add the two variables together in the form
* of variable answer and return to the screen. The user then is asked for
* continuation as Y/N then appropriate action is taken
*/
public class Calc {
/**
* this constructer does not throw InputMismatchException
*/
public Calc() {
Scanner in = new Scanner(System.in); // create scanner object
System.out.println("please enter 1st "
+ "number then press enter"
+ "\nthen enter 2nd number then "
+ "press enter again");
/*test for a double returns true */
boolean myBool = in.hasNextDouble();
/*the actual test for a double*/
if(myBool == false) {
System.out.println("wrong format");
/**
* call constructer and instantiate as new object
* by creating a new object i hope to have overwriten
* the original object stored in memory there by getting
* around the problem of hasNextDouble not allowing the
* scanner to advance
*/
Calc c = new Calc();
} else { // 1st else
double numOne = in.nextDouble(); // keyboard input
/*test for a double returns true */
boolean myBool2 = in.hasNextDouble();
/*the actual test for a double*/
if(myBool2 == false) {
System.out.println("wrong format start again");
/**
* call constructer and instantiate as new object
* there by removing need for InputMismatchException
*/
Calc c = new Calc();
} else { // 2nd else
double numTwo = in.nextDouble(); // keyboard input
double answer = numOne + numTwo; // the maths
for(int i = 0; i<35; i++) { // print pattern
System.out.print("*");
}
System.out.println("\n**** "
+ "Your 1st number is "
+ numOne + " ****");
System.out.println("**** " // print operator
+ " " + "+"
+ " ****");
System.out.println("**** "
+ "Your 2nd number is "
+ numTwo + " ****");
System.out.println("**** " // print line
+ " ______ ****");
System.out.println("**** "
+ "The answer is "
+ answer + " ****");
for(int i = 0; i<35; i++) { //print pattern
System.out.print("*");
}
System.out.println("\nDo you have "
+ "more calculations Y or N");
String reply = in.next(); //keyboard input
if(reply.equalsIgnoreCase("Y")) {
Calc c = new Calc();
} else {
System.out.println("OK bye then");
in.close(); //close scanner
} //end else
} // end 1st else
} // end 2nd else
} // end constructor
public static void main(String[] args) {
Calc c = new Calc(); // call constructor
} // end main
} // end class
// just in case i do something wrong thanks in anticipation Simon.
答案 0 :(得分:2)
从同一构造函数中调用构造函数,并且没有任何参数,看起来像是获得无限循环和堆栈溢出的可靠方法。所以,是的,这是错误的。
因为它看起来好像你在构造函数中创建的Calc
的所有实例都做了什么,所以它毫无意义。
答案 1 :(得分:0)
在与实例化该对象无直接关系的构造函数中进行任何工作通常被视为不好的做法。考虑将您的逻辑转移到Calc
上的另一种方法。这样你可以使用Calc
的单个实例,并使用该实例上的字段,你可能会更好地覆盖你想要覆盖的评论值。
正如Eran所提到的,您当前的模式使用递归调用,如果操作不正确,则会出现堆栈溢出。我不会说递归调用总是一件坏事,但你应该仔细考虑这是否是你正在做的最好的模式。
答案 2 :(得分:0)
虽然您(可能)不会收到错误,但这在很多方面都是不好的做法。
首先,您不应该在构造函数中执行任何操作。构造函数应该创建一个对象或抛出异常,就是它。如果构造函数需要任何输入,请事先读取它并将其作为参数传递给构造函数。 构建器不是用于用户交互的。
下一点是错误处理。如果用户输入无效,则绝对不需要使用递归。当然,你不会得到StackOverflowError(除非你的用户失败几千次),但循环是一个更好的解决方案。
System.out.print("Enter a double please: ");
while(!scanner.hasNextDouble()) {
System.out.print("Invalid input. Please try again: ");
scanner.next(); //discard invalid input
}
double input = scanner.nextDouble();