如果某人输入除数字之外的任何内容(例如“*”/“xyz”),我在查找如何显示“无效输入再见”消息时遇到问题为一个,b,C的在二次计算器值,我提出如果输入不是数字,则显示错误消息
DecimalFormat df = new DecimalFormat("0.00#");
Scanner sc = new Scanner(System.in);
Double a,b,c;
System.out.println("Welcome to the Grand Quadratic Calculator ! ");
System.out.print("Enter value of a = ");
a = sc.nextDouble();
System.out.print("Enter value of b = ");
b = sc.nextDouble();
System.out.print("Enter value of c = ");
c = sc.nextDouble();
double xone = (-b + Math.sqrt(b * b - 4 * a * c))/2 * a;
double xtwo = (-b - Math.sqrt(b * b - 4 * a * c))/2 * a;
if(b * b - 4 * a * c >= 0)
{
System.out.println("x1 = " + df.format(xone));
System.out.print("x2 = " + df.format(xtwo));
}
else
System.out.print("Invalid Input. \nGood Bye.");
你是什么意思?如果输入不是数字,你想打印吗? – ItamarG3
在'else'内使用'hasNextDouble'和'if'以及'nextLine'来清除不需要的输入 –