2015-11-19 93 views
-3

我写过一个Java程序,但是当我运行它时,第一个字符串MWTs中有很多错误。我做了很多改变,但仍然不成功。谁能帮我?运行Java程序时出错

我的源代码的一部分:

import java.util.*; 
import java.io.*; 

class Countwords { 
    public static String [] MWTs = {"Hong Kong","New York","United Kingdom", 
    "Basic Law","Poeple's Republic of China", 
    "Hong Kong Special Adminstrative Region","Chief Excutive", 
    "Chief Justice","Chief Secretary for Adminstrative", 
    "Interpretation and General Clauses Ordinance", 
    "Hong Kong Bill of Rights","Central People's Government of the People's Republic of China", 
    "Administrative Appeals Board", "Chief Executive in Council","Executive Council" 
    "Court of Final Appeal","Court of First Instance","District Council", 
    "District Court", "District Judge","Financial Secretary", 
    "Hong Kong Government Gazette", "Gazette Extraordinary", "Special Gazette", 
    "General Holidays Ordinance", "Financial Services and the Treasury", "Government Logistics Department", 
    "Deputy Director", "Assistant Director of Health", "High Court","Ministry of Foreign Affairs", 
    "New Kowloon", "New Territories","Provisional Legislative Council", 
    "District Council", "Executive Council","Central Authorities","Hong Kong Time", 
    "Financial Secretary", "Secretary for Justice", "Secretary for the Civil Service", 
    "Secretary for Commerce, Industry and Technology","Secretary for Constitutional Affairs", 
    "Secretary for Economic Development and Labour", "Secretary for Education and Manpower", 
    "Secretary for the Environment, Transport and Works", "Secretary for Financial Services and the Treasury", 
    "Secretary for Health, Welfare and Food", "Secretary for Home Affairs", 
    "Secretary for Housing, Planning and Lands", "Secretary for Security", 
    "Permanent Secretary", "Director of Home Affairs", "Director of Administration", 
    "Deputy Secretary", "Deputy Director of Administration", "Principal Assistant Secretary", 
    "Assistant Director of Administration", "Independent Commission Against Corruption", 
    "Principal Investigator", "Operations Department", "Immigration Service", "Customs and Excise Service", 
    "Chief Superintendent", "Customs and Excise Service ","Her Majesty", "British Governmen", "Secretary of State", 
    "Privy Council", "Her Majesty in Counci", "British Dependent Territories","Channel Islands", "Isle of Man", "British Nationality Act", 
    "United Kingdom of Great Britain and Northern Ireland", 
    "Northern Ireland", "Administrative Appeals Rules", "Public Office Ordinance", "Chief Justice Bankruptcy Ordinance", 
    }; 

    public static void bubble_sort_length(String [] strs){ 
     while (true) { 
      int swaps = 0; 
      for (int i=0; i<strs.length -1; i++){ 
       if (strs[i].length() >= strs[i+1].length()) 
        continue; 
       String tmp = strs[i]; 
       strs[i] = strs[i+1]; 
       strs[i+1] = tmp; 
       swaps++; 
      } 
      if (swaps == 0) break; 
     } 
    } 
+0

你能组织更好的代码吗? – Abdelhak

+0

我试图删除元素之前,但似乎不工作。此外,它显示了“预期的标识符”和“非法类型的开始”多次为我的第一个字符串。 – hopelessprogrammer

+1

问题出在这两个字符串之间'“行政会议” “终审法院”'。逗号缺失 并在最后一个“}”缺少 –

回答

1

试试这个片断。问题在格式化&语法。

import java.util.*; 
import java.io.*; 

class Countwords { 
    public static String[] MWTs = { "Hong Kong", "New York", "United Kingdom", 
      "Basic Law", "Poeple's Republic of China", 
      "Hong Kong Special Adminstrative Region", "Chief Excutive", 
      "Chief Justice", "Chief Secretary for Adminstrative", 
      "Interpretation and General Clauses Ordinance", 
      "Hong Kong Bill of Rights", 
      "Central People's Government of the People's Republic of China", 
      "Administrative Appeals Board", "Chief Executive in Council", 
      "Executive Council", "Court of Final Appeal", 
      "Court of First Instance", "District Council", "District Court", 
      "District Judge", "Financial Secretary", 
      "Hong Kong Government Gazette", "Gazette Extraordinary", 
      "Special Gazette", "General Holidays Ordinance", 
      "Financial Services and the Treasury", 
      "Government Logistics Department", "Deputy Director", 
      "Assistant Director of Health", "High Court", 
      "Ministry of Foreign Affairs", "New Kowloon", "New Territories", 
      "Provisional Legislative Council", "District Council", 
      "Executive Council", "Central Authorities", "Hong Kong Time", 
      "Financial Secretary", "Secretary for Justice", 
      "Secretary for the Civil Service", 
      "Secretary for Commerce, Industry and Technology", 
      "Secretary for Constitutional Affairs", 
      "Secretary for Economic Development and Labour", 
      "Secretary for Education and Manpower", 
      "Secretary for the Environment, Transport and Works", 
      "Secretary for Financial Services and the Treasury", 
      "Secretary for Health, Welfare and Food", 
      "Secretary for Home Affairs", 
      "Secretary for Housing, Planning and Lands", 
      "Secretary for Security", "Permanent Secretary", 
      "Director of Home Affairs", "Director of Administration", 
      "Deputy Secretary", "Deputy Director of Administration", 
      "Principal Assistant Secretary", 
      "Assistant Director of Administration", 
      "Independent Commission Against Corruption", 
      "Principal Investigator", "Operations Department", 
      "Immigration Service", "Customs and Excise Service", 
      "Chief Superintendent", "Customs and Excise Service ", 
      "Her Majesty", "British Governmen", "Secretary of State", 
      "Privy Council", "Her Majesty in Counci", 
      "British Dependent Territories", "Channel Islands", "Isle of Man", 
      "British Nationality Act", 
      "United Kingdom of Great Britain and Northern Ireland", 
      "Northern Ireland", "Administrative Appeals Rules", 
      "Public Office Ordinance", "Chief Justice Bankruptcy Ordinance", }; 

    public static void bubble_sort_length(String[] strs) { 
     while (true) { 
      int swaps = 0; 
      for (int i = 0; i < strs.length - 1; i++) { 
       if (strs[i].length() >= strs[i + 1].length()) 
        continue; 
       String tmp = strs[i]; 
       strs[i] = strs[i + 1]; 
       strs[i + 1] = tmp; 
       swaps++; 
      } 
      if (swaps == 0) 
       break; 
     } 
    } 

    public static void main(String[] args) { 
     bubble_sort_length(MWTs); 
     System.out.println(Arrays.toString(MWTs)); 
    } 
} 
+0

你还剩下额外的逗号。 – Titus

+0

这不会影响任何错误。这是数组声明,编译器会假定数组中的最后一个元素为null –