我正在为牛顿迭代做一个JAVA项目,但我仍然坚持如何让程序不断询问用户是否要继续进行。以及如何确保当用户输入负数时,结果未显示根是用户输入的负数。
import java.util.Scanner;
/**
* Computes estimate of square root of x to within relative error 0.01%.
*
* @param x
* positive number to compute square root of
* @return estimate of square root
*/
public final class Newton1 {
private static double sqrt(double x) {
if (x < 0) {
System.out.println("Please enter a postive number.");
return x;
}
double Error = 0.0001;
double t = x;
while (Math.abs(t - x / t) > Error * t) {
t = (x / t + t) / 2.0;
}
return t;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number: ");
double x = in.nextDouble();
System.out.println("Square root is " + sqrt(x));
System.out.println("Do you want to continue? (Enter Y or N): ");
String f = in.nextLine();
while (true) {
if (f.equalsIgnoreCase("Y")) {
} else {
break;
}
}
}
}
答案 0 :(得分:0)
您的Exception [EclipseLink-41] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(model.Exercises --> [DatabaseTable(weighttrack.exercises)])
Runtime Exceptions:
---------------------------------------------------------
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:820)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:760)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
at model.User.initComponents(User.java:39)
at model.User.<init>(User.java:23)
at model.Dashboard.initComponents(Dashboard.java:35)
at model.Dashboard.<init>(Dashboard.java:18)
at model.Dashboard$3.run(Dashboard.java:153)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [weighttrack?zeroDateTimeBehavior=convertToNullPU] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions:
---------------------------------------------------------
Exception [EclipseLink-46] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: There should be one non-read-only mapping defined for the primary key field [weighttrack.exercises.exercise_id].
Descriptor: RelationalDescriptor(model.Exercises --> [DatabaseTable(weighttrack.exercises)])
Exception [EclipseLink-41] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(model.Exercises --> [DatabaseTable(weighttrack.exercises)])
声明似乎位于错误的位置。试试这个:
while
请注意,在用户输入非负数之前,系统会继续提示他们输入非负数。