赞
踩
将三个习题的代码整合在一起了,exercise1对应1_1,以此类推。
大家一起来学习JAVA啊!!!
- import java.util.Scanner;
- import java.awt.event.KeyEvent;
- public class main
- {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int m;
- m = sc.nextInt();
- switch (m) {
- case 1:
- exercise1(sc);
- break;
- case 2:
- exercise2(sc);
- break;
- case 3:
- exercise3(sc);
- }
- }
- public static void exercise1(Scanner sc)
- {
- int a= sc.nextInt();
- int b= sc.nextInt();
- int c= sc.nextInt();
- double D;
- D=(a+b+c)/3.00;
- System.out.printf("%.3f",D);//%.3f这个点在3f之前
- }
- public static void exercise2(Scanner sc)
- {
- double c,f;
- while(!sc.equals(Character.toString((char) KeyEvent.VK_ESCAPE)))
- {
- if (sc.hasNextDouble())//检测下一个输入是否是数字
- {
- f= sc.nextDouble(); //这里的scanner是小写的30
- c=5*(f-32)/9;
- System.out.printf("%.3f",c);
- }
- else
- {
- System.out.println("输入异常");
- sc.next();//不要这个的话会一直出现输入异常,因为a一直在输入缓冲区,我们需要将其读取并跳过。
- }
- }
- }
- public static void exercise3(Scanner sc)
- {
- int n=sc.nextInt();
- int sum1=0;
- int sum2=0;
- for (int i=1;i<=n;i++)
- {
- sum1=sum1+i;
- }
- sum2=(n*(n+1))/2;
- System.out.println(sum1+" "+sum2);
- }
-
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。