降央卓玛演唱:我的九寨:Java编程高分求助!

来源:百度文库 编辑:高考问答 时间:2024/04/28 02:12:11
谁能帮我编写一个JAVA应用程序,程序运行后,根据屏幕提示输入一个正整数,然后求1到该整数的和、偶数和并输出!!紧急!!拜托各位高手了!!

文件名` Tmep.java:

public class Temp {

/**
* @param args void
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader keyBR = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入正数: ");
int num = Integer.parseInt(keyBR.readLine());
int total = 0;
int evenTotal = 0;
for(int i=1; i<=num; i++){//从1开始循环
total+=i;
if(i%2==0)evenTotal+=i;//偶数 (取摸2等于0的数)
}
System.out.println("1到该整数的和:" + total);
System.out.println("1到该整数的偶数和:" + evenTotal);
}

}

import java.util.*;
import java.io.*;
public class Temp {

/**
* @param args void
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader keyBR = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入正数: ");
int num = Integer.parseInt(keyBR.readLine());
int total = 0;
int evenTotal = 0;
for(int i=1; i<=num; i++){//从1开始循环
total+=i;
if(i%2==0)evenTotal+=i;//偶数 (取摸2等于0的数)
}
System.out.println("1到该整数的和:" + total);
System.out.println("1到该整数的偶数和:" + evenTotal);
}

}
楼上这位仁兄的程序是完全正确的,美中不足是缺少了包.我给加上拉.

现在可以运行拉.你试试吧.