import java.util.*;//导入工具包
public class textXD1{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//创建扫描仪
System.out.println("本程序用于计算1~N之间的奇数和");
int A = 1;
int N = 0;
while(A == 1){
System.out.println("请输入N的值:");
N = sc.nextInt();
System.out.println("您输入的N的值为:"+N);
System.out.println("确认请输入“1”,修改请输入“0”。");
int userQueren = sc.nextInt();
if(userQueren == 0 ){
A = 1;
}else{
A = 0;
}
}
System.out.println("已确认N值为:"+N);
int C = 1; //初始化语句
int sum = 0; //存钱罐
while (C <= N){
if(C % 2 != 0){
sum += C;
}
C++;
}
System.out.println("1~"+N+"之间的奇数和为:"+sum);
}
} |
|