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);
        }
    }
}
相關文章
相關標籤/搜索