2015-08-22 47 views
-2

我必須解決的問題是我必須從右至左打印*。如果我給5,然後第一個*下一行**等*****必須打印,對齊必須從右到左。我想在Java中從右至左打印*請提供輸入

import java.util.Formatter; 
import java.util.Scanner; 


public class Problemfour { 
    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     int t = in.nextInt(); 
     int t1 = 1; 
     String str = "*"; 
     while (t1 <= t) { 
      for (int i = 1; i <= t1; i++) { 
       System.out.print(str); 
      } 
      System.out.println(""); 
      t1 += 1; 
     } 
    } 
} 
+0

這看起來像從課件的分配。做你的功課;-) – greut

回答

1

這裏是Java的解決方案8,只是爲了好玩:

public static void main(String[] args) { 
    int t = new Scanner(System.in).nextInt(); 
    IntStream.rangeClosed(1, t) 
      .mapToObj(n -> 
       String.join("", Collections.nCopies(t - n, " ")) + 
       String.join("", Collections.nCopies(n, "#"))) 
      .forEach(System.out::println); 
} 
+0

偉大的,它爲我工作,非常感謝。 – krishna