我想在Java中从右到左打印*请提供输入

时间:2015-08-22 08:12:47

标签: java

我必须解决的问题是我必须从右到左打印*。如果我给5然后第一个*下一行**等等*****必须打印,对齐必须是从右到左。

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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这是Java-8解决方案,只是为了好玩:

[TestClass()]
public class DoFooTests
{
    [TestMethod, TestCategory("Unit")]
    public void SayHelloJohn_ShouldUseServiceProxy()
    {//SETUP
        var serviceMock = new Mock<IFooBar>(MockBehavior.Loose);
        var delegateMock = new Mock<ServiceProxy.FooServiceDelegate>(MockBehavior.Strict);
        var serviceProxyMock = new Mock<IServiceProxy>(MockBehavior.Strict);
        serviceProxyMock.Setup(m => m.FooService(delegateMock.Object));
        var name = "John";
        var response = $"Hello {name}";

        //Im trying something like this (of course this does not work...)
        delegateMock.Setup(m => m.Hello(name)).Returns(response);

        //EXECUTE
        var doFoo = new DoFoo(serviceProxyMock.Object);
        var result = doFoo.SayHello(name);
        //ASSERT
        // Do some assertions here....
    }
}