编写一个java程序,求出〔2,100〕的所有奇数和,并输出所有奇数,最后输出其和。 50
4个回答
2008-12-09
展开全部
//程序不能写吧,这个实测通过,
public class T {
public static void main(String[] args){
int a=2;//下限
int b=100;//上限不能小于下限
printOdd(a,b);
}
private static void printOdd(int a,int b){
int sum=0;//奇数和
int amount=0;//奇数个数
for(int i=a;i<=b; i++){
if(i%2==1){//判断奇数
System.out.print(i+ " ");
sum+=i;
++amount;
if(amount%10==0)System.out.println();//每打印10个就换行
}
}
System.out.println("\n"+a+"到"+b+"中奇数个数为:"+amount+"个,所有奇数和为:"+sum);
}
}
public class T {
public static void main(String[] args){
int a=2;//下限
int b=100;//上限不能小于下限
printOdd(a,b);
}
private static void printOdd(int a,int b){
int sum=0;//奇数和
int amount=0;//奇数个数
for(int i=a;i<=b; i++){
if(i%2==1){//判断奇数
System.out.print(i+ " ");
sum+=i;
++amount;
if(amount%10==0)System.out.println();//每打印10个就换行
}
}
System.out.println("\n"+a+"到"+b+"中奇数个数为:"+amount+"个,所有奇数和为:"+sum);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
简单形式:
====================================================
public class test{
public static void main(String[] args){
int sum=0;
for(int i=3;i<100;i+=2) {System.out.print(i+" ");sum+=i;}
System.out.println("\nThe sum of odd numbers between 2 and 100 is: " + sum);
}
}
通用函数形式:
====================================================
public class test{
public static void main(String[] args){
lo(2,100);
System.out.println("The sum of odd numbers between 2 and 100 is: "+son(2,100));
}
static void lo(int a,int b){
if(a>b){a=a^b;b=a^b;a=a^b;}
int s=a+(a%2==0?1:2);
int e=b-(b%2==0?1:2);
for(int i=s;i<e+1;i+=2) System.out.print(i+" ");
System.out.println();
}
static int son(int a,int b){
if(a>b){a=a^b;b=a^b;a=a^b;}
int s=a+(a%2==0?1:2);
int e=b-(b%2==0?1:2);
return s>e?0:(s==e?s:(s+e)*(e+2-s)/4);
}
}
====================================================
public class test{
public static void main(String[] args){
int sum=0;
for(int i=3;i<100;i+=2) {System.out.print(i+" ");sum+=i;}
System.out.println("\nThe sum of odd numbers between 2 and 100 is: " + sum);
}
}
通用函数形式:
====================================================
public class test{
public static void main(String[] args){
lo(2,100);
System.out.println("The sum of odd numbers between 2 and 100 is: "+son(2,100));
}
static void lo(int a,int b){
if(a>b){a=a^b;b=a^b;a=a^b;}
int s=a+(a%2==0?1:2);
int e=b-(b%2==0?1:2);
for(int i=s;i<e+1;i+=2) System.out.print(i+" ");
System.out.println();
}
static int son(int a,int b){
if(a>b){a=a^b;b=a^b;a=a^b;}
int s=a+(a%2==0?1:2);
int e=b-(b%2==0?1:2);
return s>e?0:(s==e?s:(s+e)*(e+2-s)/4);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Test {
/**
* 计算整数x、y之间(不包含)的奇数和
* @param x
* @param y
*/
public static void getOddSum(int x, int y) {
int start = (x+1)%2==0?x+2:x+1;
int sum = 0;
for(; start<y; start+=2) {
System.out.print(start + " ");
sum += start;
}
System.out.println();
System.out.println(sum);
}
public static void main(String[] args) {
Test.getOddSum(2, 100);
}
}
/**
* 计算整数x、y之间(不包含)的奇数和
* @param x
* @param y
*/
public static void getOddSum(int x, int y) {
int start = (x+1)%2==0?x+2:x+1;
int sum = 0;
for(; start<y; start+=2) {
System.out.print(start + " ");
sum += start;
}
System.out.println();
System.out.println(sum);
}
public static void main(String[] args) {
Test.getOddSum(2, 100);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class test
{
public static void main(String[] args)
{
int max=0;
for(int i=3;i<100;i+=2)
{
System.out.println(i);
max+=i;
}
System.out.println(max);
}
}
{
public static void main(String[] args)
{
int max=0;
for(int i=3;i<100;i+=2)
{
System.out.println(i);
max+=i;
}
System.out.println(max);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询