
正文
java编程代码样本 java 代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA编程,帮帮忙,写一下代码
第一题:
public static final void main(String[] args) {
long sum = 0;
for (int i = 1; i = 10; i++) {
sum += factorial(i);
}
System.out.println(sum);
}
/**
* 计算阶乘
*/
public static long factorial(long n) {
if (n 0) {
throw new IllegalArgumentException("n 0");
}
if (n == 0) {
return 1L;
}
return n * factorial(n - 1);
}
第二题:使用正则表达式判断字符类型
import java.util.regex.Pattern;
public class Test {
// 判断大写字母的正则表达式
public static final Pattern P_UPPER_CASE = Pattern.compile("[A-Z]");
// 判断小写字母的正则表达式
public static final Pattern P_LOWER_CASE = Pattern.compile("[a-z]");
// 判断数字的正则表达式
public static final Pattern P_NUMBER = Pattern.compile("[0-9]");
public static final void main(String[] args) {
char[] arr = {'a', 'b', 'c', '1', 'W', '0', 'L', 'd', '8'};
int countUpper = 0;
int countLower = 0;
int countNumber = 0;
for (int i = 0; i arr.length; i++) {
String tmp = arr[i] + "";
if (P_UPPER_CASE.matcher(tmp).matches()) {
countUpper++;
} else if (P_LOWER_CASE.matcher(tmp).matches()) {
countLower++;
} else if (P_NUMBER.matcher(tmp).matches()) {
countNumber++;
}
}
System.out.println(String.format("大写字母个数:%d,小写字母个数:%d,数字个数:%d", countUpper, countLower, countNumber));
}
}
第三题:
import java.util.Scanner;
public class Test {
public static final void main(String[] args) throws InterruptedException {
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("请输入温度:");
double temperature = in.nextDouble();
try {
process(temperature);
} catch (TemperatureOutofRangeException e) {
System.err.println("孵化室温度异常,请立即检查!");
}
Thread.sleep(1000);
}
}
public static void process(double temperature) throws TemperatureOutofRangeException {
if (temperature = 36 temperature = 39) {
System.out.println("孵化状态正常!");
} else {
throw new TemperatureOutofRangeException();
}
}
}
// 异常类
public class TemperatureOutofRangeException extends Exception {
public TemperatureOutofRangeException() {
super();
}
}
相关问答
Q1: 求java编程的代码
代码如下java编程代码样本,望采纳
public class PrintPrime{
public static void main(String args[]){
//设置一个计数变量countjava编程代码样本,用于统计一行当中已经输出数字java编程代码样本的个数
int count = 0;
//写代码时人为判断200为非素数,如果不考虑题目的严格要求的话,可以写成200
for(int i = 100;i=200;i++){
//判断数字是否为素数,若是,则count+1并输出数字
if(PrintPrime.IsPrime(i)){
count++;
System.out.print(i+" ");
}
//如果一行十个已经输出完毕,计数归零,换行
if(count==10){
count=0;
System.out.println();
}
}
}
//判断数字是否为素数
public static boolean IsPrime(int n){
//如果小于等于三,则大于一即为素数
if (n = 3) {
return n 1;
}
//从2循环到数字的开平方,算法优化
for(int i=2;i=Math.sqrt(n);i++){
if(n%i == 0)
return false;
}
return true;
}
}
Q2: Java编程(写出程序代码)
import java.util.Scanner;
/*输入一个成绩,将该成绩转换为A、B、C、D和E:成绩在90分以上,结果为A,成绩在80-90之间,结果为B,成绩在70-80之间,结果为C,成绩在60-70之间,结果为D,成绩在60分以下,结果为E。*/
public class Chengfabiao
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
if(score90)
{
System.out.println("A");
}
else if(score=80)
{
System.out.println("B");
}
else if(score=70)
{
System.out.println("C");
}
else if(score=60)
{
System.out.println("D");
}
else
{
System.out.println("E");
}
}
}
java编程代码样本的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 代码、java编程代码样本的信息别忘了在本站进行查找喔。






