1. 程式人生 > >簡單計算器與面積結合計算器

簡單計算器與面積結合計算器

util stub num n) oid oat 選擇 str ner

import java.util.Scanner;
public class Study2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("請輸入運算種類");
System.out.println("1、四則運算");
System.out.println("2、面積計算");
Scanner scanner3=new Scanner(System.in);

System.out.print("選擇: ");
int number1=scanner3.nextInt();
if (number1==1)
{
System.out.println("1、減法運算");
System.out.println("2、加法運算");
System.out.println("3、乘法運算");
System.out.println("4、除法運算");
CatA cat1=new CatA();
Scanner scanner4=new Scanner(System.in);
System.out.print("選擇: ");
int number2=scanner4.nextInt();

if (number2==1)
{
Scanner scanner1=new Scanner(System.in);
System.out.print("請輸入第一個數: ");
cat1.a=scanner1.nextInt();
Scanner scanner2=new Scanner(System.in);
System.out.print("請輸入第二個數: ");
cat1.b=scanner2.nextInt();
System.out.print("結果:"+cat1.a+"-"+cat1.b+"=");cat1.jianFa();
}
else if(number2==2)
{
Scanner scanner1=new Scanner(System.in);
System.out.print("請輸入第一個數: ");
cat1.a=scanner1.nextInt();
Scanner scanner2=new Scanner(System.in);
System.out.print("請輸入第二個數: ");
cat1.b=scanner2.nextInt();
System.out.print("結果:"+cat1.a+"+"+cat1.b+"=");cat1.jiaFa();
}
else if(number2==3)
{
Scanner scanner1=new Scanner(System.in);
System.out.print("請輸入第一個數: ");
cat1.a=scanner1.nextInt();
Scanner scanner2=new Scanner(System.in);
System.out.print("請輸入第二個數: ");
cat1.b=scanner2.nextInt();
System.out.print("結果:"+cat1.a+"*"+cat1.b+"=");cat1.chengFa();
}
else if(number2==4)
{
Scanner scanner1=new Scanner(System.in);
System.out.print("請輸入第一個數: ");
cat1.a=scanner1.nextInt();
Scanner scanner2=new Scanner(System.in);
System.out.print("請輸入第二個數: ");
cat1.b=scanner2.nextInt();
System.out.print("結果:"+cat1.a+"/"+cat1.b+"=");cat1.chuFa();
}
else {System.out.println("您輸入的有誤,請重新選擇");}
}else if(number1==2) {

System.out.println("1、矩形面積計算");
System.out.println("2、三角形面積計算");
System.out.println("3、圓形面積計算");
Scanner scanner4=new Scanner(System.in);
System.out.print("選擇: ");
int number2=scanner4.nextInt();
CatA cat1=new CatA();
if(number2==1)
{
Scanner scanner1=new Scanner(System.in);
System.out.print("請輸入邊長: ");
cat1.a=scanner1.nextInt();
Scanner scanner2=new Scanner(System.in);
System.out.print("請輸入高: ");
cat1.b=scanner2.nextInt();
System.out.print("結果:"+cat1.a+"*"+cat1.b+"=");cat1.zhengFxmj();;
}
else if (number2==2)
{
Scanner scanner1=new Scanner(System.in);
System.out.print("請輸入三角形的底: ");
cat1.a=scanner1.nextInt();
Scanner scanner2=new Scanner(System.in);
System.out.print("請輸入三角形的高: ");
cat1.b=scanner2.nextInt();
System.out.print("結果:"+"("+cat1.a+"*"+cat1.b+")"+"/"+"2"+"=");cat1.sanJxmj();
}
else if(number2==3)
{
Scanner scanner1=new Scanner(System.in);
System.out.print("請輸圓的半徑: ");
cat1.a=scanner1.nextInt();
System.out.print("結果:"+"3.14"+"*"+cat1.a+"^"+"=");cat1.yuanXmj();
}else {System.out.println("您輸入的有誤,請重新選擇");}
}else {System.out.println("您輸入的有誤,請重新選擇");}
}

}


class CatA
{
int a;
int b;
public void jianFa() //減法函數
{
System.out.println(a-b);
}
public void jiaFa() //加法函數
{
System.out.println(a+b);
}
public void chengFa() //乘法函數
{
System.out.println(a*b);
}
public void chuFa() //除法函數
{
System.out.println((float)a/(float)b);
}
public void zhengFxmj() //矩形形面積
{
System.out.println(a*b);
}
public void sanJxmj() //三角形面積
{
System.out.println((a*b)/2);
}
public void yuanXmj() //圓形面積
{
a=(int)Math.pow(a, 2);
System.out.println(3.14*a);
}
}

簡單計算器與面積結合計算器