String 如何解决此代码中第一次执行后终止的while循环?

String 如何解决此代码中第一次执行后终止的while循环?,string,String,我最近一直在研究一个字符串编码器,它将一个句子转换成代码,每个数字代表字母表中字母的位置,空格为0。它有一个while循环,可以在第一次执行后重新启动代码,但在输入1之后,它会要求输入一个字符串,但在我可以这样做之前,它会要求再次重新启动 import java.util.*; class String_coder { public static void main (String args[]) { String abc = " abcdefghijklmnop

我最近一直在研究一个字符串编码器,它将一个句子转换成代码,每个数字代表字母表中字母的位置,空格为0。它有一个while循环,可以在第一次执行后重新启动代码,但在输入1之后,它会要求输入一个字符串,但在我可以这样做之前,它会要求再次重新启动

import java.util.*;
class String_coder
{
    public static void main (String args[])
    {
        String abc = " abcdefghijklmnopqrstuvwxyz";int restart = 1, k;
        Scanner sc = new Scanner(System.in);

        //creates an array of every letter in abc
        char ABC[] = new char[abc.length()];
        for (k = 0; k < abc.length(); k++)  
        {
            ABC[k] = abc.charAt(abc.length() - (k+1)); //enters each letter into the array in order
        } 

        //Introduction
        System.out.println("Alkalyne Systems String Coder");
        System.out.println();
        System.out.println("Enter any sentence and convert it to a code");

        while(restart==1)
        {
            int i = 0, j = 0, l = 0, alp = 0, abcPos = 0;
            String a = " ";

            //Inputs a String
            System.out.println();
            System.out.println("Enter a string");
            a = sc.nextLine();

            a = a.toLowerCase(); //Converts to lower case

            //creates an array of every character in the String
            char aConv[] = new char[a.length()];
            for (i = 0; i < a.length(); i++)  
            {
                aConv[i] = a.charAt(a.length() - (i+1)); //enters each letter into the array in order
            } 

            //Compares each letter in aConv with letter in abc to find position
            System.out.print("|");
            for (alp = a.length()-1;alp >=0;alp--)
            {
                for (abcPos = abc.length()-1; abcPos >= 0;abcPos--)
                {
                    if (aConv[alp] == ABC[abcPos])
                    {
                        System.out.print(abc.length() - (abcPos + 1));
                        System.out.print("|");
                    }
                }
            }
            System.out.println();

            //restart
            System.out.println("Would you like to restart?");
            System.out.println("1 - Yes|0 - No");
            restart = sc.nextInt();     
        }

        //Exit Message
        System.out.println("Thank you for using String Coder");
        System.out.println("Have a wonderful day!");

    }
}
import java.util.*;
类字符串编码器
{
公共静态void main(字符串参数[])
{
字符串abc=“abcdefghijklmnopqrstuvxyz”;int restart=1,k;
扫描仪sc=新的扫描仪(System.in);
//创建abc中每个字母的数组
字符ABC[]=新字符[ABC.length()];
对于(k=0;k=0;alp--)
{
对于(abcPos=abc.length()-1;abcPos>=0;abcPos--)
{
if(aConv[alp]==ABC[abcPos])
{
系统输出打印(abc.length()-(abcPos+1));
系统输出打印(“|”);
}
}
}
System.out.println();
//重新启动
System.out.println(“是否要重新启动?”);
System.out.println(“1-是| 0-否”);
重新启动=sc.nextInt();
}
//退出消息
println(“感谢您使用字符串编码器”);
System.out.println(“祝你有美好的一天!”);
}
}

您是否调试了代码,以查看在第一次循环运行后重新启动中放置的内容?您是否考虑过使用
hasNextInt()
检查是否存在.a整数?。您正在使用
sc.nextInt()
从扫描仪读取
restart
的值,我想您正在以RETURN结束输入。此返回用于确定整数的结尾,但不会从扫描仪中使用。因此,当要求您输入一个新字符串时,您可以使用
sc.newLine()
读取该字符串,该字符串将检索所有数据,直到下一个出现的换行符。但是,这已经在扫描程序中等待了前面数字的输入,因此接受一个空字符串作为inputhYou调试代码以查看在第一次循环运行后重新启动中放置了什么?您是否考虑过使用
hasNextInt()
检查是否存在.a整数?。您正在使用
sc.nextInt()
从扫描仪读取
restart
的值,我想您正在以RETURN结束输入。此返回用于确定整数的结尾,但不会从扫描仪中使用。因此,当要求您输入一个新字符串时,您可以使用
sc.newLine()
读取该字符串,该字符串将检索所有数据,直到下一个出现的换行符。但是,这已经在扫描仪中等待前面数字的输入,因此接受一个空字符串作为输入