4.1
package test1;
import java.util.Scanner;
// 第八版 第四章 课后编程题的答案 4.1
public class Test1 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter an int value , the program exits if the input is 0:" );
int zcount=0;
int fcount=0;
double sum=0 ;
double average=0;
int number=1;
while(number!=0){
number=input.nextInt();
if(number>0) {
zcount++;
}
else if(number<0) {
fcount++;
}
sum=sum+number;
}
average=sum/(zcount+fcount);
System.out.println("The number of positives is "+zcount);
System.out.println("The number of negatives is "+fcount);
System.out.println("The total is "+sum);
System.out.println("The average is "+average);
}
}
4.2
package test1;
import java.util.Scanner;
public class Test2 {
//4.2(重复加法)
public static void main(String[] args) {
long startTime=System.currentTimeMillis();
int zcount=0;
int count=0;
Scanner input=new Scanner(System.in);
while(count<10) {
int number1=(int)(Math.random()*14+1);
int number2=(int)(Math.random()*14+1);
System.out.println("请问: "+number1+" + "+number2+" = ?");
int answer=input.nextInt();
if(number1+number2==answer) {
zcount++;
System.out.println("你是正确的!");
}else {
System.out.println("你是错误的!");
}
count++;
}
long endTime=System.currentTimeMillis();
long testTime=endTime-startTime;
System.out.println("正确的答案的个数为: "+zcount);
System.out.println("当前的时间为: "+testTime);
}
}
4.3
package test1;
public class Test3 {
//4.3
public static void main(String[] args) {
System.out.println("千克"+" "+"磅");
for(int i=1;i<=199;i=i+2) {
double pound=i*2.2;
//System.out.println(i);
System.out.printf("%-4d%9s%4.1f\n",i," ",pound);
}
}
}
4.4
package test1;
public class Test4 {
//4.4
public static void main(String[] args) {
System.out.println("英里"+" "+"千米");
for(int i=1;i<=10;i++) {
double km=i*1.609;
System.out.printf("%-2d%16s%-6.3f\n", i," ",km);
}
}
}
4.5
package test1;
public class Test5 {
//4.5 "\t"是制表符
public static void main(String[] args){
double b1,kg2;
int b2=20;
System.out.println("千克\t\t磅\t\t千克\t\t磅");
for(int i=1;i<=199;i=i+2) {
b1=i*2.2;
kg2=b2/2.2;
System.out.printf("%d\t\t%.1f\t\t%d\t\t%.2f\n", i,b1,b2,kg2);
b2+=5;
}
}
}
4.6
package test1;
public class Test6 {
//4.6
public static void main(String[] args) {
double km1,yl2;
int km2=20;
System.out.println("英里\t\t千米\t\t千米\t\t英里");
for(int i=1;i<=10;i++) {
km1=i*1.609;
yl2=km2/1.609;
System.out.printf("%d\t\t%.3f\t\t%d\t\t%.3f\n",i,km1,km2,yl2);
km2=km2+5;
}
}
}
4.7
package test1;
public class Test7 {
//4.7
public static void main(String[] args) {
int year;
double xf=10000;
double sum=0;
for(year=1;year<=10;year++) {
xf=xf*1.05;
if(year<=4)
sum=sum+xf;
}
System.out.println("十年后的学费为: "+xf);
System.out.println("从十年后算起,4年内的学费为: "+sum);
}
}
4.8和4.9暂时不会!!!
4.10
public class Test10 {
//4.10
public static void main(String[] args) {
int count=0;
for(int i=100;i<=1000;i++) {
if(i%5==0 && i%6==0) {
count++;
if(count%10==0) //每行显示10个
System.out.print(i+"\n");
else
System.out.print(i+" ");
}
}
}
}
4.11
public class Test11 {
//4.11
public static void main(String[] args) {
int count=0;
for(int i=100;i<=200;i++) {
if(i%5==0 ^ i%6==0) {
count++;
if(count%10==0)
System.out.print(i+"\n");
else
System.out.print(i+" ");
}
}
}
}
4.12
public class Test12 {
//4.12
public static void main(String[] args) {
int n = 0;
int count=0;
while(n<12000) {
if(n*n>12000) {
count++;
if(count==1)
System.out.print(n);
}
n++;
}
}
}
4.13
public class Test13 {
//4.13
public static void main(String[] args) {
int n=0;
while(n<12000) {
if(n*n*n<12000) {
n++;
}else {
n--;
System.out.println(n);
break;
}
}
}
}
4.14
public class Test14 {
//4.14
public static void main(String[] args) {
char i;
int count=0;
for(i=33;i<=126;i++) {
count++;
if( count % 10 == 0)
System.out.print(i+"\n");
else
System.out.print(i+" ");
}
}
}
4.15
package test1;
import java.util.Scanner;
public class Test15 {
//4.15
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入两个正整数: ");
int n1=input.nextInt();
int n2=input.nextInt();
int d = 0;
if(n1
else if(n2
while(d>=1) {
if(n1%d==0 && n2%d==0) {
System.out.println(n1+" 和 "+n2+" 的最大公约数是:"+d);
break;
}
d--;
}
}
}
4.16
package test1;
import java.util.Scanner;
public class Test16 {
//4.16
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一个整数: ");
int n=input.nextInt();
int i;
int k=2;
for(i=1;i<10000;i++) { //要注意循环可执行的次数,一定要大于n
if(n%k==0) {
System.out.print(" "+k);
n=n/k;
k=2;
}else {
k++;
}
}
}
}
4.17
package test1;
import java.util.Scanner;
public class Test17 {
//4.17 金字塔系列(这是我复制别人的源码,本人不会写系列)
public static void main(String[] args) {
//画各种金字塔的程序
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer between 1 and 15 :");
int n = input.nextInt();
//左金字塔
/* int count = 1;
for(int i = 1;i <=n;i++){
for(int j = n;j>=1;j--){
if(count <= n-i){
System.out.print(" ");
count++;
}
else{
System.out.printf("%2d",j);
System.out.print(" ");
}
}
count = 1;
System.out.println();
}
//右金字塔
for(int i = 1;i <= n;i++){
for(int j = 1;j <= i;j++){
System.out.printf("%2d",j);
System.out.print(" ");
}
System.out.println();
}*/
//整个金字塔
int countFront = 1;
for(int i = 1;i <=n;i++){
int out = i;
for(int j = 1;j <= 2*n-1;j++){
if(countFront <= n-i || countFront >=n+i ){
System.out.print(" ");
}
else{
if(j <=n){
System.out.printf("%2d",out);
System.out.print(" ");
out--;
}
if(j > n){
System.out.printf("%2d",out+2);
System.out.print(" ");
out++;
}
}
countFront++;
}
countFront = 1;
System.out.println();
}
//右金字塔翻版倒影
/* for(int i = n;i >= 1;i--){
for(int j = 1;j <= i;j++){
System.out.printf("%2d",j);
System.out.print(" ");
}
System.out.println();
}
//斜对角数字一样的左金字塔
for(int i = n;i >= 1;i--){
int cnt = 1;
for(int j = 1;j <= n;j++){
if(cnt <= n-i){
System.out.print(" ");
cnt++;
}
else{
System.out.printf("%2d",j-n+i);
System.out.print(" ");
}
}
System.out.println();
}*/
}
}
/*
* 这里用到了数组!!!
* System.out.println("请输入一个1~15之间的整数:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] array = new int[n][2*n+1];
for (int i = 0; i < n; i++) {
for (int j = n-i; j <= n + i; j++){
array[i][j]=Math.abs(n-j)+1;
}
}
for(int i=0;i
System.out.print(array[i][j]==0?" ":array[i][j]+" ");
}
System.out.println();
} */