本文目錄一覽:
- 1、Java實驗,代碼怎麼寫?
- 2、Java實驗,在線等急
- 3、Java程序實驗
- 4、Java實驗求助
- 5、java實驗
Java實驗,代碼怎麼寫?
Shape.java接口代碼
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圓類代碼
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return PI * this.radius * this.radius;
}
public double perimeter() {
return 2 * PI * this.radius;
}
}
Cylinder.java圓柱體類代碼
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double area() {
return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
return super.area() * this.height;
}
}
X5_3_6.java主類代碼
public class X5_3_6 {
public static void main(String[] args) {
Circle cir1 = new Circle(5);
System.out.println(“圓的面積為:” + cir1.area());
System.out.println(“圓的周長為:” + cir1.perimeter());
Cylinder cy1 = new Cylinder(10, 15);
System.out.println(“圓柱體的表面積為:” + cy1.area());
System.out.println(“圓柱體的體積為:” + cy1.volume());
}
}
上面是我寫的代碼,下圖是執行結果,麻煩看一下,是否可以。
Java實驗,在線等急
運行截圖:
ps:如果輸入的數不是四位數,則要重新輸入。
源代碼:
package Ttest;
import java.util.Scanner;
public class Example002 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int x = 0;
boolean flag = true;
System.out.print(“請輸入四位整數(如3752):”);
while(flag) {
x = input.nextInt();
if(x / 1000 == 0 || x / 10000 != 0) {//判斷x是否為四位整數
System.out.print(“請重新輸入:”);
}
else {
flag = false;
}
}
int d1 = x % 10;//求模運算
x /= 10;//除法運算
int d2 = x % 10;
x /= 10;//除法運算
int d3 = x % 10;
x /= 10;//除法運算
int d4 = x % 10;
System.out.println(“d1=” + d1 + “; d2=” + d2 + “; d3=” + d3 + “; d4=” + d4);
input.close();
}
}
Java程序實驗
import java.util.Arrays;
import java.util.Scanner;
public class S {
public static void main(String[] args) {
System.out.println(“請輸入一個身份證號:”);
Scanner scanner = new Scanner(System.in);
String id = scanner.nextLine();
String pattern = “44[0-9]{16}”;
if(id.matches(pattern)){
System.out.println(“廣東”);
}else {
System.out.println(“非廣東或者非身份證號碼”);
}
System.out.println(“請輸入字符串:例如AB-C-ABC”);
String a1 = scanner.nextLine();
String a2 = a1.replaceAll(“A”, “First”);
String[] a3 = a2.split(“-“);
System.out.println(Arrays.toString(a3));
System.out.println(“請輸入第一個字符串:”);
String b1 = scanner.nextLine();
System.out.println(“請輸入第二個字符串:”);
String b2 = scanner.nextLine();
if(b1.compareTo(b2) 0){
System.out.println(b2+”在”+b1+”之前”);
}else if(b1.compareTo(b2) 0) {
System.out.println(b1+”在”+b2+”之前”);
}else {
System.out.println(“相同”);
}
System.out.println(“請輸入一個字符串:”);
String str = scanner.nextLine();
String[] ch = str.split(“”);
System.out.println(ch[1]+ch[2]+ch[3]);
}
}
需要設置中文編碼
Java實驗求助
package cn;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestDate {
// 格式:年-月-日 小時:分鐘:秒
public static final String yyyyMMddHHmmss = “yyyy-MM-dd HH:mm:ss”;
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int date = c.get(Calendar.DATE);
if(year==2016month==5date==11)
{
System.out.println(“是2016年5月11日”);
}
else
{
System.out.println(“不是2016年5月11日”);
}
//秒
long second=timeSub(“2017-05-11 10:10:10″,”2017-05-12 10:10:10”);
//天數
int day=(int) (second/(60*60*24));
System.out.println(second);
System.out.println(day);
}
/**
* 兩個日期相減
*
* @param firstTime
* @param secTime
* @return 相減得到的秒數
*/
public static long timeSub(String firstTime, String secTime) {
long first = stringtoDate(firstTime, yyyyMMddHHmmss).getTime();
long second = stringtoDate(secTime, yyyyMMddHHmmss).getTime();
return (second – first) / 1000;
}
/**
* 把符合日期格式的字符串轉換為日期類型
*
* @param dateStr
* @return
*/
public static java.util.Date stringtoDate(String dateStr, String format) {
Date d = null;
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
formater.setLenient(false);
d = formater.parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
d = null;
}
return d;
}
}
java實驗
import java.util.Scanner;
public class MyExam1_4 {
public static double getArea(double r) {
final double PI = 3.14; // 定義常量 PI
return PI * r * r; // 計算圓的面積並返回 // —–1—–
}
public static void main(String[] args) {
double radium, area;
System.out.println(“請輸入半徑:”);
radium = new Scanner(System.in).nextDouble(); // —–2—–
area = getArea(radium); // 調用 getArea 方法 // —–3—–
System.out.println(“圓的半徑是” + radium + ” ,面積是” + area);
}
}
完整代碼了,樓主可直接測
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/151029.html