簡體   English   中英

使用構造函數的java莫爾斯電碼problem

[英]java morse code probelm using constructors

我有一個使用構造函數的 java 莫爾斯電碼賦值。 我讓代碼工作但很難調用構造函數。

  1. 我的原始代碼。
import java.util.Scanner;

public class morseTest {

    public static void main(String[] args) {


    char alphabet[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
    'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
    'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
    ',', '.', '?' };
    

    String morse[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 
            ".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
            "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
            "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
            "-----", "--..--", ".-.-.-", "..--.." };    



    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine().toLowerCase();    
    
    char[] chars = input.toCharArray();

    String str = "";
    for (int i = 0; i < chars.length; i++) {
        for (int index = 0; index < alphabet.length; index++) {
            if (alphabet[index] == chars[i]) {
                str = str + morse[index] + " ";
            }
        } 
    }
    System.out.println(str);
    sc.close();
    }
    
}
  1. 我必須從 Demo.java 的 MorseCode.java 調用並實現一個方法 stringToMorse()。
import java.util.Scanner;
import java.util.*;

public class Demo {
    public static void main(String[] args) {
        // PLACE CODE HERE
        
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine().toLowerCase();

        MorseCode message = new MorseCode(input);

        System.out.println(message.stringToMorse());
        
    }
}
import java.util.Scanner;

public class MorseCode {
    private static char[] alphabet;
    private static String[] morse;
    private static String input;

    public MorseCode() {
        // PLACE CODE HERE

        char alphabet[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
        'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
        'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
        ',', '.', '?' };
     

        String morse[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 
                ".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
                "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
                "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
                "-----", "--..--", ".-.-.-", "..--.." };              
    }
    
    MorseCode(String s) {
        input = s;
    }

    public static String stringToMorse(String s) {
         // PLACE CODE HERE

         char[] chars = input.toCharArray();

         String str = "";
         for (int i = 0; i < chars.length; i++) {
             for (int index = 0; index < alphabet.length; index++) {
                 if (alphabet[index] == chars[i]) {
                     str = str + morse[index] + " ";
                 }
             } 
         }
         return s;
    } 

編譯時,我收到錯誤消息

Demo.java:13: 錯誤:MorseCode 類中的方法 stringToMorse 不能應用於給定類型; System.out.println(message.stringToMorse()); ^ 要求:找到的字符串:無參數原因:實際和形式參數列表長度不同 1 錯誤

public static String stringToMorse(String s)這個方法需要一個參數。

在調用message.stringToMorse() ,沒有傳遞給它的參數。

此外,在代碼中 stringToMorse 方法被聲明為靜態方法,因此可以通過 className 調用它。 請參閱下面的代碼更改。

public class Demo {
public static void main(String[] args) {
    // PLACE CODE HERE
    
    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine().toLowerCase();

    System.out.println(MorseCode.stringToMorse(input));
    
   }
}

MorseCode 類的代碼在 stringToMorse 方法中有錯誤。 它正在返回未在任何地方聲明的變量s

import java.util.Scanner;

public class MorseCode {
private static char[] alphabet ={ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
',', '.', '?' };


 private static String[] morse= { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 
        ".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
        "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
        "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
        "-----", "--..--", ".-.-.-", "..--.." };              
    

public static String stringToMorse(String input) {
     // PLACE CODE HERE

     char[] chars = input.toCharArray();

     String str = "";
     for (int i = 0; i < chars.length; i++) {
         for (int index = 0; index < alphabet.length; index++) {
             if (alphabet[index] == chars[i]) {
                 str = str + morse[index] + " ";
             }
         } 
     }
     return str;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM