java if第三章课后作业_java第三章课后习题答案.doc

java第三章课后习题答案

3-1

import java.util.*;

public class XIN31{

public static void main(String args[]){

double sum;

Scanner reader=new Scanner(System.in);

System.out.println("请输入一个值:");

double x=reader.nextDouble();

if(x<=0)

{

sum=2*x-1;

System.out.println("y="+sum);

}

else

{

sum=3*x-1;

}

System.out.println("y="+sum);

}

}

3-2

public class XIN32{

public static void main(String args[]){

int sum=0,j;

int i=1;

while(i<=1000)

{

j=i%(7*3);

if(j==0)

{

sum=sum+i;

i++;

}

else

i++;

}

System.out.println("数值="+sum);

}

}

3-3

import java.util.*;

public class XIN33

{

public static void main(String args[])

{

int i = 0, a = 8, sum = 8;

for (i = 1; i <= 9; i++)

{

a = a * 10 + 8;

sum = sum + a;

}

System.out.println("数值=" + sum);

}

}

3-4

public class XIN34{

public static void main(String args[]){

int sign=-1;

double deno=1.0,term,sum=0.0;

while(deno<=19999)

{

sign=-sign;

term=sign/deno;

sum=term+sum;

deno=deno+2;

}

System.out.println("结果为="+sum);

}

}

3-5

import java.util.*;

public class XIN35

{

public static void main(String args[])

{

int i = 0, sum = 0, sum1 = 0, sum2 = 0;

int j = 0, a, b, c;

for (i = 0; i <= 199; i++)

{

a = i * (i - 1);

a = a * (i + 1);

sum1 = sum + a;

}

for (j = 0; j <= 99; j++)

{

b = j * (j - 1);

b = b * (j + 1);

sum2 = sum + b;

}

c = sum1 - sum2;

System.out.println("结果为" + c);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值