I';我对java完全陌生,想知道是否有任何方法可以改进这个程序
该计划的目标是采取一个字符串,并创建一个金字塔形状使用的字母类似于你可以经常看到你管。程序首先询问用户要转换的字符串,然后询问他们是否要忽略空白,如果他们决定忽略空白,则在程序减/加字符时将忽略空白。我已经编写了大约一周的代码,还没有学会如何正确创建类。任何关于如何使用更高级的编码来改进我的代码的建议都将不胜感激I';我对java完全陌生,想知道是否有任何方法可以改进这个程序,java,Java,该计划的目标是采取一个字符串,并创建一个金字塔形状使用的字母类似于你可以经常看到你管。程序首先询问用户要转换的字符串,然后询问他们是否要忽略空白,如果他们决定忽略空白,则在程序减/加字符时将忽略空白。我已经编写了大约一周的代码,还没有学会如何正确创建类。任何关于如何使用更高级的编码来改进我的代码的建议都将不胜感激 import java.util.Scanner; public class Main { public static void main(String[] args)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
String fls = "FALSE";
String tr = "TRUE";
Scanner stringWord = new Scanner(System.in);
System.out.print("Enter the string you wish to convert: ");
String example = stringWord.nextLine();
Scanner whiteSpaceOrNo = new Scanner(System.in);
System.out.print("\nTrue or false, you want to ignore whitespace: ");
String whiteSpace = whiteSpaceOrNo.nextLine();
System.out.print("\n");
if(whiteSpace.toUpperCase().equals(fls))
{
for(int i = 0; i < example.length()+1; i++)
{
if(i < example.length())
{
System.out.println(example.substring(0, i + 1));
}
else
{
for(int f = example.length(); f > 0; f--)
{
System.out.println(example.substring(0, f-1));
}
}
}
}
else if (whiteSpace.toUpperCase().equals(tr))
{
for(int d = 0; d < example.length(); d++)
{
if(example.charAt(d) != ' ')
{
System.out.println(example.substring(0, d+1));
}
if(d == example.length() - 1)
{
for(int x = example.length() - 1; x >= 0; x--)
{
if(example.charAt(x) != ' ')
{
System.out.println(example.substring(0, x+1));
}
}
}
}
}
else
{
System.out.println("Incorrect response, restart program");
}
}
}
import java.util.Scanner;
公共班机
{
公共静态void main(字符串[]args)
{
字符串fls=“FALSE”;
字符串tr=“TRUE”;
Scanner stringWord=新扫描仪(System.in);
System.out.print(“输入要转换的字符串:”);
字符串示例=stringWord.nextLine();
扫描仪whiteSpaceOrNo=新扫描仪(System.in);
System.out.print(“\n如果要忽略空格,请单击“ue”或“false”);
字符串whiteSpace=whiteSpaceOrNo.nextLine();
系统输出打印(“\n”);
if(whiteSpace.toUpperCase().equals(fls))
{
对于(int i=0;i0;f--)
{
System.out.println(例如,子字符串(0,f-1));
}
}
}
}
else if(whiteSpace.toUpperCase().equals(tr))
{
对于(int d=0;d=0;x--)
{
if(例如,字符(x)!=“”)
{
System.out.println(例如,子字符串(0,x+1));
}
}
}
}
}
其他的
{
System.out.println(“错误响应,重新启动程序”);
}
}
}
建议的改进:
i
结尾的i循环之后运行(在第一部分中,不忽略空格)建议的改进:
i
结尾的i循环之后运行(在第一部分中,不忽略空格)如果这段代码(真的)有效,那么它可能是codereview.stackexchange.com的候选代码。这个问题对于StackOverflow来说“太宽泛了”。将一个程序转换成如此简单易用的类可能是不必要的。首先,当要求你的程序时,你的程序不会忽略空格。为了简化操作,您可以使用
if(removeWhiteSpace)示例.replaceAll(“\\s”,”)
其中removeWhiteSpace
应该是一个布尔值
,而不是字符串
。如果此代码(真的)有效,那么它可能是codereview.stackexchange.com的候选代码。这个问题对于StackOverflow来说“太宽泛了”。将一个程序转换成如此简单易用的类可能是不必要的。首先,当要求你的程序时,你的程序不会忽略空格。为了简化操作,您可以使用if(removeWhiteSpace)示例.replaceAll(“\\s”,”)
其中removewitespace
是一个布尔值
,而不是字符串
。