JAVA語言程序設計(基礎篇) 第十版——第五章 循環 (參考答案)

(5.2~5.7節)+(5.8~5.10節)

(綜合題的答案還沒有寫)

*5.1(統計正數和負數的個數然後計算這些數的平均值)

import java.util.Scanner;

public class F1 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter an integer, the input ends if it is 0:");
		int number=input.nextInt();
		
		int count=0;
		int zcount=0;
		int fcount=0;
		double sum=0;
		double average=0;
		
		if(number!=0) {
			
		while(number!=0) {
			count++;
			if(number>0) {
				zcount++;
			}
			else {
				fcount++;
			}
			
			sum+=number;
			average=sum/count;
			number=input.nextInt();
		}
		
		
		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);
		
		}
		else if(number==0) {
			System.out.println("No numbers are entered except 0");
		}
		
		input.close();

		
	}

}

5.2(重複加法)

import java.util.Scanner;

public class F2 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		long startTime=System.currentTimeMillis();
		int rightCount=0;
		String output="";
		
		int count=0;
		while(count<10) {
			
		int random1=(int) (1+Math.random()*15);
		int random2=(int) (1+Math.random()*15);
		
		System.out.print("請問:"+random1+"+"+random2+" = ? ");
		int answer=input.nextInt();
		
		if(answer==(random1+random2)) {
			System.out.println("You are right.");
			rightCount++;
		}
		else {
			System.out.println("You are wrong!"+" answer shoule be "+(random1+random2));
		}
		
		count++;
		
		output+="\n"+random1+" + "+random2+" = "+answer+" "
		+((answer==random1+random2)?"correct":"wrong");
		
		}
		long endTime=System.currentTimeMillis();
		long textTime=endTime-startTime;
		System.out.println(output);
		System.out.println("正確答案個數:"+rightCount);
		System.out.println("測驗時間爲:"+textTime/1000+" seconds");
		
		input.close();
	}

}

5.3(將千克轉換成磅)

public class F3 {

	public static void main(String[] args) {
		System.out.println("千克                           磅");
	
		for(int i=1; i<=199; i++) {
			System.out.printf("%-3d              %5.1f \n", i, 2.2*i);
		}
		
		

	}

}

5.4(將英里轉換爲千米)

public class F4 {

	public static void main(String[] args) {
		System.out.println("英里                    千米");
		for(int i=1; i<=10; i++) {
			System.out.printf("%-2d          %-6.3f \n", i, i*1.609 );
		}
	}

}

5.5(千克與磅之間的互換)

小夥伴肯定一開始也和我一樣想到用嵌套循環,可是後面一運行,才恍然大悟。

這裏不需要用嵌套就可以解決,“小朋友,你是否有很多問號?”,哈哈哈哈 ^_^

public class F5 {

	public static void main(String[] args) {
		System.out.println("千克                       磅                      磅                            千克");
		
		 
		for(int i=1, j=20; i<=199 && j<=515; i+=2,j+=5) {
			System.out.printf("%-3d          %5.1f          %-3d            %6.2f \n", i, i*2.2, j, j/2.2);
		}
		
		
	}

}

5.6(英里與千米之間的的互換)

public class F6 {

	public static void main(String[] args) {
		
		System.out.println("英里                    千米                      千米                     英里");
		
		for(int i=1, j=20; i<=10 && j<=65; i++, j+=5) {
			System.out.printf("%-2d           %-6.3f           %-2d           %6.3f \n", i, i*1.609, j, j/1.609);  
		}
		
		
		

	}

}

 

**5.7(財務應用程序:計算將來的學費)

public class F7 {

	public static void main(String[] args) {
		
		double tuitionFee=10000;//表示當前學費
		double afterFee = 0;
		double sum=10000;
		
		for(int i=1; i<=10; i++) {
			
			 afterFee=1.05*tuitionFee;//每年後的學費
			 sum=sum+afterFee;
			 
			 tuitionFee=afterFee;
			
		}
		
		System.out.printf("十年後的學費爲: % .2f美元 \n", afterFee);
		
		double total=afterFee;
		double oldfee =afterFee ;
		double newFee;
		//計算從十年後那一年算起,4年內的總學費
		for(int i=1; i<=3; i++) {
			 newFee=1.05*oldfee;//每年後的學費
			 total=total+newFee;
			 
			 oldfee=newFee;
			
		}
		
		System.out.printf("從十年後的那一年算起,4年內的學費爲: % .2f美元", total);


	}

}

 

5.8(找出最高分)

這裏我提前用後面章節的數組來存儲名字和分數。

看我下面代碼的這兩行:

System.out.print("請輸入學生名字:");
            name[i]=input.next();

若是把  name[i]=input.next();   改寫成  name[i]=input.nextLine(); 會出錯,注意一下。

import java.util.Scanner;
//提前用數組來做
public class F8 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("請輸入學生的個數:");
		int number=input.nextInt();
		
		double[] score=new double[number];
		String[] name=new String[number];
		
		double max=0;
		String maxName="";
		
		for(int i=0; i<number; i++) {
			System.out.print("請輸入學生名字:");
			name[i]=input.next();
			System.out.print("請輸入學生分數:");
			score[i]=input.nextDouble();
			
			if(score[i]>max) {
				max=score[i];
				maxName=name[i];
			}
			
		}
		
		System.out.println("最高分的學生的名字是:"+maxName+" "+max);
		
		input.close();
	}

}

 

*5.9(找出兩個分數最高的學生)

import java.util.Scanner;

public class F9 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("請輸入學生個數:");
		int number = input.nextInt();
				
		String name[] = new String[number];
		int score[] = new int[number];
				
		int max= 0, maxIndex = 0;
		int second = 0, secondIndex = 0;
		 
		Scanner input1 = new Scanner(System.in);
		Scanner input2 = new Scanner(System.in);
				
		for (int i = 0; i < number; i++) {
			System.out.print("請輸入第" + (i + 1) + "個學生的姓名:");
			name[i] = input1.nextLine();
			System.out.print("請輸入第" + (i + 1) + "個學生的成績:");
			score[i] = input2.nextInt();
		 
			if (score[i] > max && score[i] > second) {
					max = score[i];
					second = max;
			}
			else if (score[i] < max && score[i] > second) {
					second = score[i];			
								
				}
			}
				
		
			for (int i = 0; i < number; i++) {
				if (score[i] == max) {
					maxIndex = i;
			} 
			else if (score[i] == second) {
					secondIndex = i;
			  }
		    } 
						
		System.out.println("成績最高的學生是:" + name[maxIndex] + ",對應的分數是:" + score[maxIndex]);
		System.out.println("成績第二的學生是:" + name[secondIndex] + ",對應的分數是:" + score[secondIndex]); 
			
			
	}
			
			
}

5.10(找出能被5和6整除的數)

public class F10 {

	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) {
					System.out.println(i);
				}
				else {
					System.out.print( i+" ");
				}
				
				
			}
		}
		
		
		
		
     }
	
	}

 

5.11(找出能被5或6整除,但不能被兩者同時整除的數)

public class F11 {

	public static void main(String[] args) {
		
		int count=0;
		
		for(int i=100; i<=200; i++) {
			
			if((i%5==0 || i%6==0) && (i%5==0 ^ i%6==0)) {
				count++;
				if(count%10==0) {
					System.out.println(i);
				}
				else {
					System.out.print( i+" ");
				}
				
				
			}
		}
		
		
		
		
	}

}

5.12(求滿足n²>12 000的 n 的最小值) 

public class F12 {

	public static void main(String[] args) {
		
		int n=0;
		
		while(n*n <= 12000) {
			
			n++;
			
		}
		
		System.out.println("最小整數是: " + n);
		
		

	}

}

 5.13(求滿足 n³<12000的 n 的最大值)

public class F13 {

	public static void main(String[] args) {
		
		int n=0;
		while(n*n*n < 12000) {
			n++;
		}
		
		System.out.println("最大整數是: " + n);

	}

}

(5.8~5.10節)

*5.14(計算最大公約數)

import java.util.Scanner;

public class F14 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("請輸入兩個正整數: ");
		int n1=input.nextInt();
		int n2=input.nextInt();
		int gcd=-1;
		int d=0;
		
		if(n1>n2) {
			d=n2;
		}
		else if(n1<n2) {
			d=n1;
		}
		
		while(d>=1 && gcd==-1) {
			if(n1%d==0 && n2%d==0) {
				gcd=d;
			}
			d--;
		}
		
		System.out.println("最大公約數爲: " + gcd);
		

	}

}

*5.15(顯示ASCII碼字符表)

public class F15 {

	public static void main(String[] args) {
		
		int count=0;
		for(int i=33; i<=126; i++) {
			count++;
			
			char ch=(char)(i+'\u0000');
			
			if(count%10==0) {
				System.out.println(ch);
			}
			else {
				System.out.print(ch + " ");
			}
			
		
		}
		
	}

}

*5.16(找出一個整數的因子)

這道題我想了很久,只能說做題不易,且做且珍惜┭┮﹏┭┮

第一步:好好想一想要用哪一種循環來做題(這裏我用while循環)

建議:已經知道循環次數的用for循環;

              不知道循環次數的用while循環;

              如果要在檢驗循環繼續條件前需要執行一次循環體的用do-while循環)。

第二步:自行分析循環繼續條件;

第三步:分析循環體;

第四步:添加循環控制變量;

package p5;

import java.util.Scanner;

public class F16 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("請輸入一個整數: ");
		
		int number=input.nextInt();
		int i=2;
		
		System.out.print("因子爲: ");
		
		while(number != 1) {
			if(number % i ==0) {
				number = number / i ;
				System.out.print( i + " ");//輸出因子
			}
			else {
				i++;
			}
		}
		

	}

}

 

 **5.17(顯示金字塔)

這玩意兒首先你得琢磨一下,是不是想到了字符串的連接,還有循環。你品,您仔細品。

金字塔代碼:

import java.util.Scanner;

public class F17 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("請輸入一個整數: ");
		
		int n=input.nextInt();
		input.close();
		String s="1";
		String s1 = " ";
		
		System.out.printf("%30s\n",s);
		
		for(int i=2; i<=n; i++) {
			s=i+" "+s;
			s1=s1+i+" ";
			
			System.out.printf("%30s%-30s\n",s,s1);
		}
	}
}

**5.18(使用循環語句打印4個圖案)

可參考一個大神的博客:

https://blog.csdn.net/xjlovewjh/article/details/104312690

她用的都是嵌套的循環來寫的,我的和她不一樣,不過我感覺她的多標準一些。(我用的不是嵌套循環)

圖案一:

圖案一代碼:

import java.util.Scanner;

public class F17 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("請輸入一個整數: ");
		
		int n=input.nextInt();
		String s=" ";
		
		for(int i=1; i<=n; i++) {
			 s=i+" "+s;
			
			System.out.printf("%-20s \n", s);
		}
		

	}

}

 

圖案二:

 

 圖形二代碼:

public class F18 {

	public static void main(String[] args) {
		
		for(int i=1; i<=6; i++) {
			 for(int j=1; j<=i; j++) {
				 System.out.printf("%d ",j);
			 }
			 System.out.println();
		 }
	}
}

 

 

圖案三:

圖案三代碼:

import java.util.Scanner;

public class F17 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("請輸入一個整數: ");
		
		int n=input.nextInt();
		String s=" ";
		
		for(int i=1; i<=n; i++) {
			 s=i+" "+s;
			
			System.out.printf("%20s \n", s);
		}
		

	}

}

 圖案四:

public class F18 {

	public static void main(String[] args) {
		
		for(int line = 6;line >= 1;line--)
		{
			for(int i = 1;i <= 2 *(6-line);i++)
				System.out.print(" ");
			for(int i = 1;i <= line;i++)
				System.out.printf("%d ",i);
			System.out.print("\n");
		}
	}
}

**5.19(打印金字塔形的數字)

我不會寫這題,都是你們可以參照一下某大神的博客:

https://blog.csdn.net/xjlovewjh/article/details/104312717(這裏有代碼)

 

我亂打碼得到另一種:(不過不是你們想要的答案)

 亂碼踩死人:

public class F19 {

	public static void main(String[] args) {
		String s=" 1";
		String s1=" ";
		System.out.printf("%30s\n", s);
		
		for(int i=1; i<=7; i++) {
		s=((int)Math.pow(2,i))+" "+s;
		s1=s1+" "+((int)Math.pow(2,i));
		System.out.printf("%30s%-30s \n", s, s1);
		}

	}

}

 *5.20(打印2到1000之間的素數)

不會,略

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章