【问题标题】:Problems at detecting spacebars at Strings在字符串中检测空格键的问题
【发布时间】:2017-01-14 15:49:02
【问题描述】:

所以我正在做一个接受用户输入的程序,当它找到与数字相似的字符时,它会用数字替换它。 (例如,它将 O 替换为 0,e 替换为 3,等等)问题是,当它找到一个空白区域时,一切都搞砸了。您可以通过编译代码来检查输出是否完全混乱。

/* Program to encrypt text replacing some letters by similar numbers
Done by: Gabriel Mello
*/
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String input; //Allocating space for user input
    char[] output=new char[100000]; //Allocating space for final output
    while(true){ // Lets it work as many times as wished


     System.out.println("Escribí la frase que quieras transformar"); // Spanish for input your frase
     input=sc.next(); //Takes user input

         for(int i=0; i<=input.length()-1;i++){ //Iterates over every char in the input
                     switch(input.charAt(i)){//Checks wether the current digit is valid for replacement,
                     case 'O':                    // if it is, it replaces it, if not, it leaves it as it is.
                     case 'o': output[i]='0';
                     break;
                     case 'L':
                     case 'l': 
                     case 'I':
                     case 'i': output[i]='1';
                     break;
                     case 'Z':
                     case 'z': output[i]='2';
                     break;
                     case 'E':
                     case 'e': output[i]='3';
                     break;
                     case 'A':
                     case 'a': output[i]='4';
                     break;
                     case 'S':
                     case 's': output[i]='5';
                     break;
                     case 'G':
                     case 'g': output[i]='6';
                     break;
                     case 'T':
                     case 't': output[i]='7';
                     break;
                     case 'B':
                     case 'b': output[i]='8';
                     break;
                     case 'P':
                     case 'p': output[i]='9';
                     break;
                     default: output[i]=input.charAt(i);
                     }

         }

             System.out.println(output); //Prints the output
             for(int i=0;i<=output.length-1;i++){ //Resets the output array
                 output[i]=' ';
             }
    }


}
}

【问题讨论】:

  • 在您的问题中添加示例输入和错误输出
  • 您的代码打印了几行新行,这使得问题难以解决。尝试将 System.out.println(output) 移出您的 while 循环。
  • while 循环只是让用户可以多次使用该程序,而不必一遍又一遍地运行它。忽略它
  • 您是否尝试过使用调试器单步执行代码并检查遇到空格时发生了什么?
  • 尝试使用 Eclipse 的调试器,不知道它是如何工作的,它似乎只是以相同的方式运行代码

标签: java arrays string encryption spaces


【解决方案1】:

扫描仪输入在空格处拆分。因此,如果您输入12 34,您的代码看到的第一个输入是12。它通过for(int i=0; i&lt;=input.length()-1;i++) 循环运行,然后while 循环找到另一个输入34 并再次运行它。请参阅 Java 文档:

扫描器使用分隔符模式将其输入分解为标记,默认情况下匹配空格。

input=sc.next() 替换为input=sc.nextLine() 以修复它。

【讨论】:

  • the while loop finds another input "34" and again runs throu it. 是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多