JAVA上機作業3

大愛索隆的主發表於2020-10-17

T2:

//求位移:
import java.util.Scanner;
public class t2 {
	public static double s(double v0,double t)
	{
		double s,g=9.8;
		s=v0*t+g*t*t/2.0;
		return s;
	}

	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		double v0,t;
		System.out.println("請輸入初速度和下落時間:");
		v0=cin.nextDouble();
		t=cin.nextDouble();
		System.out.print("下落時間為:"+s(v0,t));

	}

}

T1:

//求圓的周長和麵積:
import java.util.Scanner;
public class t1 {
	//周長:
		public static double zc(double r)
		{
			double c,pi=3.14;
			c=2*pi*r;
			return c;
		}
		//面積:
		public static double mj(double r)
		{
			double s,pi=3.14;
			s=pi*r*r;
			return s;
		}

	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		double r;
		r=cin.nextDouble();
		System.out.println("周長為:"+zc(r));
		System.out.println("面積為:"+mj(r));

	}
	

}

T3:

import java.util.Scanner;
//判斷奇偶:
public class t3 {
	public static int pd(int n)//判斷奇偶
	{
		int f;
		if(n%2==0)
			f=1;
		else
			f=0;
		return f;
	}

	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		int n;
		System.out.println("請輸入要判斷的整數:");
		n=cin.nextInt();
		if(pd(n)==1)
			System.out.println("是偶數");
		else
			System.out.println("不是偶數");

	}

}

T4:

import java.util.Scanner;
//判斷並計算三角形的面積
public class t4 {
	//判斷三角形
	public static int pd(double a,double b,double c)
	{
		int f;
		if(a+b>c&&a+c>b&&b+c>a&&a>0&&b>0&&c>0)
			f=1;
		else
			f=0;
		return f;
	}
	
	//計算面積:
	public static double mj(double a,double b,double c)
	{
		double p,s;
		p=(a+b+c)/2;
		s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
		return s;
	}

	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		double a,b,c;
		System.out.println("請輸入三角形三邊:");
		a=cin.nextDouble();
		b=cin.nextDouble();
		c=cin.nextDouble();
		if(pd(a,b,c)==0)
			System.out.println("非三角形");
		else
		{
			System.out.println("三角形面積為:"+mj(a,b,c));
		}

	}

}

T5:

import java.util.Scanner;
//判斷年級
public class t5 {
	public static int pd(int n)
	{
		int f;
		if(n==2)
			f=1;
		else if(n==3||n==4)
			f=2;
		else if(n==5||n==6)
			f=3;
		else
			f=0;
		return f;
		
		
	}

	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		System.out.println("請輸入孩子的年齡:");
		int nl;
		nl=cin.nextInt();
		if(pd(nl)==0)
			System.out.println("滾!");
		if(pd(nl)==1)
			System.out.println("小班");
		if(pd(nl)==2)
			System.out.println("中班");
		if(pd(nl)==3)
			System.out.println("大班");
			
		
	}

}

T6:

//實現分段函式:
import java.util.Scanner;
public class t6 {
 //行If實現:
  public static double y1(double x)
  {
   double y = 0;
   if(x<0)
    y=2*x+1;
    if(0<=x&&x<10)
    y=x*x;
    if(x>=10)
    y=4*x-4;
   return y;
  }
  //塊If實現:
 public static double y2(double x)
 {
  double y = 0;
  if(x<0)
   y=2*x+1;
  else if(0<=x&&x<10)
   y=x*x;
  else if(x>=10)
   y=4*x-4;
  return y;
 }
 //Select Case語句實現:
 public static double y3(double x)
 {
  double y=0int f = 0;if(x<10) f=0if(x>=0&&x<10) f=1if(x>=10) f=2;
  switch(f)
  {
  case 0:{y=2*x+1;break;}
  case 1:{y=x*x;break;}
  case 2:{y=4*x-4;break;}
  }
  return y;
}
 public static void main(String[] args) {
  Scanner cin=new Scanner(System.in);
  double x;
  x=cin.nextDouble();
  System.out.println(y1(x));
  System.out.println(y2(x));
  System.out.println(y3(x));
  }

T7:

import java.util.Scanner;
//排序:
public class t7 {
	public static int[] px(int a[],int n)//排序
	{
		int i,j;
		for(i=0;i<n;i++)
		{
			for(j=i;j<n;j++)
			{
				if(a[i]<a[j])
				{
					int t;
					t=a[i];
					a[i]=a[j];
					a[j]=t;
				}
			}
				
		}
		return a;
		
		
	}

	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		System.out.println("請輸入三個整數:");
		int a[],i;
		a=new int[3];
		for(i=0;i<3;i++)
		{
			a[i]=cin.nextInt();
		}
		a=px(a,3);
		for(i=0;i<3;i++)
		{
			System.out.print(a[i]+"  ");
		}
	}
}

T8:

import java.util.Scanner;
//算平均數,並輸出大於平均數的數:
public class t8 {
	public static double pj(double a,double b,double c)//平均數計算
	{
		double p;
		p=(a+b+c)/3;
		return p;
	}
	
	public static int pd(double a,double p)//判斷
	{
		int f;
		if(a>p)
			f=1;
		else
			f=0;
		return f;
	}

	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		double a,b,c;
		a=cin.nextDouble();
		b=cin.nextDouble();
		c=cin.nextDouble();
		System.out.println("平均數為:"+pj(a,b,c));
		if(pd(a,pj(a,b,c))==1)
			System.out.print(a);
		if(pd(b,pj(a,b,c))==1)
			System.out.print(b);
		if(pd(c,pj(a,b,c))==1)
			System.out.print(c);

	}

}

T9:

//產生50個隨機數,並輸出最小數
import java.util.Scanner;
import java.util.List; 
import java.util.Random; 
public class t9 {
 //隨機函式:
 public static int rd()
 {
  int j=(int)(Math.random()*100);
  return j;
 }
 //取最小數:
 public static int[] min(int a[])
 {
  int i;
  for(i=0;i<49;i++)
  {
   int j=i+1;
    if(a[i]<a[j])
    {
     int t;
     t=a[i];
     a[i]=a[j];
     a[j]=t;
    }  
  }
  return a;
 }
 public static void main(String[] args) {
  int i;
  int a[];
  a=new int[50];
  for(i=0;i<50;i++)
  {
   a[i]=rd();
  }
  min(a);
  System.out.println(a[49]);
 }
}

T10:

//求100以內所有奇數的和
import java.util.Scanner;
public class t10 {
 //for迴圈實現:
 public static int h1()
 {
  int i,sum=0;
  for(i=1;i<100;i+=2)
  {
   sum+=i;
  }
  return sum;
 }
 //do-while實現:
 public static int h2()
 {
  int i=1,sum=0;
  do
  {
   sum+=i;
   i+=2;
  }while(i<100);
  return sum;
 }
 //while實現:
 public static int h3()
 {
  int i=1,sum=0;
  while(i<100)
  {
   sum+=i;
   i+=2;
  }
  return sum;
 }
 public static void main(String[] args) {
  System.out.println(h1());}
}

T11:

//輸出100~200以內所有不能被3和4整除的數
public class t11 {
 //判斷能否被3和4整除
 public static int pd(int a)
 {
  int f=0;
  if(a%3==0||a%4==0)
   return f;
  else
   return a;
 }
 public static void main(String[] args) {
  int i;
  for(i=100;i<=200;i++)
  {
   if(pd(i)!=0)
    System.out.print(i+"  ");
  }
 }
}

T12:

//100~200間各位數字之和為6的數字
public class T12 {
 //計算各位數之和並判斷是否為6
 public static int pdhe(int a)
 {
  int i,sum=0,f=0;
  for(i=10;a>0;i=10)
  {
   int b=a%i;
   sum+=b;
   a=(a-b)/i;
  }
  if(sum==6)
   f=1;
  return f;
 }
 public static void main(String[] args) {
  int i,sum=0;
  for(i=100;i<=200;i++)
  {
   if(pdhe(i)==1){
    System.out.print(i+" ");
    sum++;}
  }
  System.out.println(); 
  System.out.print("共"+sum+"個"); 
 }
}

T13:

//計算兩個數的字尾表示式
import java.util.Scanner;
public class T13 {
 //計算
 public static int js(int a,int b,char c)
 {
  int d=0;
  switch(c)
  {
  case '+':{d=a+b;break;}
  case '-':{d=a-b;break;}
  case '*':{d=a*b;break;}
  case '/':{d=a/b;break;}
  }
  return d;
 }
 public static void main(String[] args) {
  Scanner cin=new Scanner(System.in);
  int a,b;
  char c;
  a=cin.nextInt();
  b=cin.nextInt();
  c=cin.next().charAt(0);
  System.out.print(js(a,b,c));
 }
}

T14:

//輸入年份和月份,輸出該月的天數:
import java.util.Scanner;
public class t14 {
 //判斷是否為閏年
 public static int rn(int n)
 {
  if((n%4==0&&n%100!=0)||(n%4==0&&n%400==0))
   return 1;
  else
   return 0;
 }
//判斷該月的天數
 public static int yue(int n,int y)
 {
  int t=30;
  if(rn(n)==1)
  {
   switch(y)
   {case 1: case 3: case 5: case 7: case 8: case 10: case 12:{t=31;break;}
    case 2:{t=29;break;} 
   }
  }
  else
  {
   switch(y)
   {case 1: case 3: case 5: case 7: case 8: case 10: case 12:{t=31;break;}
    case 2:{t=28;break;} 
   }
  }
  return t;
 }
 public static void main(String[] args) {
  Scanner cin=new Scanner(System.in);
  System.out.println("請輸入年份和月份:");
  int n,y;
  n=cin.nextInt();y=cin.nextInt();
  System.out.println("該月天數為:"+yue(n,y));
 }
}

T15:

//求一個正數的階乘
import java.util.Scanner;
public class t15 {
//for實現:
 public static int jc1(int a)
 {
  int n=1,i;
  for(i=1;i<=a;i++)
  {n*=i;}
  return n;
 }
 //while實現:
 public static int jc2(int a)
 {
  int n=1,i=1;
  while(i<=a)
  {n*=i;i++;}
  return n;
 }
 //do-while實現:
 public static int jc3(int a)
 {
  int n=1,i=1;
  do
  {n*=i;i++;}while(i<=a);
  return n;
 }
 public static void main(String[] args) {
  Scanner cin=new Scanner(System.in);
  System.out.println("請輸入一個正數:");
  int a; a=cin.nextInt();
        if(a<=0) System.out.println("輸入錯誤");
        else System.out.println(jc3(a));
 }
}

T16:

//統計正整數和負整數的個數
import java.util.Scanner;
public class t16 {
 //正負數判斷
 public static int zf(int a)
 {
  int f=0;
  if(a>0) f=1;
  return f;
 }
 public static void main(String[] args) {
  Scanner cin=new Scanner(System.in);
  System.out.println("請輸入一些整數:");
  int a,z=0,f=0,i=0;
  while(i==0)
  {
   a=cin.nextInt();
   if(a==0) break;
   else{
    if(zf(a)==1) z++;
    else f++;
   }
  }
  System.out.println("正整數個數為:"+z);
  System.out.println("負整數個數為:"+f);
 }
}

相關文章