标签:
1、通过键盘输入一个整数n,编程计算1+2+3+...+n的值。
2、通过键盘输入任意6个整数,输出最大数和最小数。
3、编写程序计算12-22+32-42+...+972-982+992-1002的值。
</pre><pre name="code" class="java">1、import java.util.Scanner; public class test1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n,i,sum=0; n=sc.nextInt(); for(i=1;i<=n;i++) sum+=i; System.out.println(sum); } }2、
import java.util.Arrays; import java.util.Scanner; public class test2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a[]=new int[6]; int i; for(i=0;i<6;i++) a[i]=sc.nextInt(); Arrays.sort(a); System.out.println(a[0]); System.out.println(a[5]); } }
public class test3 { public static void main(String[] args) { int i=12,j=22,n,sum=0; for(n=1;n<=98;n++) { sum+=i-j; i+=10; j+=10; } System.out.println(sum); } }
class student{ int id; int age; String last_name; String first_name; void initstudent(String l,String f){ last_name=l; first_name=f; } String get(){ return first_name+last_name; } } public class test4 { public static void main(String[] args) { student s=new student(); s.initstudent("lin","yu"); System.out.println(s.get()); } }
class Cylinder{ double r; double height; void initCylinder(double l,double h){ r=l; height=h; } double getArea(){ return Math.PI*r*r; } double getVolume(){ return Math.PI*r*r*height; } } public class test5 { public static void main(String[] args) { Cylinder c=new Cylinder(); c.initCylinder(8.6, 5.5); System.out.println(c.getArea()); System.out.println(c.getVolume()); } }
class Person{ int id; String name; String sleep(){ return "睡觉时流56口水"; } String eat(){ return "宝宝吃牛奶"; } } public class test6 { public static void main(String[] args) { Person p=new Person(); System.out.println(p.eat()); System.out.println(p.sleep()); } }
标签:
原文地址:http://blog.csdn.net/qq_24928451/article/details/45216827