1.
(1)
import java.util.Scanner; public class test { public static void main(String[] args) { irr l=new irr(); l.method(); Scanner sc=new Scanner(System.in); sc.next(); } }
(2)
public interface inter { //默认在int前加上public static final int a=10; //默认在成员方法前加public abstract void method(); }
(3)
public class irr implements inter{ @Override public void method() { System.out.println("method"); } }
2
(1)
public class test implements inter1,inter2 { public static void main(String[] args) { //接口有重名,重写不用多写 } @Override public void method1 () { } @Override public void method2 () { } @Override public void method3 () { } @Override public void method4 () { } }
(2)
public interface inter1 { public abstract void method1(); public abstract void method2(); public abstract void method3(); public abstract void method4(); }
(3)
public interface inter2 { public abstract void method1(); public abstract void method2(); public abstract void method3(); }
3
(1)
public interface inter1 { public abstract void method1(); }
(2)
public interface inter2 { public abstract void method2(); }
(3)
public interface inter3 extends inter1,inter2{ public abstract void method3(); }
(4)
//实现类
public class irr implements inter3{ @Override public void method3() { } @Override public void method1() { } @Override public void method2() { } }
4.
(1)有判断是否为double
import java.util.Scanner; public class jisuanpingfanggen1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入你的值"); if (sc.hasNextDouble()) { // 先判断输入是否是能转换为double类型的内容 double num = sc.nextDouble(); // 获取double类型的输入值 double result = sqrt(num); // 调用sqrt方法计算平方根 System.out.println("输入值的平方根是:" + result); } else { System.out.println("输入的不是合法的数值,请重新输入"); } sc.close(); // 关闭Scanner对象,释放资源 } public static double sqrt(double c) { if (c < 0) return Double.NaN; double err = 1e-15; double t = c; while (Math.abs(t - c / t) > err * t) t = (c / t + t) / 2.0; return t; } }
(2)无判断
import java.util.Scanner; public class jisuanpingfanggen { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入你的值"); double num=sc.nextDouble(); double result=sqrt(num); System.out.println("输入的平方根是"+result); sc.close(); } public static double sqrt ( double c) { if (c < 0) return Double.NaN; double err = 1e-15; double t = c; while (Math.abs(t - c / t) > err * t) t = (c / t + t) / 2.0; return t; } }标签:Java,Scanner,double,void,System,abstract,public,迭代法,平方根 From: https://blog.csdn.net/l13849427451/article/details/144040924