2013年2月8日 星期五

TQC+ JAVA 迴圈 301 ~ 310

TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK

/* TQC+ JAVA6 - 301 */
import java.util.*;
class JPA301 {
public static void main(String argv[]) {
System.out.println("Input:");
int tm =new Scanner(System.in).nextInt();
int sum = 0 ;
//從1開始加,且變數會一直+1再加入總和,直到變數大於使用者輸入的數便停止
for (int a=1;a<=tm ; a++) {
sum = sum+a;
}
System.out.printf("1 + ... + %d = %d",tm,sum);
}
}
/* TQC+ JAVA6 - 302 */
import java.util.Scanner;
public class JPA302 {
public static void main(String[] args) {
int i = 1, j = 1, count = 0;
for (i = 1; i <= 3; i++) {//第一個迴圈,i從1到3,執行三圈
for (j = 1; j <= 9; j++)//第一個迴圈,j從1到9,執行九圈
count++;//每跑一次,便+1
}
System.out.printf("count = %d\n", count);
}
}
/* TQC+ JAVA6 - 303 */
public class JPA303 {
public static void main(String[] args) {
int i, j, sum = 0;
System.out.printf("1~1000中的完美數有: ");
for (j=2;j<1000;j++){//第一層迴圈從2跑到1000
sum=0;
for (i=1;i<j;i++) //第二層迴圈從1跑到第一層迴圈的數字便停止
if(j%i==0)//在其中,若有兩數相為j的因數,則將它加入總和中
sum = sum + i;
//若最後的總合等於本身該數,則印出銀幕來
if(sum==j)
System.out.printf("%d ",j);
}
}
}
/* TQC+ JAVA6 - 304 */
import java.util.Scanner;
public class JPA304 {
public static void main(String[] args) {
int total = 0;
int s = 0;
int count =0;
double average;
//先在迴圈外面要求使用這輸入
System.out.print("Please enter meal dollars or enter -1 to stop: ");
s = new Scanner(System.in).nextInt();
//進入迴圈時會檢查,使否符合條件
while(s!=-1){
//進入迴圈後,第一件事便是進行運算
count++;
total = total +s;
//運算完後再次要求使用者輸入
System.out.print("Please enter meal dollars or enter -1 to stop: ");
s = new Scanner(System.in).nextInt();
}
average = (double)total/count;
System.out.println("餐點總費用:" + total);
System.out.printf(" %d 道餐點平均費用為: %.2f %n",count,average);
}
}
/* TQC+ JAVA6 - 305 */
import java.util.Scanner;
public class JPA305 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
test();
test();
test();
}
public static void test() {
System.out.print("Please enter one value:");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int sum=1;
//條件判斷,是否在使用者輸入值1~10之間
if(a>0&&a<=10)
{
//進行階乘運算
for(int t=1 ; t<=a ; t++)
sum = sum*t;
System.out.printf("%d! : %d\n",a,sum);
}
else
System.out.printf("Error, the value is out of range.");
}
}
/* TQC+ JAVA6 - 306 */
import java.util.*;
public class JPA306 {
public static void main (String argv[]){
int num1, num2;
Scanner input = new Scanner(System.in);
//先在迴圈外面請使用者輸入第一個數字
System.out.println("Input:");
num1 = input.nextInt();
//檢查第一個數字是否符合條件
while (num1 != 999) {
//再請使用者輸入第二個數字
num2 = input.nextInt();
System.out.println(powerOf(num1, num2));
//之後再請使用者從頭輸入第一個數字
System.out.println("Input:");
num1 = input.nextInt();
}
}
//N次方運算方法
static int powerOf (int m, int n) {
//因Math.pow()的回傳值為doubel,故結果必須將它強制轉型為int
return (int) (Math.pow(m,n));
}
}
/* TQC+ JAVA6 - 307 */
import java.util.Scanner;
public class JPA307 {
public static void main (String argv[]){
int num1, num2;
//先讓使用者在迴圈外面輸入兩個數字
System.out.println("Input:");
num1 = new Scanner(System.in).nextInt();
num2 = new Scanner(System.in).nextInt();
//若條件不符合便不會進入迴圈
while (num1!=999) {
//一進會迴圈後,會先執行一次gcd方法
System.out.println(gcd(num1,num2));
//使用者再次輸入兩個數字,供下次迴圈判斷
System.out.println("Input:");
num1 = new Scanner(System.in).nextInt();
num2 = new Scanner(System.in).nextInt();
}
}
//gcd求最大公因數演算法
static int gcd (int m, int n) {
int tmp;
while (m % n != 0) {
tmp = n;
n = m % n;
m = tmp;
}
return n;
}
}
/* TQC+ JAVA6 - 308 */
//本題要求使用do-while-loop
import java.util.Scanner;
public class JPA308 {
static Scanner keyboard = new Scanner(System.in);
static int i = -1;
public static void main(String[] args) {
int total = 0, s = 0;
//do-while-loop的特性是部會先檢查條件,一定會先強行執行一次
do {
System.out.print("請輸入消費金額,或輸入-1結束:");
total = total +s;
//因我們是透過使用者輸入-1來判斷結束,所以-1此數值不可加入運算,故將它擺在後頭,使他無作用時
//會直接被條件是擋掉而跳離迴圈
s = new Scanner(System.in).nextInt();
} while(s!=-1);
System.out.print("電腦周邊總消費:" + total);
}
}
/* TQC+ JAVA6 - 309 */
import java.util.Scanner;
class JPA309 {
public static void main(String argv[]){
int size = new Scanner(System.in).nextInt();
int sum = 0;
//累加的迴圈
for(int a=1 ;a<=size;a++) {
//條件判斷,當a整除3或者整除5,則進入處理
if(a%3==0 ||a%5==0) {
//當在此判斷中,符合a整除7的數字,則直接結束該次迴圈以下的所有程式碼,從下一個迴圈開始
if(a%7==0)
continue;
sum = sum +a;
}
}
System.out.println("Answer: " + sum);
}
}
/* TQC+ JAVA6 - 310 */
//此題題目要求使用do-while-loop
import java.util.Scanner;
public class JPA310 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int init = 2,sum=0;
int size = 0;
//先讓使用者輸入n值
System.out.println("請輸入n的值(n>0,且為偶數):");
size = new Scanner(System.in).nextInt();
//檢查n值是否大於零且為偶數,若不符合便一再重新要求輸入
while(size<=0||size%2!=0) {
System.out.print("請輸入n的值(n>0,且為偶數):");
size = new Scanner(System.in).nextInt();
}
//開始進行累加動作
do{
sum = init +sum;
init += 2;
}while(init<=size);//當累加用的變數大於使用者輸入的值便停止
System.out.printf("%d+%d+...+%d=%d", 2,4,size,sum);
}
}


TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK

本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~

沒有留言:

張貼留言