1.打印九九乘法表
1 x 1 = 1
1 x 2 = 2 ,2 x 2 = 4
…
import java.util.*;
public class Demozy1 {
public static void main(String[] args) {
/*
1.打印九九乘法表
1 x 1 = 1
1 x 2 = 2 ,2 x 2 = 4
...
*/
for (int n = 1; n <= 9 ;n++ ){
//int num = 1 ;
for (int m = 1;m <= n ;m++ ){
System.out.print(m+"X"+ n+"="+n*m+" ");
if (m == n){
System.out.println();
break;
}
//num++;
}
}
}
}
2.打印等腰三角形
import java.util.*;
public class Demozy2 {
public static void main(String[] args) {
/*
2.打印等腰三角形
*
* *
* * *
* * * *
* * * * *
*/
Scanner sc =new Scanner(System.in);
System.out.print("请输入需要打印的行数:");
int num=sc.nextInt();
for (int n=1;n<=num ;n++ ){
//先打印“空格”
for (int kong=1;kong<=num-n ;kong++ ){
System.out.print(" ");
}
//后打印 “ * ” 。
for (int xing=1;xing<=(2*n-1);xing++ ){
System.out.print("*");
}
System.out.println();
}
}
}
3.定义方法,计算1+2+…n的和。
import java.util.*;
public class Demozy3 {
public static void main(String[] args) {
//3.定义方法,计算1+2+...n的和。
Scanner sc =new Scanner(System.in);
System.out.println("请输入一个整数");
int n = sc.nextInt();
int sum = gethe(n);
System.out.println("1 + 2 + ......" + n + " = " + sum);
}
public static int gethe(int n){
int sum = 0;
for (int i = 1;i<=n; i++){
sum=sum+i;
if(i == n){
break;
}
}
return sum;
}
}
4.简单的计算器:
用户键盘输入两个数字,然后提示用户选择(1-4)相关运算方式(输入1进行加法运算、输入2进行减法运算、输入3进行乘法运算、输入4进行除法运算)
根据选择的运算方式调用相应的方法,进行运算,并返回运算结果,打印输出到屏幕
import java.util.*;
public class Demozy4 {
public static void main(String[] args) {
/*
4.简单的计算器:
*/
Scanner sc = new Scanner(System.in);
System.out.println("请依次输入两个整数");
double a = sc.nextDouble();
double b = sc.nextDouble();
System.out.println("请选择运算符 1.“ + ”、2.“ - ”、3.“ x ”4.“ ÷ ”");
int fu = sc.nextInt();
if (fu == 1){
double jg = getjia(a,b);
System.out.println(a+" + "+b+" = "+jg);
}else if (fu == 2){
double jg = getjian(a,b);
System.out.println(a+" - "+b+" = "+jg);
}else if (fu == 3){
double jg = getcheng(a,b);
System.out.println(a+" x "+b+" = "+jg);
}else if (fu == 4){
double jg = getchu(a,b);
System.out.println(a+" ÷ "+b+" = "+jg);
}else{
System.out.print("请输入1、2、3、4 中的任一正确的运算符序号!");
}
}
public static double getjia(double a , double b){
return a+b ;
}
public static double getjian(double a , double b){
return a-b ;
}
public static double getcheng(double a , double b){
return a*b ;
}
public static double getchu(double a , double b){
return a/b ;
}
}