標題:
TQC20220723模擬考二
[打印本頁]
作者:
葉桔良
時間:
2022-7-23 14:41
標題:
TQC20220723模擬考二
請將考試答案貼到這邊,並標示題號。
作者:
朱春男
時間:
2022-7-23 14:55
110
import java.util.Scanner;
public class JPA01 {
public static void main(String args[]) {
double totalarea;
System.out.printf("圓形面積為:%f \n",calCircle(5));
System.out.printf("三角形面積為:%f \n",calTriangle(10,5));
System.out.printf("方形面積為:%f \n",calRectangle(5,10));
totalarea = calCircle(5)+calTriangle(10,5)+calRectangle(5,10);
System.out.printf("此圖形面積為:%f \n",totalarea);
}
static double calCircle(double x) {
double total;
total=5*5*3.1415926;
return total;
}
static double calTriangle(double x,double y) {
double total;
total=x*y/2;
return total;
}
static double calRectangle(double x,double y) {
double total;
total=x*y;
return total;
}
}
複製代碼
作者:
朱春珠
時間:
2022-7-23 14:57
110
import java.util.Scanner;
public class JPA01 {
public static void main(String args[]) {
double totalarea;
System.out.printf("圓形面積為:%f \n",calCircle(5));
System.out.printf("三角形面積為:%f \n",calTriangle(10,5));
System.out.printf("方形面積為:%f \n",calRectangle(5,10));
totalarea = calCircle(5)+calTriangle(10,5)+calRectangle(5,10);
System.out.printf("此圖形面積為:%f \n",totalarea);
}
static double calCircle(double x) {
return x*x*3.1415926;
}
static double calTriangle(double x,double y) {
return x*y/2;
}
static double calRectangle(double x,double y) {
return x*y;
}
}
複製代碼
作者:
朱春珠
時間:
2022-7-23 14:57
202
import java.util.*;
class JPA02 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
test();
test();
}
public static void test() {
int x=0,y=0;
System.out.println("Input:");
x=keyboard.nextInt();
y=keyboard.nextInt();
if(x>y)
System.out.println(x+" is larger than "+y);
else
System.out.println(y+" is larger than "+x);
}
}
複製代碼
作者:
朱春珠
時間:
2022-7-23 14:58
310
import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int n=0,total=0;
System.out.print("請輸入n的值(n > 0,且為偶數):");
n=keyboard.nextInt();
do
{
System.out.print("請輸入n的值(n > 0,且為偶數):");
n=keyboard.nextInt();
}while(n<=0 || n%2!=0);
for(int i=2;i<=n;i+=2)
{
total+=i;
}
System.out.print("2+4+...+"+n+"="+total);
}
}
複製代碼
作者:
朱春珠
時間:
2022-7-23 14:59
402
import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
int n=0;
System.out.print("Input n(0 <= n <= 16): ");
n=keyboard.nextInt();
while(n!=999)
{
System.out.println(n+"的階乘(尾端遞迴) = "+x(n,1));
System.out.println(n+"的階乘(迴圈) = "+y(n));
System.out.print("Input n(0 <= n <= 16): ");
n=keyboard.nextInt();
}
}
static int x(int n,int ans)
{
if(n==0)
return ans;
else
return x(n-1,ans*n);
}
static int y(int n)
{
int ans=1;
for(int i=1;i<=n;i++)
{
ans=ans*i;
}
return ans;
}
}
複製代碼
作者:
朱春男
時間:
2022-7-23 15:00
202
import java.util.*;
class JPA02 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
test();
test();
}
public static void test() {
int x,y;
System.out.println("Input:");
x=keyboard.nextInt();
y=keyboard.nextInt();
if(x>y)
System.out.println(x+" is larger than "+y);
else
System.out.println(y+" is larger than "+x);
}
}
複製代碼
作者:
朱春男
時間:
2022-7-23 15:01
310
import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int n,total=0;
System.out.print("請輸入n的值(n > 0,且為偶數):");
n=keyboard.nextInt();
while(n<=0 || n%2!=0){
System.out.print("請輸入n的值(n > 0,且為偶數):");
n=keyboard.nextInt();
}
for(int i=2;i<=n;i+=2)
total+=i;
System.out.println("2+4+...+"+n+"="+total);
}
}
複製代碼
作者:
朱春男
時間:
2022-7-23 15:01
402
import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
int n;
System.out.print("Input n (0 <= n <= 16): ");
n=keyboard.nextInt();
while(n!=999){
System.out.println("6 的階乘(尾端遞迴) = "+facTail(n));
System.out.println("6 的階乘(迴圈) = "+facLoop(n));
System.out.print("Input n (0 <= n <= 16): ");
n=keyboard.nextInt();
}
}
static int facTail(int n){
if(n==0)
return 1;
else{
return n*facTail(n-1);
}
}
static int facLoop(int n){
int total=1;
for(int i=1;i<=n;i++)
total=total*i;
return total;
}
}
複製代碼
作者:
朱春男
時間:
2022-7-23 15:02
508
public class JPA05 {
public static void main(String[] argv) {
int[] data = {2, 4, 3, 5, 7, 6, 9, 1}; // 為排序的資料
int tmp;
for(int i=0;i<data.length-1;i++){
for(int j=0;j<data.length-1;j++){
if(data[j+1]<data[j]){
tmp=data[j+1];
data[j+1]=data[j];
data[j]=tmp;
}
}
for(int k=0;k<data.length;k++)
System.out.print(data[k]+" ");
System.out.println();
}
}
}
複製代碼
作者:
朱春珠
時間:
2022-7-23 15:10
508
public class JPA05 {
public static void main(String[] argv) {
int[] data = {2, 4, 3, 5, 7, 6, 9, 1}; // 為排序的資料
for(int i=0;i<8;i++)
{
for(int j=1;j<8;j++)
{
if(data[j-1]>data[j])
{
int tmp=data[j-1];
data[j-1]=data[j];
data[j]=tmp;
}
}
for(int k=0;k<8;k++)
{
System.out.print(" "+data[k]);
}
System.out.println();
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2