Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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 while循环不工作_Java_Arrays_String_Loops_While Loop - Fatal编程技术网

Java while循环不工作

Java while循环不工作,java,arrays,string,loops,while-loop,Java,Arrays,String,Loops,While Loop,该程序允许用户输入一个短语并将其转换为ROT13,在ROT13中,输入的每个英文字母在其后面的13个位置变为字母(a变为N)。当输入1个字符时,我的当前代码可以工作,但是我需要它在代码中运行字符数。我试着在开始时加入一个while循环,但它似乎不起作用。为什么会这样 import java.io.*; public class J4_1_EncryptionErasetestCNewTry { public static void main (String [] args) thro

该程序允许用户输入一个短语并将其转换为ROT13,在ROT13中,输入的每个英文字母在其后面的13个位置变为字母(a变为N)。当输入1个字符时,我的当前代码可以工作,但是我需要它在代码中运行字符数。我试着在开始时加入一个while循环,但它似乎不起作用。为什么会这样

import java.io.*;

public class J4_1_EncryptionErasetestCNewTry
{

    public static void main (String [] args) throws IOException
    {
        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed 

        String key [] = {"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"};
        String keyA [] = {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"};

        System.out.println("Enter a phrase: ");
        String phrase = myInput.readLine();

        int length = phrase.length();
        int y = 0, i = 0, num = 0;

        while (y <= length) {
            String letter = Character.toString(phrase.charAt(y));
            y++;
            while(!(letter.equals(key[i]))){
                i++;
            }
            num = i;
            System.out.println(keyA[num]);
            y++;
        }
    }
}
import java.io.*;
公共类J4_1_加密测试设备
{
公共静态void main(字符串[]args)引发IOException
{
BufferedReader myInput=new BufferedReader(new InputStreamReader(System.in));//Buffered Reader读取输入的数字
字符串键[]={“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”};
字符串keyA[]={“N”、“O”、“P”、“Q”、“R”、“S”、“T”、“U”、“V”、“W”、“X”、“Y”、“Z”、“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”、“I”、“J”、“K”、“L”、“M”};
System.out.println(“输入短语:”);
字符串短语=myInput.readLine();
int length=短语.length();
int y=0,i=0,num=0;

虽然(y我以不同的方式实现了它,但它的工作原理与您预期的一样,仅适用于您的示例中的大写字母:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class WhileLoopIssue {

    public static void main( String[] args ) throws IOException {
        BufferedReader myInput = new BufferedReader( new InputStreamReader(
                System.in ) );// Buffered Reader reads the
                              // number inputed

        final List<String> letterList = Arrays.asList( "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" );

        System.out.println( "Enter a phrase: " );
        String phrase = myInput.readLine();

        final String[] letters = phrase.split( "" );     // Split input phrase
        final StringBuffer buffer = new StringBuffer();  // Variable to save letters. Could be a String as well.
        for ( int i = 0; i < letters.length; i++ ) {
            final int letterIndex = letterList.indexOf( letters[i] );  // Get the numeric value of the letter
            if ( letterIndex < 0 )  // Skip iteration if not found. Maybe a lowercase, or an empty String
                continue;

            final int nextLetterIndex = 13 + letterIndex;   // Actual value of the letter + 13
            if ( nextLetterIndex > letterList.size() ) {
                buffer.append( nextLetterIndex % letterList.size() );  // Actual value greater than the total number of letters in the alphabet, so we get the modulus for the letter
            } else {
                buffer.append( letterList.get( nextLetterIndex ) );  // Letter in the range, get it
            }
        }
        System.out.println( buffer.toString() );
    }
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.array;
导入java.util.List;
公共类whileloop问题{
公共静态void main(字符串[]args)引发IOException{
BufferedReader myInput=新的BufferedReader(新的InputStreamReader(
System.in));//缓冲读取器读取
//输入的数字
最终列表letterList=array.asList(“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”);
System.out.println(“输入短语:”);
字符串短语=myInput.readLine();
最终字符串[]字母=短语。拆分(“”;//拆分输入短语
final StringBuffer=new StringBuffer();//用于保存字母的变量。也可以是字符串。
for(int i=0;iletterList.size()){
buffer.append(nextLetterIndex%letterList.size());//实际值大于字母表中的字母总数,因此我们得到字母的模数
}否则{
buffer.append(letterList.get(nextLetterIndex));//范围内的字母,获取它
}
}
System.out.println(buffer.toString());
}
}

你的代码很可能会在你的内部while循环中中断,因为你没有重置
i
的值。如果不这样做,你会碰到StringIndexOutOfBounds。我建议在你的外部while循环中初始化
i
,或者干脆在外部while循环中移动
int i=0;

你需要重新设置在每次迭代中t i。可能会在数组“key”的末尾找到第一个字母。您的代码将在那里找到下一个输入字符,我猜这不是您想要的,并且不会找到该字符并抛出SIOBException。我已经更改了while循环,还删除了变量y中的两次增量。请看

    while (y < length) {
        i = 0; //Every Time you want to search from start of the array 
                //so just reset the i.
        String letter = Character.toString(phrase.charAt(y));
        while(!(letter.equals(key[i]))){
            i++;
        }
        num = i;
        System.out.println(keyA[num]);
        y++;
    }
while(y
我假设您输入的是一个只有大写字母的短语,否则您将遇到SIOBException,因为您将无法在数组中找到该字母

顺便说一句,您应该使用一些其他数据结构,而不是那些数组,这些数据结构可以有效地进行搜索,如hashmap。您在数组中的线性搜索没有得到优化。

请参阅代码注释

public static void main(String[] args) {

        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed 

        String key [] = {"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"};
        String keyA [] = {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"};

        System.out.println("Enter a phrase: ");
        String phrase = "";

        try {
            phrase = myInput.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        int length = phrase.length();
        int y = 0, i = 0, num = 0;

        while (y < length) { // This should be y < length. Otherwise, it would throw a StringIndexOutOfBoundsException.
            i=0; // Re-initialize
            String letter = Character.toString(phrase.charAt(y));
//            y++; // Unecessary incremental
            while(!(letter.equalsIgnoreCase(key[i]))){
                i++;
            }
            num = i;
            System.out.print(keyA[num]);
            y++;
        }

    }
publicstaticvoidmain(字符串[]args){
BufferedReader myInput=new BufferedReader(new InputStreamReader(System.in));//Buffered Reader读取输入的数字
字符串键[]={“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”};
字符串keyA[]={“N”、“O”、“P”、“Q”、“R”、“S”、“T”、“U”、“V”、“W”、“X”、“Y”、“Z”、“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”、“I”、“J”、“K”、“L”、“M”};
System.out.println(“输入短语:”);
字符串短语=”;
试一试{
phrase=myInput.readLine();
}捕获(IOE异常){
e、 printStackTrace();
}
int length=短语.length();
int y=0,i=0,num=0;
while(y
虽然这并不能解决您的问题,但它符合您的意图:

public static String rot13(String s) {
    String r = "";
    for (byte b : s.getBytes())
        r += (char)((b + 13 - 'A') % 26 + 'A');
    return r;
}
你的代码太复杂了,无法完成它的工作。实际上,所有的工作都可以在一行中完成。使用字节算法而不是数组查找等。简单/更少的代码总是最好的方法

请不要评论效率低下等问题。这是b