Java练习1

*/
 * Copyright (c) 2016,烟台大学计算机与控制工程学院
 * All rights reserved.
 * 文件名:text.cpp
 * 作者:常轩
 * 微信公众号:Worldhello
 * 完成日期:2016年9月20日
 * 版本号:V1.0
 * 程序输入:无
 * 程序输出:见运行结果
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Qing
 */
import java.util.Scanner;
public class Test {
      public static void main(String args[]){
         
        Scanner reader=new Scanner(System.in);
         //P40  实验3-1  将三个整数从小到大进行排序
        System.out.println("Please input three number.");
        int Num1,Num2,Num3;
        Num1=reader.nextInt();
        Num2=reader.nextInt();
        Num3=reader.nextInt();
        Number n=new Number();
        n.sort(Num1, Num2, Num3);
        /*-------------------------------------------------------------------*/
        //P42  实验3-2  根据彩票号的末尾号码来判断所创建对象的中奖情况
        int number;
        System.out.println("Please input your number:");
        number=reader.nextInt();
        Administrator person=new Administrator();     
        person.juage(number);
        /*-------------------------------------------------------------------*/
        //P46   实验1   根据用户电量计算电费 
        double electricity;
        System.out.println("Please input electricity:");
        electricity=reader.nextDouble();
        Calculation family=new Calculation();
        family.calcu(electricity);
        /*-------------------------------------------------------------------*/
        //P47   实验2   猜数字游戏,提示玩家猜大了还是猜小了
         Guess student=new Guess();
         student.guessnumber();
        /*-------------------------------------------------------------------*/
        //P49   练习4  用for循环输出俄文字母表
        Alphabet one=new Alphabet();
        one.pAlphabet();
       /*--------------------------------------------------------------------*/
        //P49   练习5  编写一个程序求1!+2!+.....+20!
        Factorial two=new Factorial();
        two.pfactorial();
       /*-------------------------------------------------------------------*/
        //P49   练习6  求1000以内的所有完数
         PerfectNumber three=new PerfectNumber();
        three.findPerfectnumber();
        /*------------------------------------------------------------------*/
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Qing
 */
public class Administrator {
     void juage(int number){                  //用switch语句来实现判断
        if(number<=0||number>99999){
            System.out.println("Input error!Please enter the five digit lottery number");
        }else{
        int d1=number%10;
        int d2=number%100;
        int d3=number%1000;
        switch(d1){
            case 1:
            case 3:
            case 9:
                System.out.println("Lottery is the three prize!");
                break;
            default:
                System.out.println("Lottery is not the three prize");
        }
        switch(d2){
            case 29:
            case 46:
            case 21:
                System.out.println("Lottery is the two prize");
                break;
            default :
                System.out.println("Lottery is not the two prize");
        }
        switch(d3){
            case 875:
            case 326:
            case 596:
                System.out.println("Lottery is the one prize");
                break;
            default:
                System.out.println("Lottery is not the one prize");
        }
        }
        
    }
     
     //用if-else  if-else 多条件分支语句代替switch语句来实现判断
     /*
        void judge(int number){
        if(number<=0||number>99999){
            System.out.println("Input error!Please enter the five digit lottery number");
        }else{
        int d1=number%10;
        int d2=number%100;
        int d3=number%1000;
        if(d1==1||d1==3||d1==9){
            System.out.println("Lottery is the three prize!");
            if(d1==1||d1==9){
                if(d2/10==2){
                     System.out.println("Lottery is the two prize");
                }
            }
        }else if(d2==21||d2==29||d2==46){
            System.out.println("Lottery is the two prize");
        }else if(d3==875||d3==326||d3==596){
             System.out.println("Lottery is the one prize");
        }
        }
     }
      */
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Qing
 */
public class Alphabet {
     void pAlphabet(){
        char c1 = 'А';
        char c2 = 'а'; 
        for (int i = c1; i < c1 + 32; i++) {
        System.out.print((char) i+""+(char)(c2++)+" "); 
        }
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Qing
 */
public class Calculation {
     void calcu(double electricity){
        double elecFee=0;
        if(electricity<0){
            System.out.println("Input error!");
        }
        else{
            if(electricity<=90){
                elecFee=electricity*0.6;
            }
            if(electricity>=91&&electricity<=150){
                elecFee=90*0.6+(electricity-90)*1.1;
            }
            if(electricity>150){
                elecFee=90*0.6+60*1.1+(electricity-150)*1.7;
            }
        }
        System.out.printf("ElectricityFees is:%5.2f",elecFee);
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Qing
 */
public class Factorial {
     void pfactorial(){
        int sum=0,product=1;
        int i,j;
        for(i=1;i<=20;i++){
            for(j=1;j<=i;j++){
                product=product*j;
            }
            sum=sum+product;
            product=1;
        }
        System.out.println("Result is:"+sum);
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Qing
 */
import java.util.Scanner;
import java.util.Random;
public class Guess {
     void guessnumber(){
        Scanner reader=new Scanner(System.in);
        Random random=new Random();
        int gNumber;
        int rNumber=random.nextInt(100)+1;                   //产生1-100之间的整数
        System.out.println("Give you an integer between 1-100, please guess the number:");
        System.out.println("Please enter your guess:");
        gNumber=reader.nextInt();
        if(gNumber<1||gNumber>100){
            System.out.println("Input error!please again.");
            gNumber=reader.nextInt();
        }
        while(gNumber!=rNumber){
            if(gNumber>rNumber){
                System.out.println("Too big,please input again!");
                gNumber=reader.nextInt();
            }
            else{
                System.out.println("Too small,please input again!");
                gNumber=reader.nextInt();
            }
        }
        System.out.println("You are right!");
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Qing
 */
public class PerfectNumber {
    void findPerfectnumber(){
        int factorSum=0;
        int i,j;
        for(i=2;i<=1000;i++){
            for(j=1;j<=i/2;j++){
                if(i%j==0){
                    factorSum=factorSum+j;
                }
            }
            if(factorSum==i){
                System.out.println(i);
            }
            factorSum=0;
        }
    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Qing
 */
public class Number {
     void sort(int n1,int n2,int n3){
        int temp=0;
        if(n2<n1){
            temp=n1;
            n1=n2;
            n2=temp;    
            System.out.println("The first sort result is :"+n1+"、"+n2+"、"+n3);
        }
        if(n3<n1){
            temp=n3;
            n3=n1;
            n1=temp;
            System.out.println("The second sort result is :"+n1+"、"+n2+"、"+n3);
        }
        if(n3<n2){
            temp=n3;
            n3=n2;
            n2=temp;
            System.out.println("The end sort result is :"+n1+"、"+n2+"、"+n3);
        }
    }
}

posted @ 2016-09-20 15:06  壹言  阅读(186)  评论(0编辑  收藏  举报