1、最新整理JAVA代码集/*程序填空,不要改变与输入输出有关的语句。输入整数a和b,计算并输出a、b的和与差。例:输入2 -8输出The sum is -6The difference is 10*/import java.io.*;import java.util.Scanner;public class Test20001 public static void main(String args) int a, b, sum, diff; Scanner in=new Scanner(System.in); a=in.nextInt(); b=in.nextInt(); sum=a+b; di
2、ff=a-b; /*-*/ System.out.println(The sum is +sum); System.out.println(The difference is +diff); /*程序填空,不要改变与输入输出有关的语句。输入1个实数x,计算并输出其平方根。例:输入1.21输出The square root of 1.21 is 1.1*/import java.io.*;import java.util.Scanner;public class Test20002 public static void main(String args) double x, root; Scan
3、ner in=new Scanner(System.in); x=in.nextDouble(); root=Math.sqrt(x); /*-*/ System.out.println(The square root of +x+ is +root); /*程序填空,不要改变与输入输出有关的语句。输入华氏温度f,计算并输出相应的摄氏温度c。c = 5/9(f-32).例:括号内是说明输入17.2 (华氏温度)输出The temprature is -8.222222222222223*/import java.util.Scanner;public class Test20003 publi
4、c static void main(String args) Scanner in=new Scanner(System.in); double f, c; f=in.nextDouble(); c = 5*(f-32); c=c/9; /*-*/ System.out.println(The temprature is +c); /*程序填空,不要改变与输入输出有关的语句。输入2个整数time1和time2,表示火车的出发时间和到达时间,计算并输出旅途时间。有效的时间范围是0000到2359,不需要考虑出发时间晚于到达时间的情况。例:括号内是说明输入712 1411(出发时间是7:10,到
5、达时间是14:11)输出The train journey time is 6 hrs 59 mins.*/import java.util.Scanner;public class Test20004 public static void main(String args) Scanner in=new Scanner(System.in); int time1, time2, hours, mins,a,b; time1=in.nextInt(); time2=in.nextInt(); a=time1/100; b=time2/100; hours=b-a; mins=(time2-10
6、0*b)-(time1-100*a); if(mins0)hours-;mins+=60; /*-*/ System.out.println(The train journey time is +hours+ hrs + mins+ mins.); /*程序填空,不要改变与输入输出有关的语句。输入1个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字,最后将第1位和第3位上的数字互换,第2位和第4位上的数字互换,组成加密后的新数。例:括号内是说明输入1257输出The encrypted number is 4601(每一位上的数字加9除以10取余后,
7、得0146,交换后得到4601)*/import java.util.Scanner;public class Test20005 public static void main(String args) Scanner in=new Scanner(System.in); int number, digit1, digit2, digit3, digit4, newnum,a,b; number=in.nextInt(); digit1=number/1000; digit2=(number-1000*digit1)/100; digit3=(number-1000*digit1-digit
8、2*100)/10; digit4=number-1000*digit1-100*digit2-10*digit3; digit1=(digit1+9)%10; digit2=(digit2+9)%10; digit3=(digit3+9)%10; digit4=(digit4+9)%10; a=digit1; digit1=digit3; digit3=a; b=digit2; digit2=digit4; digit4=b; newnum=1000*digit1+100*digit2+10*digit3+digit4; /*-*/ System.out.println(The encryp
9、ted number is +newnum); /*程序填空,不要改变与输入输出有关的语句。输入一个大写英文字母,输出相应的小写字母。例:输入G输出g*/import java.io.*;public class Test20006 public static void main(String args) throws IOException char ch; ch=(char)System.in.read(); ch+=32; /*-*/ System.out.println(ch); /*已知某位学生的数学、英语和计算机课程的成绩分别是87分、72分和93分,求该生3门课程的平均分。本题无
10、输入。输出示例:math = 87, eng = 72, comp = 93average = 84*/public class Test20007 public static void main(String args) int math, eng, comp, average; math=87;eng=72;comp=93; average=(math+eng+comp)/3; /*/ System.out.println(math = +math+, eng = +eng+, comp = +comp); System.out.println(average = + average);
11、/*输入存款金额 money、存期 year 和年利率 rate,根据下列公式计算存款到期时的利息 interest(税前),输出时保留2位小数。interest = money(1+rate)year - money输入输出示例:括号内为说明输入1000 3 0.025 (money = 1000, year = 3, rate = 0.025)输出interest = 76.89*/import java.util.Scanner;public class Test20008 public static void main(String args) int money, year; dou
12、ble interest, rate; Scanner in=new Scanner(System.in); money=in.nextInt(); year=in.nextInt(); rate=in.nextDouble(); interest = money*(1+rate)- money; /*使用in.nextDouble()和in.nextInt()输入double和int型数据*/ /*/ System.out.println(interest = + (int)(interest*100+0.5)/100.0); /*输入一个学生的数学成绩,如果它低于60,输出“Fail”,否
13、则,输出“Pass”。输出使用System.out.println(Pass);及System.out.println(Fail);例:括号内是说明输入2 (repeat=2) 60 59输出PassFail*/import java.util.Scanner;public class Test30001 public static void main(String args) int ri, repeat; int mark; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri=repeat; ri+) m
14、ark=in.nextInt(); if(mark60)System.out.println(Fail); else System.out.println(Pass); /*-*/ /*输入四个整数,输出其中的最小值。例:括号内是说明输入3 (repeat=3) 12 6 1 9010 40 30 20-1 -3 -4 -5输出min is 1 (12 6 1 90 中的最小值是1)min is 10 (10 40 30 20 中的最小值是10)min is -5 (-1 -3 -4 -5中的最小值是-5)*/import java.util.Scanner;public class Test
15、30002 public static void main(String args) int ri, repeat; int a, b, c, d, min; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri=repeat; ri+) a=in.nextInt(); b=in.nextInt(); c=in.nextInt(); d=in.nextInt(); min=a; if(ba)min=b; if(cb)min=c; if(dc)min=d; /*-*/ System.out.println(min
16、 is +min); /*输入三角形的三条边a, b, c,如果能构成一个三角形,输出面积area和周长perimeter;否则,输出“These sides do not correspond to a valid triangle”。在一个三角形中,任意两边之和大于第三边。三角形的面积计算公式:aere*area = s(s-a)(s-b)(s-c)其中:s = (a+b+c)/2输出代码:System.out.println(area=+area+;perimeter=+perimeter);System.out.println(These sides do not correspond
17、 to a valid triangle); 例:括号内是说明输入2 (repeat=2) 5 5 31 1 4输出area=7.154544;perimeter=13.0These sides do not correspond to a valid triangle*/import java.util.Scanner;public class Test30003 public static void main(String args) int ri, repeat; float a, b, c, area, perimeter, s; Scanner in=new Scanner(Syst
18、em.in); repeat=in.nextInt(); for(ri=1; ric&a+cb&b+ca) s = (a+b+c)/2; area = (float)(Math.sqrt(s*(s-a)*(s-b)*(s-c); perimeter=a+b+c; System.out.println(area=+area+;perimeter=+perimeter); else System.out.println(These sides do not correspond to a valid triangle); /*-*/ /*输入整数x,若x大于0,y=1;若x等于0,y=0;否则,y
19、=-1,最后输出y。例:括号内是说明输入3 (repeat=3) 2 -8 0输出1 (x=2时y=1)-1 (x=-8时y=-1)0 (x=0时y=0)*/import java.util.Scanner;public class Test30004 public static void main(String args) int ri, repeat; int x, y; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri0)y=1; elseif(x=0)y=0;else y=-1; /*-*/ Sys
20、tem.out.println(y); /*输入一个职工的月薪salary,输出应交的个人所得税tax。tax = rate * (salary-850)当 salary = 850 时,rate = 0%;当 850 salary = 1350 时,rate = 5%;当 1350 salary = 2850 时,rate = 10%;当 2850 salary = 5850 时,rate = 15%;当 5850 salary 时,rate = 20%;例:括号内是说明输入5 (repeat=5)1010.8732098.7680040102850输出tax=8.04tax=6249.75
21、tax=0.0tax=474.0tax=200.0*/import java.util.Scanner;public class Test30005 public static void main(String args) int ri, repeat; float rate, salary, tax; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri=repeat; ri+) salary=in.nextFloat(); if(salary=850) rate=0; else if (salary=135
22、0) rate=0.05f; else if (salary=2850) rate=0.1f; else if (salary=5850) rate=0.15f; else rate=0.2f; tax = rate * (salary-850); /*-*/ System.out.println(tax=+(int)(tax*100+0.5)/100.0); /*以下4种水果的单价分别是3.00元/公斤,2.50元/公斤,4.10元/公斤,10.20元/公斤。1 apples2 pears3 oranges4 grapes输入水果的编号,输出该水果的单价。如果输入不正确的编号,显示单价为0。
23、例:括号内是说明输入1 (repeat=1) 3 (oranges的编号) 输出1 apples2 pears3 oranges4 grapesprice=4.1*/import java.util.Scanner;public class Test30006 public static void main(String args) int ri, repeat; int choice; float price=0; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri=A&ch=a&ch=z)ch-=32;
24、System.out.print(ch); ch=(char)System.in.read(); /*读入1 个正整数 n(n=100),计算并输出11/21/31/n 。例:括号内是说明输入2 (repeat=1)210输出1.52.9289684*/import java.util.Scanner;public class Test40001 public static void main(String args) int ri, repeat; int i, n; float sum=0; Scanner in=new Scanner(System.in); repeat=in.next
25、Int(); for(ri=1; ri=repeat; ri+) n=in.nextInt(); sum=0; for(i=1;i=n;i+) sum+=1.0f/i; /*-*/ System.out.println(sum); /*读入1 个正整数 n(n=50),计算并输出n! 。例:括号内是说明输入3 (repeat=2)1 105输出1.03628800.0120.0*/import java.util.Scanner;public class Test40002 public static void main(String args) int ri, repeat; int i, n; double fact; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri=repeat; ri+) n=in.nextInt(); fact=1; for(i=1;i=n;i+) fact*=i; /*-*/ System.out.println(fact); /*读入1 个正实数x和1个正整数 n(n=50),计算并输出x的n次幂。例:括号内是说明输入2 (repe
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1