0
我正在設計一個應用程序,我應該採取一系列的字符串和中斷號碼。我對此很新,所以我對如何循環和「斷開」字符串的邏輯感到困惑。我在runner類「String index out of range:0(in java.lang.String)」中遇到了一個運行時錯誤,因爲它突出顯示了我的代碼的最後一行(while perimeter)。斷路器應用程序
的輸入的實施例將是:
ħE的L- L O,W 0 R L d
的輸出的實施例將是
HEL
低
WOR
LD
到目前爲止我的代碼是
import java.util.Scanner;
public class LineBreaker
{
private String line;
private int breaker;
public LineBreaker()
{
this("",0);
}
public LineBreaker(String s, int b)
{
s = "";
b = 0;
}
public void setLineBreaker(String s, int b)
{
line = s;
breaker = b;
}
public String getLineBreaker(String s, int b)
{
String box = "";
for(int i = 0; i < s.length() - 1; i++)
{
if(i == s.charAt(b))
{
System.out.println();
}
}
return box;
}
}
我的亞軍類:
import java.util.Scanner;
public class LineBreakerRunner
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
String choice = "";
do{
System.out.println("Enter a group of characters please.");
String characters = keyboard.nextLine();
System.out.println("Enter an integer please.");
int number = keyboard.nextInt();
LineBreaker test = new LineBreaker();
test.getLineBreaker(characters,number);
System.out.println("Do you want to enter more data?(Y/N)");
choice = keyboard.nextLine();
}while(choice.charAt(0) == 'Y'||choice.charAt(0) == 'y');
}
}
任何幫助將不勝感激,感謝您的時間!
使用'substring'在'字符串'類以及'for'循環。另外,班上的變量's'和'b'在哪裏?看起來你不太理解構造函數。 –