Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何循环这些if语句?_Java_Loops_If Statement - Fatal编程技术网

Java 如何循环这些if语句?

Java 如何循环这些if语句?,java,loops,if-statement,Java,Loops,If Statement,我对编程非常陌生,当不使用整数时,我很难理解循环。我正在尝试制作一个英文到摩尔斯电码翻译程序,目前为止已经完成了: import java.util.Scanner; 公共级英语莫尔斯语{ public static void main(String[] args) { System.out.println("Please enter text you wish to convert to Morse Code."); Scanner sc =new Scanner(Sy

我对编程非常陌生,当不使用整数时,我很难理解循环。我正在尝试制作一个英文到摩尔斯电码翻译程序,目前为止已经完成了:

import java.util.Scanner;
公共级英语莫尔斯语{

public static void main(String[] args) {

     System.out.println("Please enter text you wish to convert to Morse Code.");
     Scanner sc =new Scanner(System.in);
     String english = sc.next();


         if (english.equalsIgnoreCase("A")){
             System.out.print(".- ");
         }
         if (english.equalsIgnoreCase("B")){
             System.out.print("-... ");
         }
         if (english.equalsIgnoreCase("C")){
             System.out.print("-.-. ");
         }
         if (english.equalsIgnoreCase("D")){
             System.out.print("-.. ");
         }
         if (english.equalsIgnoreCase("E")){
             System.out.print(". ");
         }
         if (english.equalsIgnoreCase("F")){
             System.out.print("..-. ");
         }
         if (english.equalsIgnoreCase("G")){
             System.out.print("--. ");
         }
         if (english.equalsIgnoreCase("H")){
             System.out.print(".... ");
         }
         if (english.equalsIgnoreCase("I")){
             System.out.print(".. ");
         }
         if (english.equalsIgnoreCase("J")){
             System.out.print(".--- ");
         }
         if (english.equalsIgnoreCase("K")){
             System.out.print("-.- ");
         }
         if (english.equalsIgnoreCase("L")){
             System.out.print(".-.. ");
         }
         if (english.equalsIgnoreCase("M")){
             System.out.print("-- ");
         }
         if (english.equalsIgnoreCase("N")){
             System.out.print("-. ");
         }
         if (english.equalsIgnoreCase("O")){
             System.out.print("--- ");
         }
         if (english.equalsIgnoreCase("P")){
             System.out.print(".--. ");
         }
         if (english.equalsIgnoreCase("Q")){
             System.out.print("--.- ");
         }
         if (english.equalsIgnoreCase("R")){
             System.out.print(".-. ");
         }
         if (english.equalsIgnoreCase("S")){
             System.out.print("... ");
         }
         if (english.equalsIgnoreCase("T")){
             System.out.print("- ");
         }
         if (english.equalsIgnoreCase("U")){
             System.out.print("..- ");
         }
         if (english.equalsIgnoreCase("V")){
             System.out.print("...- ");
         }
         if (english.equalsIgnoreCase("W")){
             System.out.print(".-- ");
         }
         if (english.equalsIgnoreCase("X")){
             System.out.print("-..- ");
         }
         if (english.equalsIgnoreCase("Y")){
             System.out.print("-.-- ");
         }
         if (english.equalsIgnoreCase("Z")){
             System.out.print("--.. ");
         }
         if (english.equalsIgnoreCase("1")){
             System.out.print(".---- ");
         }
         if (english.equalsIgnoreCase("2")){
             System.out.print("..--- ");
         }
         if (english.equalsIgnoreCase("3")){
             System.out.print("...-- ");
         }
         if (english.equalsIgnoreCase("4")){
             System.out.print("...- ");
         }
         if (english.equalsIgnoreCase("5")){
             System.out.print("..... ");
         }
         if (english.equalsIgnoreCase("6")){
             System.out.print("-.... ");
         }
         if (english.equalsIgnoreCase("7")){
             System.out.print("--... ");
         }
         if (english.equalsIgnoreCase("8")){
             System.out.print("---.. ");
         }
         if (english.equalsIgnoreCase("9")){
             System.out.print("----. ");
         }
         if (english.equalsIgnoreCase("0")){
             System.out.print("----- ");
         }


}
}


在多次尝试不同的LOP和条件后,我无法克服我的程序只能翻译一个字符的问题。如果输入多个,它将终止。如果有人能引导我找到正确的途径,找出如何循环这些If语句,直到所有字符都被翻译,我将不胜感激。

一旦成功检索到用户输入的单词,您就可以对该单词进行迭代。看一看。根据文件,它

将此字符串转换为新的字符数组

因此,您的代码如下所示:

for (char c : english.toCharArray()){
    if (c == 'A'){
        System.out.print(".- ");
    }else if (c == 'B'){
        //etc...
    }
}
注意以下几点:

for (char c : english.toCharArray()){
    if (c == 'A'){
        System.out.print(".- ");
    }else if (c == 'B'){
        //etc...
    }
}
  • 您不需要在
    char c
    上调用
    .equals
    ,因为它是一个原语
  • 此实现使用增强的for循环。你可以阅读更多关于他们的信息

一旦成功检索到用户输入的单词,就可以对该单词进行迭代。看一看。根据文件,它

将此字符串转换为新的字符数组

因此,您的代码如下所示:

for (char c : english.toCharArray()){
    if (c == 'A'){
        System.out.print(".- ");
    }else if (c == 'B'){
        //etc...
    }
}
注意以下几点:

for (char c : english.toCharArray()){
    if (c == 'A'){
        System.out.print(".- ");
    }else if (c == 'B'){
        //etc...
    }
}
  • 您不需要在
    char c
    上调用
    .equals
    ,因为它是一个原语
  • 此实现使用增强的for循环。你可以阅读更多关于他们的信息
    • 我会用这个

      char[] ca = english.toCharArray();
      
      然后你可以循环

      for (char c : ca) {
       //... your code        
      }
      
      为了进一步改进,在c语言上使用开关语法

      是的

      我会用这个

      char[] ca = english.toCharArray();
      
      然后你可以循环

      for (char c : ca) {
       //... your code        
      }
      
      为了进一步改进,在c语言上使用开关语法

      是的


      使用
      HashMap
      表示翻译:

      HashMap<Character , String> char_to_morse = new HashMap<>();
      char_to_morse.put('A' , ".- ");
      //put all other chars in the map here
      
      //retrieving a code:
      System.out.print(char_to_morse.get(someChar));
      
      HashMap char_to_morse=new HashMap();
      char_to_morse.put('A',“-”);
      //把地图上的其他所有字符都放在这里
      //检索代码:
      System.out.print(char_to_morse.get(someChar));
      
      使用
      HashMap
      表示翻译:

      HashMap<Character , String> char_to_morse = new HashMap<>();
      char_to_morse.put('A' , ".- ");
      //put all other chars in the map here
      
      //retrieving a code:
      System.out.print(char_to_morse.get(someChar));
      
      HashMap char_to_morse=new HashMap();
      char_to_morse.put('A',“-”);
      //把地图上的其他所有字符都放在这里
      //检索代码:
      System.out.print(char_to_morse.get(someChar));
      
      我想这就是你想要的:

      Scanner sc = new Scanner(System.in);
      String english;
      
      while (sc.hasNext()){     //While there is another token in Scanner
         english = sc.next();   //Give the next token to english variable
      
         // Your code with the ifs
      
      }
      

      我想这就是你想要的:

      Scanner sc = new Scanner(System.in);
      String english;
      
      while (sc.hasNext()){     //While there is another token in Scanner
         english = sc.next();   //Give the next token to english variable
      
         // Your code with the ifs
      
      }
      

      您正在尝试比较整个字符串。如果字符串是“hello”,则应逐个翻译其中的每个字母-首先是
      h
      ,然后是
      e
      ,依此类推。为此,您需要从零索引字母开始,然后转到1索引字母,依此类推。你知道在给定索引下给出字符串长度和字符长度的方法吗?不确定,但如果你能解释的话,那就太好了!您正在尝试比较整个字符串。如果字符串是“hello”,则应逐个翻译其中的每个字母-首先是
      h
      ,然后是
      e
      ,依此类推。为此,您需要从零索引字母开始,然后转到1索引字母,依此类推。你知道在给定索引下给出字符串长度和字符长度的方法吗?不确定,但如果你能解释的话,那就太好了!这个答案+保罗的答案将是最好的组合:)我如何让原语c忽略输入的大小写?这个答案+保罗的答案将是最好的组合:)我如何让原语c忽略输入的大小写?