1.计算出8/5的值保留小数点1位。
class calculate{
public static void main(String[] args){
double result = 8.6/5.0;
ps("%.1f",result);
}
public static <T> void p(T t){
System.out.println("result:"+t);
}
public static <T> void ps(String s,T t){
System.out.printf(s,t);
}
}
2.复杂的表达式计算
class calculate{
public static void main(String[] args){
double result = 1+2*Math.sqrt(3)/(5-0.1);
ps("%.8f",result);
}
public static <T> void p(T t){
System.out.println("result:"+t);
}
public static <T> void ps(String s,T t){
System.out.printf(s,t);
}
}
3.计算圆柱体表面积,输出圆柱体表面积,保留3位小数
样例输入:3.5 9
样例输出:Area = 274.889
过程:
double jishuPI(double z){
double sum=2;
int n=1;
int m=3;
double t=2;
while(t>z){
t=t*n/m;
sum=sum+t;
n++;
m+=2;
}
return sum;
}
java程序:
import java.util.Scanner;
class calculate{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
s("please input a r and a h to get area");
double r = sc.nextFloat();
double h = sc.nextFloat();
double pi = jishuPI(1e-15);
double result = 2*pi*r*r + 2*pi*r*h;
ps("Area = %.3f",result);
}
public static <T> void p(T t){
System.out.println("result:"+t);
}
public static void s(String s){
System.out.println(s);
}
public static <T> void ps(String s,T t){
System.out.printf(s,t);
}
public static double jishuPI(double z){
double sum=2;
int n=1;
int m=3;
double t=2;
while(t>z){
t=t*n/m;
sum=sum+t;
n++;
m+=2;
}
return sum;
}
}
顺序结构程序设计
4.三位数反转,输入一个数,分离出它的百位、十位、个位,反转后输出
样例输入:127
样例输出:721
import java.util.Scanner;
class calculate{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int bai = r/100;
int shi = (r/10%10) * 10;
int ge = (r%10) * 100;
if(ge == 0){
ps("Area = %s","0"+(shi+bai));
}else{
ps("Area = %d",ge+shi+bai);
}
}
public static <T> void ps(String s,T t){
System.out.printf(s,t);
}
}
5.变量转换,输入两个整数a和b,交换二者值,然后输出
样例输入:824 16
样例输出:16 824
方法一:
import java.util.Scanner;
class calculate{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = a;
a = b;
b = c;
ps("%d %d",a,b);
}
public static <T> void ps(String s,T t,T r){
System.out.printf(s,t,r);
}
}
方法二:
import java.util.Scanner;
class calculate{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
a += b;
b = a - b;
a = a - b;
ps("%d %d",a,b);
}
public static <T> void ps(String s,T t,T r){
System.out.printf(s,t,r);
}
}
方法三:
import java.util.Scanner;
class calculate{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
ps("%d %d",b,a);
}
public static <T> void ps(String s,T t,T r){
System.out.printf(s,t,r);
}
}
分支结构设计
已知鸡和兔的总数为n,总腿数为m,输入n和m,依次输出鸡和兔的数目,如果无解,则输出“No Answer”
样例输入:14 32
样例输出:12 2
样例输入:10 16
样例输出:No Answer
import java.util.Scanner;
class calculate{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int y = (m - 2*n)/2;
int x = (4*n - m)/2;
int nImpl = x + y;
int mImpl = 2*x + 4*y;
if(nImpl == n && mImpl == m && x >= 0 && y >= 0){
ps("%d %d",x,y);
}else{
System.out.printf("No Answer");
}
}
public static <T> void ps(String s,T t,T r){
System.out.printf(s,t,r);
}
}
三整数排序
输入3个整数,从小到大排序后输出
输入样例:20 7 33
输出样例:7 20 33
import java.util.Scanner;
class calculate{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] a = new int[3];
a[0] = sc.nextInt();
a[1] = sc.nextInt();
a[2] = sc.nextInt();
//冒泡排序
for(int i = 0;i < a.length - 1;i++){
if(a[i] > a[i+1]){
int t = a[i];
a[i] = a[i+1];
a[i+1] = t;
}
}
System.out.printf("%d %d %d",a[0],a[1],a[2]);
}
}