3 changed files with 178 additions and 0 deletions
@ -0,0 +1,118 @@ |
|||
**Java源码** |
|||
|
|||
import java.util.Scanner; |
|||
|
|||
public class TemperatureConverter { |
|||
|
|||
public static double celsiusToFahrenheit(double c){ |
|||
|
|||
return c\*9.0/5.0+32.0; |
|||
|
|||
} |
|||
|
|||
public static double fahrenheitToCelsius(double f){ |
|||
|
|||
return(f-32.0)\*5.0/9.0; |
|||
|
|||
} |
|||
|
|||
public static void main(String\[] args){ |
|||
|
|||
if (args.length>=2){ |
|||
|
|||
try{ |
|||
|
|||
double value=Double.parseDouble(args\[0]); |
|||
|
|||
String unit=args\[1].toUpperCase(); |
|||
|
|||
if(unit.startsWith("C")){ |
|||
|
|||
double f=celsiusToFahrenheit(value); |
|||
|
|||
System.out.printf("%.2f℃=%.2f℉%n",value,f); |
|||
|
|||
}else if (unit.startsWith("F")){ |
|||
|
|||
double c=fahrenheitToCelsius(value); |
|||
|
|||
System.out.printf("%.2f℉=%.2f℃%n",value,c); |
|||
|
|||
}else { |
|||
|
|||
System.out.println("未知单位,请使用 C 或 F。"); |
|||
|
|||
} |
|||
|
|||
}catch(Exception e){ |
|||
|
|||
System.out.println("命令行解析失败,请按示例输入,例如:java TemperatureConverter 36.6 C"); |
|||
|
|||
} |
|||
|
|||
}else{ |
|||
|
|||
mainLogic(); |
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
public static void mainLogic(){ |
|||
|
|||
Scanner scanner=new Scanner(System.in); |
|||
|
|||
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); |
|||
|
|||
String s=scanner.nextLine().trim(); |
|||
|
|||
if (s.isEmpty()){ |
|||
|
|||
System.out.println("'输入为空,程序退出。"); |
|||
|
|||
return; |
|||
|
|||
} |
|||
|
|||
String\[] parts=s.split("\\\\s+"); |
|||
|
|||
try{ |
|||
|
|||
double value=Double.parseDouble(parts\[0]); |
|||
|
|||
String unit=parts.length>1? parts\[1].toUpperCase():"C"; |
|||
|
|||
if (unit.startsWith("C")){ |
|||
|
|||
double f=celsiusToFahrenheit(value); |
|||
|
|||
System.out.printf("%.2f℃=%.2f℉%n",value,f); |
|||
|
|||
}else if(unit.startsWith("F")){ |
|||
|
|||
double c=fahrenheitToCelsius(value); |
|||
|
|||
System.out.printf("%.2f℉=%.2f℃%n",value,c); |
|||
|
|||
}else{ |
|||
|
|||
System.out.println("未知单位,请使用 C 或 F。"); |
|||
|
|||
} |
|||
|
|||
}catch(Exception e){ |
|||
|
|||
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); |
|||
|
|||
}finally{ |
|||
|
|||
scanner.close(); |
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
|
|||
|
|||
@ -0,0 +1,60 @@ |
|||
**异常数据拦截与清洗流水线** |
|||
|
|||
public class DataCleaner { |
|||
|
|||
  public static void main(String\[] args) { |
|||
|
|||
  int\[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76}; |
|||
|
|||
  int validSum = 0; |
|||
|
|||
  int validCount = 0; |
|||
|
|||
  for (int data:sensorData){ |
|||
|
|||
  if (data==999){ |
|||
|
|||
  System.out.println("致命错误:传感器掉线,终止处理"); |
|||
|
|||
  break; |
|||
|
|||
  } |
|||
|
|||
  if(data>=1 \&\& data<=100){ |
|||
|
|||
  validSum +=data; |
|||
|
|||
  validCount++; |
|||
|
|||
  } |
|||
|
|||
  else if (data<=0||(data>10 \&\& data!=999)){ |
|||
|
|||
  System.out.println("警告:发现越界数据 \["+data+"],已跳过"); |
|||
|
|||
  continue; |
|||
|
|||
  } |
|||
|
|||
  } |
|||
|
|||
  if(validCount>0){ |
|||
|
|||
  double average=(double) validSum/validCount; |
|||
|
|||
  System.out.println("有效数据的平均值"+average); |
|||
|
|||
  }else{ |
|||
|
|||
  System.out.println("无有效数据"); |
|||
|
|||
  } |
|||
|
|||
  } |
|||
|
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
Loading…
Reference in new issue