2012年8月5日 星期日

[作業] 階乘問題 n!

1.請寫出可計算n!階乘的類別,若n=10 計算10!。

階乘
0!=1
1!=1
2!=2*1=2
3!=3*2*1=6
4!=4*3*2*1=24
依此類推

2.請運用階乘類別寫出可計算階乘和的類別,若n=3 計算1!+2!+3!。

3.請說明兩者的關係 is a 或是 has a。


A:
終於發表題目了....趕緊來看看...哈哈
回復如下:

本帖隱藏的內容需要回覆才可以瀏覽

import java.util.*;
class fact{
        private int sum = 0;
        public int setFact(int n){
                int fac=1;
                if(n == 0){
                        fac = 1 ;
                }else{
                        for(int i=1;i<=n;i++){
                                fac *= i;
                        }
                }
                        return(fac);                        
        }
        public int setSum(int m){
                for(int i =0;i<=m;i++){
                        sum+=setFact(i);
                }
                return(sum);
        }
}
public class Test3 {
        public static void main(String[] args){
                Scanner s=new Scanner(System.in);
                System.out.println("請輸入n值");
                int m = s.nextInt();
                fact ff = new fact();
                System.out.println("階乘:"+ff.setFact(m));
                System.out.println("階乘和:"+ff.setSum(m));
        }
}
兩者在同一類別中

BigInteger的使用方法

個人試著練習了一下BigInteger的使用方法
但以下程式碼卻發生9個錯誤

import static java.lang.System.*;
import java.math.*;
public class BigInteger{
    public static void main(String args[]){
        BigInteger a = new BigInteger("8786567886771666523775892");
        BigInteger b = new BigInteger("1126838773993333890888316");
        BigInteger c = a.add(b);
        out.println("a + b = " + c);
        out.println("a - b = " + a.add(b.negate()));
        out.println("a - b = " + a.subtract(b));
        out.println("a × b = " + a.multiply(b));
        out.println("a ÷ b = " + a.divide(b));
        out.println("a^3 = " + a.pow(3));
        out.println("gcd(a , b)" + a.gcd(b));
    }
}

大致發生這3種錯誤:
constructor BigInteger in class BigInteger cannot be applied to given types;
actual and formal argument lists differ in length
error: cannot find symbol

向各方高手 求解  謝謝
A:
請把你的類別名稱改掉

名稱要具唯一性

BigInteger a = new BigInteger

你是要NEW你自己,還是要NEW "java.math.BigInteger" ??
A:
說得好
其實class名稱我改過了
但是錯誤並沒有減少的趨勢
謝謝您的回答
A:
請問您的類別名稱改掉後,檔名有跟著一起改嗎?

有重新編譯??

我付上三張圖片,證明用各種方法來跑您貼上的程式碼,是正常的

By 柏宇

DOS  Eclipse  NetBeans




Q: 這就怪了 @@
看來我們改的擋名&class都一樣
居然我的DOS編譯不了
難道這台電腦有問題 = =?
我試試看其他電腦好了
非常感謝

重新指派法

請問一下大家,昨天小孟老師的第二題範例(重新指派法).
第一個問題:
有沒有test t=new test();這行.
因為想說是static頂層方法,可以直接去存取,不需要再建立物件.
想說是不是我打字的時候打錯?
第二個問題:
關於GC回收,因為會回傳s,是否是main方法內的s被回收?

public class test
{
        public static void main(String args[])
        {
                test t=new test();
                StringBuffer s=getStringBuffer();
                System.out.println("s="+s);
        }
        static StringBuffer getStringBuffer()
        {
                StringBuffer s=new StringBuffer("java");
                return s;
        }
}


A:
>>第一個問題:
>>有沒有test t=new test();這行.
你可以試試看,即使把這行mark之後一樣可以執行,因為這個t 確實沒用到

>>第二個問題:
>>關於GC回收,因為會回傳s,是否是main方法內的s被回收?
假如是 main方法內的s被回收,那誰要來接收getStringBuffer所回傳的s?

[作業] 撰寫販賣機

請以物件導向方式撰寫報紙販賣機及飲料販賣機。

1.硬幣物件有三種幣別屬性1元 5元 10元。
2.販賣機可接受硬幣物件。
3.販賣機有投幣、退幣找零、按鈕購物及顯示物品功能。
4.販賣機可賣三種商品。
要做一個使用者視窗介面出來
Q:
老師~
用import java.io.*;、BufferedReader寫的話
可以嗎~?A: 可以


A:
簡陋版瘋哥販賣機
import java.util.*;
class Coins{//硬幣類別
        private int one;
        private int five;
        private int ten;
        private int money;
        public void setMoney(int n){//應找金額硬幣數量
                money = n;
                ten=(int)n/10;
                five = (int)(n-ten*10)/5;
                one = n-(ten*10+five*5);
        }
        public void getMoney(){//印出硬幣數量
                System.out.println("退"+ten+"個10元硬幣");
                System.out.println("退"+five+"個5元硬幣");
                System.out.println("退"+one+"個1元硬幣");
                System.out.println("共退"+money+"元");
        }
}        
class Vma{
                private int money;
                private int[] moneyObj;
                public void inPutMoney(int n){//投幣
                        money = n;
                }
                public void outPutMoney(){//找零
                        Coins c = new Coins();
                        c.setMoney(this.money);
                        c.getMoney();
                }
                public void setObj(String s,int s1 , String ss,int ss1 , String sss,int sss1){//設定三種商品
                        int[] moneyObj1 = {s1,ss1,sss1};
                        moneyObj = moneyObj1;
                        System.out.print("品項   1:"+s+"   ");
                        System.out.print("2:"+ss+"   ");
                        System.out.print("3:"+sss+"\n\n");
                }
                public void moneyCount(int n){//計算金額
                        int a = money;
                        if((a-n)>=0){
                                money-=n;
                        }else{
                                System.out.println("投入金額不足");
                        }
                        
                }
                public int chooseObj(int n){//選擇項目
                        if(n==1)
                                return moneyObj[0];
                        else if(n==2)
                                return moneyObj[1];
                        else if(n==3)
                                return moneyObj[2];
                        else
                                System.out.println("輸入錯誤");
                                return 0;
                }
}
public class Test4 {
        public static void main(String[] args){
                Scanner s = new Scanner(System.in);
                System.out.println("**********************");
                System.out.println("******瘋哥販賣機******");
                System.out.println("**********************\n");
                Vma v = new Vma();
                v.setObj("可樂100元",100,"紅茶50元",50,"果汁120元",120);
                System.out.print("@@@@@@請輸入投幣金額:");
                int n=s.nextInt();
                v.inPutMoney(n);
                System.out.print("\n======請輸入購買品項例如買第一個品項就輸入1)");
                int n1 = s.nextInt();
                v.moneyCount(v.chooseObj(n1));
                v.outPutMoney();
                
        }
}

複寫跟多載是不一樣的東西

小孟老師有說用方法寫出這題目,下面是可以編譯的,但是我把System那行改了一下,編譯就出錯了,請問問題是出在哪邊?

可以編譯的:
class CShape
{
        public double area()
        {
                return 1.0;
        }
}
class CCircle extends CShape
{
        double radius;
        public double area(double r)
        {
                return r*r*3.1415926;
        }
}
class CSquare extends CShape
{
        double side;
        public double area(double s)
        {
                return s*s;
        }
}
class CTriangle extends CShape
{
        double base,height;
        public double area(double b,double h)
        {
                return b*h/2;
        }
}
public class test 
{
        public static void main(String args[])
        {
                CShape s01=new CCircle();
                System.out.println(((CCircle)s01).area(2.0));

                CShape s02=new CSquare();
                System.out.println(((CSquare)s02).area(2.0));

                CShape s03=new CTriangle();
                System.out.println(((CTriangle)s03).area(2.0,2.0));
        }
}

編譯錯誤的 
public class test 
{
        public static void main(String args[])
        {
                CShape s01=new CCircle();
                System.out.println(s01.area(2.0));

                CShape s02=new CSquare();
                System.out.println(s02.area(2.0));

                CShape s03=new CTriangle();
                System.out.println(s03.area(2.0,2.0));
        }
}

test.java:37: error: method area in class CShape cannot be applied to given type
s;
                System.out.println(s01.area(2.0));
                                      ^
  required: no arguments
  found: double
  reason: actual and formal argument lists differ in length
test.java:40: error: method area in class CShape cannot be applied to given type
s;
                System.out.println(s02.area(2.0));
                                      ^
  required: no arguments
  found: double
  reason: actual and formal argument lists differ in length
test.java:43: error: method area in class CShape cannot be applied to given type
s;
                System.out.println(s03.area(2.0,2.0));
                                      ^
  required: no arguments
  found: double,double
  reason: actual and formal argument lists differ in length
3 errors

這題錯在哪?


class test
{
        int i01,i02,sum01=0,sum02=1;
        test(int i01)
        {
                for(int i02=1;i02<=i01;i02++)
                {
                        sum01+=i02;
                        sum02*=i02;
                }
                        System.out.println("1+2+3+....+"+i01+"="+sum01);
                        System.out.println("1*2*3*....*"+i01+"="+sum02);
        }
}
import javax.swing.*;
public class test01
{
        public static void main(String args[])
        {
                int num;
                String str01;
                do
                {
                        str01=JOptionPane.showInputDialog("請輸入大於一以上數字");
                        num=Integer.parseInt(str01);
                }while(num<1);
                test t=new test(num);
        }
}

編譯會出錯訊息

test01.java:15: error: class, interface, or enum expected
import javax.swing.*;
^
1 error

A:
請把你的import

貼在 class test 上方..........

不然就是把你的class test 另外創一個類別
A:
import javax.swing.*;
public class test01
{
        public static void main(String args[])
        {
                int num;
                String str01;
                do
                {
                        str01=JOptionPane.showInputDialog("請輸入大於一以上數字");
                        num=Integer.parseInt(str01);
                }while(num<1);
                test t=new test(num);
        }
}
class test
{
        int i01,i02,sum01=0,sum02=1;
        test(int i01)
        {
                for(int i02=1;i02<=i01;i02++)
                {
                        sum01+=i02;
                        sum02*=i02;
                }
                        System.out.println("1+2+3+....+"+i01+"="+sum01);
                        System.out.println("1*2*3*....*"+i01+"="+sum02);
        }
}

請遵循package -import-class順序即可

homew

引用老師 Release 的 Source code 稍微改造一下,並且只使用目前教過的東西。

花了今天一些時間到補習班實作,僅供參考@@"
package guessgame;

import javax.swing.*;

public class Guess {

    public static void main(String[] args) {
        int a = 0, b = 0, c = 0;
        a = (int) (Math.random() * 9 + 1);
        b = (int) (Math.random() * 9 + 1);
        for (; a == b;) {
            b = (int) (Math.random() * 9 + 1);
        }
        c = (int) (Math.random() * 9 + 1);
        for (; c == b || c == a;) {
            c = (int) (Math.random() * 9 + 1);
        }

        String userInput = JOptionPane.showInputDialog("請輸入三個數字");

        int playerInput = Integer.parseInt(userInput);
        //先做數字個數判斷,確保個數是3個,才不會造成變數 d、e、f 出錯。
        for (; playerInput < 100 || playerInput > 1000;) {
            for (; playerInput < 100;) {
                JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數少於三個");
                userInput = JOptionPane.showInputDialog("請再輸入一次");
                playerInput = Integer.parseInt(userInput);
            }
            for (; playerInput > 1000;) {
                JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數多於三個");
                userInput = JOptionPane.showInputDialog("請再輸入一次");
                playerInput = Integer.parseInt(userInput);
            }
        }

        int d = playerInput / 100;
        int e = (playerInput % 100) / 10;
        int f = playerInput % 10;
        /* 第一層loop是set所有特殊情況,任一發生即進入。
         * 因判斷重複的loop還會再讓Player輸入一次,所以還是需要判斷個數的loop。
         * 第二層loop針對所有特殊情況分類處理。
         * 只用在第一次輸入時,判斷Player輸入的數字少於三個、多於三個、重複。
         *
         * 缺點:
         * 輸入同樣數字,會因前一次掉入特殊情況的loop,造成顯示不一樣的對話方塊顯示,例如:
         * 輸入1234會判斷個數多於三個,接著輸入1122,一樣會判斷多於三個;
         * 若改先輸入112會判斷重複,接著輸入1122,一樣會判斷重複。
         * 結論是同樣輸入1122,卻會因為前一次的輸入而jump不一樣的對話框,
         * 不過loop顯示出來的對話框訊息,勉強算是正確就是了。
         */
        for (; playerInput < 100 || playerInput > 1000 || d == e || d == f || e == f;) {
            for (; playerInput < 100;) {
                JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數少於三個");
                userInput = JOptionPane.showInputDialog("請再輸入一次");
                playerInput = Integer.parseInt(userInput);
            }
            for (; playerInput > 1000;) {
                JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數多於三個");
                userInput = JOptionPane.showInputDialog("請再輸入一次");
                playerInput = Integer.parseInt(userInput);
            }
            for (; d == e || d == f || e == f;) {
                JOptionPane.showMessageDialog(null, "錯誤!您輸入了重複的數字");
                userInput = JOptionPane.showInputDialog("請再輸入一次");
                playerInput = Integer.parseInt(userInput);
                d = playerInput / 100;
                e = (playerInput % 100) / 10;
                f = playerInput % 10;
            }
            d = playerInput / 100;
            e = (playerInput % 100) / 10;
            f = playerInput % 10;
        }

        //第二次以後要提示之前猜的AB數,所以用while做第二次以後的loop,直到猜中就break。
        for (; true;) {
            int scoreA = 0, scoreB = 0;
            if (a == d) {
                scoreA++;
            }
            if (b == e) {
                scoreA++;
            }
            if (c == f) {
                scoreA++;
            }
            if (a == e || a == f) {
                scoreB++;
            }
            if (b == d || b == f) {
                scoreB++;
            }
            if (c == d || c == e) {
                scoreB++;
            }

            //當數字及位置沒有全對時進入。
            if (scoreA != 3) {
                userInput = JOptionPane.showInputDialog(scoreA + " A\n" + scoreB + " B\n請再輸入三個數字");
                playerInput = Integer.parseInt(userInput);
                //先做數字個數判斷,確保個數是3個,才不會造成變數 d、e、f 出錯。
                for (; playerInput < 100 || playerInput > 1000;) {
                    for (; playerInput < 100;) {
                        JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數少於三個");
                        userInput = JOptionPane.showInputDialog("請再輸入一次\n" + scoreA + " A\n" + scoreB + " B");
                        playerInput = Integer.parseInt(userInput);
                    }
                    for (; playerInput > 1000;) {
                        JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數多於三個");
                        userInput = JOptionPane.showInputDialog("請再輸入一次\n" + scoreA + " A\n" + scoreB + " B");
                        playerInput = Integer.parseInt(userInput);
                    }
                }
                d = playerInput / 100;
                e = (playerInput % 100) / 10;
                f = playerInput % 10;
                //同上面的迴圈,只是多加了提醒Player目前是幾A幾B的訊息。
                for (; playerInput < 100 || playerInput > 1000 || d == e || d == f || e == f;) {
                    for (; userInput.length() < 3;) {
                        JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數少於三個");
                        userInput = JOptionPane.showInputDialog("請再輸入一次\n" + scoreA + " A\n" + scoreB + " B");
                        playerInput = Integer.parseInt(userInput);
                    }
                    for (; userInput.length() > 3;) {
                        JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數多於三個");
                        userInput = JOptionPane.showInputDialog("請再輸入一次\n" + scoreA + " A\n" + scoreB + " B");
                        playerInput = Integer.parseInt(userInput);
                    }
                    for (; d == e || d == f || e == f;) {
                        JOptionPane.showMessageDialog(null, "錯誤!您輸入了重複的數字");
                        userInput = JOptionPane.showInputDialog("請再輸入一次\n" + scoreA + " A\n" + scoreB + " B");
                        playerInput = Integer.parseInt(userInput);
                        d = playerInput / 100;
                        e = (playerInput % 100) / 10;
                        f = playerInput % 10;
                    }
                    d = playerInput / 100;
                    e = (playerInput % 100) / 10;
                    f = playerInput % 10;
                }
            } else {
                JOptionPane.showMessageDialog(null, "您猜對啦:)遊戲結束!");
                break;
            }
        }
    }
}