简单的Java程序

来源:互联网 发布:php extension dir 编辑:程序博客网 时间:2024/05/16 11:28
一:设计一个程序,完成如下内容:


===欢迎使用===
| 1.求最大值 |
| 2.求最小值 |
| 3.求指数值 |
| 4.求平方根 |
| 5.退       出 |
===========
请您选择:


1.
请您输入第一个数字:
请您输入第二个数字:
x和y中较大的数字为:


2.
请您输入第一个数字:
请您输入第二个数字:
x和y中较小的数字为:


3.
请您输入底数:
请您输入指数:
x的y次方为:


4.
请您输入开方数:
x的平方根为:

5.谢谢使用:

代码:package text3;
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {
show();
oprater();
}


public static void oprater() {
System.out.print("请选择:");
while (true) {
Scanner sc = new Scanner(System.in);
String num = sc.nextLine();
switch (num) {
case "1":
max();
break;
case "2":
min();
break;
case "3":
pow();
break;
case "4":
sqrt();
break;
case "5":
System.out.println("欢迎下次使用!!!!");
System.exit(0);
break;


default:
System.out.println("系统错误!!!");
break;
}
}
}


public static void show() {
System.out.println("--欢迎使用--");
System.out.println("1.求最大值:");
System.out.println("2.求最小值:");
System.out.println("3.求指数值:");
System.out.println("4.求平方根:");
System.out.println("5:退       出:");


}


static void max() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个数字:");
double x = sc.nextDouble();
System.out.print("请输入第二个数字:");
double y = sc.nextDouble();
if (x <= y) {
x = y;
}
System.out.println("两个数中最大值为:" + x);
System.out.print("请选择:");
}


static void min() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个数字:");
double x = sc.nextDouble();
System.out.print("请输入第二个数字:");
double y = sc.nextDouble();
if (x > y) {
x = y;
}
System.out.println("两个数中最小值为:" + x);
System.out.print("请选择:");
}


static void pow() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入数字:");
double a = sc.nextDouble();
System.out.print("请输入指数:");
double b = sc.nextDouble();
double result = Math.pow(a, b);
System.out.println(a + "^" + b + "=" + result);
System.out.print("请选择:");
}


static void sqrt() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个正数:");
double a = sc.nextDouble();
double result1 = Math.sqrt(a);
double result2 = -Math.sqrt(a);
System.out.println(a + "的平方根是:" + result1 + "和" + result2);
System.out.print("请选择:");
}
}


二:打印图形

1:空心矩形:
**********
*             *
*             *
*             *

**********

代码:static void hollowRectangle() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
if (i == 0 || i == 4 || j == 0 || j == 9) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}


2:空心上三角(右):
##########
#       #
 #      #
  #     #
   #    #
    #   #
     #  #
      # #
      ##
       #

代码:static void hollowUpTriangleRight(){
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if(i==1||j==10||i==j){
System.out.print("#");
}else {
System.out.print(" ");
}
}
System.out.println();
}

3:空心上三角(左):

##########
#       # 
#      #  
#     #   
#    #    
#   #     
#  #      
# #       
##        
#    

代码:static void hollowUpTriangleLeft(){
for (int i = 0; i < 10; i++) {


for (int j = 0; j <10; j++) {
if(i==0||j==0||i+j==9){
System.out.print("#");
}else {
System.out.print(" ");
}
}
System.out.println();
}


4:实心上三角(右):
##########
  #########
    ########
      #######
        ######
          #####
            ####
              ###
                ##
                  #

代码:static void filledUpTriangleRight(){
for (int i = 1; i <= 10; i++) {
for (int j = 1; j<=10 ; j++) {
if(i>j){
System.out.print(" ");
}else {
System.out.print("#");
}
}
System.out.println();
}

}



5:实心上三角(左):
##########
######### 
########  
#######   
######    
#####     
####      
###       
##        
#    

代码:static void filledUpTriangleLeft(){
for (int i = 1; i <= 10; i++) {
for (int j = 1; j<=10 ; j++) {
if(j>=i){
System.out.print("#");
}
}
System.out.println();
}
}



6:空心下三角(左):
#         
##        
#  #       
#     #      
#       #     
#          #    
#            #   
#               #  
#                 # 
##########

代码:static void hollowBelowTriangleLeft(){
for (int i = 1; i <= 10; i++) {
for (int j = 1; j<=10 ; j++) {
if(i==10||j==1||i==j){
System.out.print("#");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}


7:空心下三角(右):
                                 #
                              # #
                          #     #        
                       #        #      
                    #          #     
                 #             #  
              #                #
           #                  #

        #                     #

     # # # # #### # #

代码:static void hollowBelowTriangleRight(){
for (int i = 1; i <= 10; i++) {
for (int j = 1; j<=10 ; j++) {
if(i==10||j==10||i+j==11){
System.out.print("#");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}


8:

#         
##        
###       
####      
#####     
######    
#######   
########  
######### 
##########

代码:

static void filledBelowTriangleLeft(){
for (int i = 1; i <= 10; i++) {
for (int j = 1; j<=10 ; j++) {
if(j<=i){
System.out.print("#");
}
}
System.out.println();
}
}


9:实心下三角 (右):
                      #
                    ##
                  ###
                ####
             #####
          ######
        #######
     ########
  #########
##########

代码:static void filledBelowTriangleRight(){
for (int i = 1; i <= 10; i++) {
for (int j =1; j<=10 ; j++) {
if(i+j>=11){
System.out.print("#");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}

10:空心菱形:
 #          
# #         
#   #        
      #     #       
     #       #      
    #         #     
   #           #    
  #             #   
 #               #  
#                 # 
#                   #
#                 # 
 #               #  
  #             #   
   #           #    
    #         #     
     #       #      
      #     #       
       #   #        
        # #         
         #          


代码:

static void hollowRhombus(){
for (int i = 1; i <= 21; i++) {
for (int j =1; j<=21 ; j++) {
if(i+j==12||i==j+10||j==i+10||i+j==32){
System.out.print("#");
}else {
System.out.print(" ");
}
}
System.out.println();
}

}



11 实心菱形:      #          
                          ###         
                       #####        
                     #######       
                  #########      
               ###########     
            #############    
         ###############   
      #################  
   ################### 
#####################
  ################### 
    #################  
      ###############   
        #############    
         ###########     
           #########      
             #######       
               #####        
                 ###         
                   # 

代码: static void filledRhombus(){
    for (int i = 1; i <= 21; i++) {
for (int j =1; j<=21 ; j++) {
if(i+j>=12&&i<=j+10&&j<=i+10&&i+j<=32){
System.out.print("#");
}else {
System.out.print(" ");
}

}
System.out.println();
}
}

三、输入两个整数,返回这两个数的最大公约数和最小公倍数

package text3;
import java.util.Scanner;
public class MinMaxData {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个整数:");
double x = sc.nextDouble();
System.out.print("请输入第二个整数:");
double y = sc.nextDouble();
highestCommonDivissor( x,y);
System.out.println(x+","+y+"的最大公約数是:"+highestCommonDivissor( x,y));
lowestCommonMultiple( x, y);

}
static double highestCommonDivissor(double x,double y){
double z=x%y;
//余数不为0,继续相除,直到余数为0   
while(z!=0) {
x=y;
y=z;
z=x%y;
}
return y;
}//最小公倍數:x*y/他們的最大公約數
static void  lowestCommonMultiple(double x,double y){

double result=(x*y)/MinMaxData.highestCommonDivissor(x, y);
System.out.println(x+","+y+"的最小公倍数是:"+result);

}


}

四:求1!+2!+……+10!的和。

代码:package text3;


public class Factorial {


public static void main(String[] args) {
muliple();
}
static void muliple(){
int a=1;
long sum=0L;
for(int i=1;i<=10;i++){
     a*=i;
sum=sum+a;
}
System.out.println("1!+2!+3!+4+5!+6!+7!+8!+9!+10!="+sum);
}


}