JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 簡單的九九乘法表查詢工具
AJavaLearner





發文: 5
積分: 0
於 2018-08-22 12:09 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
國一新手寫的
如果有什麼可以提升效率的方法懇請指教


Homework.java (1.65k)


reply to postreply to post
作者 Re:簡單的九九乘法表查詢工具 [Re:AJavaLearner]
kentyeh





發文: 641
積分: 6
於 2018-08-23 01:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你的程式有了太多重覆的Code了,請善用迴圈。
另外Scanner可取得整數的輸入,若輸入有誤時,用try...catch的錯誤控制去管控即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.InputMismatchException;
import java.util.Scanner;
 
public class Homework {
 
    public static void main(String[] args) {
        System.out.println("輸入1到9即會顯示那個數字的乘法(九九乘法表)\n如要結束請輸入0");
        int x = 0;
        int y = 0;
        Scanner sc = new Scanner(System.in);
        while (x == 0 || y == 0) {
            try {
                int input = sc.nextInt();
                if (input == 0) {
                    System.out.println("已結束查詢,希望這對你有幫助\n登出中...........................................");
                    System.exit(0);
                } else if (input > 0 && input < 10) {
                    if (x == 0) {
                        x = input;
                    } else if (y == 0) {
                        y = input;
                        System.out.printf("%d × %d = %d\n", x, y, x * y);
                        x = 0;
                        y = 0;
                    }
                }else {
                    System.err.println("必須為1~9之數字,或是輸入0結束程式!");
                }
            } catch (InputMismatchException ex) {
                System.err.println("必須為1~9之數字,或是輸入0結束程式!");
                sc.next();//消耗
            }
        }
    }
}


kentyeh edited on 2018-08-23 01:25
reply to postreply to post
作者 Re:簡單的九九乘法表查詢工具 [Re:kentyeh]
AJavaLearner





發文: 5
積分: 0
於 2018-08-23 01:46 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
kentyeh
你的程式有了太多重覆的Code了,請善用迴圈。
另外Scanner可取得整數的輸入,若輸入有誤時,用try...catch的錯誤控制去管控即可wrote:



我用switch主要是因為系統不須一直去比對(我的印象中)
感謝提供想法,不然我都忘記可以使用try/catch了
只是還有一個小問題

可不可以只使用Scanner輸入一次?


reply to postreply to post
作者 Re:簡單的九九乘法表查詢工具 [Re:AJavaLearner]
clock

小時候在看國外測試機器人的影片



發文: 65
積分: 0
於 2018-08-23 05:26 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
九九乘法表有甚麼特別的秘密嗎
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 public static void main(String[] args) {
        
        System.out.println("輸入1到9即會顯示那個數字的乘法(九九乘法表)\n如要結束請輸入0");
        
        Scanner scan = new Scanner(System.in);
        String x = scan.next();
      
 
        if (x.matches("[1-9]")) {
            for (int i = 1; i <= 9; i++) {
                System.out.print(x + "*" + i + "=" + Integer.parseInt(x) * i);
            }
        }else{
              System.out.println("已離開程式");
        }
 
    }


reply to postreply to post
c:你好像很喜歡研究機械產品
X:我從小就跟博士做研究
c:哪位博士
X:南宮博士
作者 Re:簡單的九九乘法表查詢工具 [Re:clock]
AJavaLearner





發文: 5
積分: 0
於 2018-08-23 07:36 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
更精簡了XD
也學到了[a-z]這個用法

printf 還是不太懂,是像String.format()嗎?


reply to postreply to post
作者 Re:簡單的九九乘法表查詢工具 [Re:AJavaLearner]
kentyeh





發文: 641
積分: 6
於 2018-08-24 05:26 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你要一行輸入,就要識別裡面的數字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
 
public class Homework {
 
    public static void main(String[] args) {
        System.out.println("輸入1到9即會顯示那個數字的乘法(九九乘法表)\n如要結束請輸入0");
        Pattern p2n = Pattern.compile("^.*(\\d+).+(\\d+).*$");
        Scanner sc = new Scanner(System.in);
        while (true) {
            String input = sc.nextLine();
            if ("0".equals(input.trim())) {
                System.out.println("已結束查詢,希望這對你有幫助\n登出中...........................................");
                System.exit(0);
            } else {
                Matcher m = p2n.matcher(input);
                if (m.matches()) {
                    int x = Integer.parseInt(m.group(1), 10);
                    int y = Integer.parseInt(m.group(2), 10);
                    System.out.printf("%d × %d = %d\n", x, y, x * y);
                } else {
                    System.err.println("必須輸入兩個數字,或是輸入0結束程式!");
                }
            }
        }
    }
}

若是要輸入一整行運算式,就要用到中序轉後序,這裡有現成的code


reply to postreply to post
作者 Re:簡單的九九乘法表查詢工具 [Re:kentyeh]
AJavaLearner





發文: 5
積分: 0
於 2018-08-24 06:11 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有點看不太懂,什麼是Pattern?
我會盡量趕快學到那


reply to postreply to post
作者 Re:簡單的九九乘法表查詢工具 [Re:AJavaLearner]
kentyeh





發文: 641
積分: 6
於 2018-08-27 00:49 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
https://www.javaworld.com.tw/jute/post/view?bid=20&id=130126&sty=1&tpg=1&age=0

reply to postreply to post
» JWorld@TW »  Java 程式分享區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8