Re: [問題] 檢查保留字的程式
※ 引述《nothao (hao)》之銘言:
: 因為不知道指令是哪一個,所以我就用一個類別來做為檢查的方式
: import javax.swing.JOptionPane;
: public class test{
: public static void main(String[] args){
: String box =JOptionPane.showInputDialog("請輸入字串");//box裝初始字串
: String box1;//box1裝檢察字串
: text t1 =new text();
: int num;
: num = box.indexOf(' ');//檢查空白
: int num1 =0 ;
: if(num != -1){
: while( num != -1){
: box1=box.substring(num1,num);
: t1.text(box1);
: num1=num + 1 ;
: num = box.indexOf(num1,' ');
: }
: }
: else{
: t1.text(box);
: }
: }
: }
: class text(){
^^這裡沒有括弧。
: String[] array = {"abstract","do","implements","protected","throws",
^^你底下的方法是static的,這裡卻不是static,你方法裡頭根本呼叫不到他。
: "boolean","double","import","public","transient","break","else","instanceof",
: "return","true","byte","extends","int","short","try","case","false","interface",
: "static","void","catch","final","long","strictfp","volatile","char","finally",
: "native","super","while","class","float","new","switch","const","for","null",
: "synchronized","continue","goto","package","this"};
: public static text(String x){
^^^^^^方法名稱與class名稱相同,而且沒有回傳型態。
即使是void,你還是宣告回傳為void。
: int j = 0;
: while(j <= 53){
: if(x.equals(array[j])
^少了一個括弧。
: System.out.println("字串"+ x + "為保留字");
: else
: j++;
: }
: System.out.print("沒有保留字");
: }
: }
: 卻出現錯誤訊息
: t1.text(box1); symbol:method text(java.lang.String) location:class text
: 所以很好奇是哪邊的語法寫錯了…
如果是C#的話,static建構子是沒有引數(參數)且不可能為外部呼叫的。
那是CLR在 class Loading的時候由class loader去做的,你不能操縱它。
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.85.116.116
※ 編輯: zanyking 來自: 210.85.116.116 (04/10 01:27)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 16 之 17 篇):