我尝试了多种方法,但我仍然无法弄清楚如何使我的代码询问用户是否要在 java 中再次运行它。这是我两天后到期的大学项目。
我已经尝试了几个 do-while 语句,但它仍然不起作用。
public class Project_2_1
{
public static void main(String args[])
{
System.out.println("My project");
System.out.println("Project_2 Problem_1\n");
System.out.println("This program computes both roots of a quadratic equation,\n");
System.out.println("Given the coefficients A,B, and C.\n");
double secondRoot = 0, firstRoot = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of a ::");
double a = sc.nextDouble();
System.out.println("Enter the value of b ::");
double b = sc.nextDouble();
System.out.println("Enter the value of c ::");
double c = sc.nextDouble();
double determinant = (b*b)-(4*a*c);
double sqrt = Math.sqrt(determinant);
if(determinant>0)
{
firstRoot = (-b + sqrt)/(2*a);
secondRoot = (-b - sqrt)/(2*a);
System.out.println("Roots are :: "+ firstRoot +" and "+secondRoot);
}else if(determinant == 0){
System.out.println("Root is :: "+(-b + sqrt)/(2*a));
}
}
}
慕少森
叮当猫咪
三国纷争
相关分类