怎么用Java编程分段函数求值

怎么用Java编程分段函数求值,第1张

import java.util.Scanner

public class Admin {

public static void main(String[] args) {

Scanner in = new Scanner(System.in)

int x = in.nextInt()

int y = x

if (x >= 1 &&x <10) {

y = 3 * x - 2

} else if (x >= 10) {

y = 4 * x

}

System.out.println(y)

}

}

import java.util.Scanner

//递归法求阶乘,eclipse编译通过

public class Test {

public static void main(String srag[]){

int num = 0

while(true){

System.out.println("请输入一个正整数:")

Scanner sc = new Scanner(System.in)

num = sc.nextInt()

if(num>=0) break

}//如果输入的为正整数,跳出循环

System.out.println(num + "!=" + jc(num))

}

static int jc(int a){

if(a==1) return 1

else{

return a*jc(a-1)

}

} //递归法求阶乘

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12078744.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存