[英]How do I make something happen on hover of a row in a JavaFX TableView?
[英]How do I make this output happen in java?
我是一個初學者,我想使用for循環和下標輸出以下內容,但不確定。
輸出:
Jamaica
amaica
maica
aica
ica
ca
a
為了實現此輸出,我該怎么辦?
第一:您需要循環生成n
行,這是數組的長度。
第二:您需要打印與row - 1
相同的空格row - 1
次。
第二:您需要從row - 1
開始打印字符row - 1
數字到字符串的長度。
最終的解決方案將是:
public class MyClass {
public static void printStr(String str) {
int i,j;
for (i = 0; i < str.length();i++) {
for(j = 0; j < i; j++) {
System.out.print(" ");
}
for(j = i; j < str.length();j++) {
System.out.print(str.charAt(j));
}
System.out.println("");
}
}
public static void main(String args[]) {
MyClass.printStr("Jamaica");
}
}
我將使用兩個正則表達式 ,第一個在String
充滿空白時終止循環。 第二個將循環正文中的第一個非空白字符替換為空白(在打印當前String
值之后)。 並且,如果String
可能為空,則應注意這一點。 喜歡,
String s = "Jamaica";
if (!s.isEmpty()) {
while (!s.matches("\\s+")) {
System.out.println(s);
s = s.replaceFirst("\\S", " ");
}
}
輸出(根據要求)
Jamaica
amaica
maica
aica
ica
ca
a
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next(); //input through scanner class
int len = s.length();
for(int i=0;i<len;i++){
for(int j=0;j<i;j++){
System.out.print(" ");
}
for(int j=i;j<len;j++){
System.out.print(s.charAt(j));
}
System.out.println("");
}
}
希望有幫助
嘗試以下代碼:
StringBuilder country = new StringBuilder("Jamaica");
for(int i=0; i< country.length();i++){
if(i > 0)
{
for(int j=0;j<i;j++){
country.setCharAt(j,' ');
}
}
System.out.println(country);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.