现在我正在尝试使用swift 4打印以下星形图案。
1
12
123
1234
为此我采取了以下方法。
for i in stride(from: 1, to: 5, by: 1) {
// prints from 1 to 4
for j in stride(from: 1, to: i, by: 1) {
print(j, separator: "", terminator: "")
}
print("*\n")
}
但是现在这将打印在输出之下。
*
1*
12*
123*
有什么方法可以解决这个问题?
答案 0 :(得分:3)
1) Pyramid
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/@vishakhaagarwa1/DarkorchidSmoothPayware#main.swift
for i in 1...5
{
for _ in stride(from: 5, to: i, by: -1)
{
print(terminator : " ")
}
for _ in 1...i
{
print("*",terminator : " ")
}
print(" ")
}
2) Right Triangle
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/@vishakhaagarwa1/StingyOldApplicationstack#main.swift
var i = 1
var j = 1
for i in i...5
{
for _ in j...i
{
print("*",terminator : "")
}
print("")
}
3) Mirrored Right Triangle
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/@vishakhaagarwa1/CookedParallelRobodoc#main.swift
for i in 1...5
{
for _ in stride (from: 5, to: i, by: -1)
{
print(" ",terminator : "")
}
for _ in 1...i
{
print("*",terminator : "")
}
print("")
}
4) Diamond
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
LINK :-
https://replit.com/@vishakhaagarwa1/WarmEmptyDegrees#main.swift
for i in 1...10
{
if i % 2 != 0
{
for _ in stride(from: 10, to: i, by: -1)
{
print(terminator : " ")
}
for _ in 1...i
{
print("*",terminator : " ")
}
print(" ")
}
}
for i in stride(from: 10, to: 1, by: -1)
{
if i % 2 != 0
{
for _ in stride(from: 10, to: i-1, by: -1)
{
print(terminator : " ")
}
for _ in stride(from: 2, to: i, by: 1)
{
print("*",terminator : " ")
}
print(" ")
}
}
5) Downward Triangle
* * * * *
* * * *
* * *
* *
*
LINK :-
https://replit.com/@vishakhaagarwa1/FearlessIndolentByte#main.swift
for i in 1...5
{
for _ in stride(from: i, to: 6, by: 1)
{
print("*",terminator : "")
}
print("")
}
6) Right Pascal’s Triangle
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
LINK :-
https://replit.com/@vishakhaagarwa1/IndianredNervousPrinter#main.swift
for i in 1...5
{
for _ in 1...i
{
print("*",terminator : "")
}
print("")
}
for i in 1...4
{
for _ in stride (from: 5, to: i, by: -1)
{
print("*",terminator : "")
}
print("")
}
7) Sandglass Pattern
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/@vishakhaagarwa1/TangibleDeeppinkWatch#main.swift
for i in 1...5
{
for _ in 1...i
{
print(terminator: " ")
}
for _ in stride(from: i, to: 6, by: 1)
{
print("*",terminator : " ")
}
print("")
}
for i in 1...5
{
for _ in stride(from: 6, to: i, by: -1)
{
print(terminator : " ")
}
for _ in 1...i
{
print("*",terminator : " ")
}
print(" ")
}
答案 1 :(得分:1)
如果你想要
1
12
123
1234
for i in 1..<5 { // or for i in stride(from: 1, to: 5, by: 1) {
for j in 1...i { // for j in stride(from: 1, through: i, by: 1) {
print(j, terminator: "")
}
print("")
}
如果你想要
*
**
***
****
for i in 1..<5 {
for _ in 1...i {
print("*", terminator: "")
}
print("")
}
答案 2 :(得分:0)
以下是一些快速的逻辑模式,
['PN', 'P/N', 'PN:', 'P/N:']
答案 3 :(得分:0)
var q : String = ""
repeat
{
print("***** Numerical Patterns *****")
print("1 for Number Right Triangle.. ")
print("2 for Sequential Number Right Triangle.. ")
print("3 for Pascal Number Triangle.. ")
print("Enter Your Choice :: ")
guard let index = Int(readLine()!), index > 0 && index < 4 else
{
print(" !!!! WArning :: Wrong Selection.......")
print("\n")
continue
}
var n: Int
print("Enter N Number For Pattern :: ")
let num = readLine()
if let t2 = Int(num!)
{
n = t2
}
else
{
print("wrong input ! taking default [5]")
n = 5
}
switch index {
case 1 :
for i in 1...n
{
for j in 1...i
{
print(j,terminator: " ")
}
print("")
}
case 2 :
var j = 0
for i in 1...n
{
for _ in 1...i
{
j = j+1
print(j,terminator: " ")
}
print("")
}
case 3 :
for i in 1...n
{
//print("i==",i)
for _ in stride(from: n, to: i, by: -1)
{
//print("k==",k)
print(terminator : " ")
}
var no = 1
for j in 1...i
{
//print("j==",j)
print(no,terminator : " ")
no = no * (i - j) / (j)
}
print(" ")
}
default :
print( "Wrong Input............")
}
print("\n")
print("Do you want to continues [PRESS Y/y] OR CLOSE [PRESS N/n] :: ")
q = (readLine()!)
print("\n")
}while(q == "y" || q == "Y")
答案 4 :(得分:-1)
1. Pyramid
1
1 2
1 2 3
1 2 3 4
for i in 1...4{
for _ in 1...5-i{
print(terminator:" ")
}
for j in 1...i{
print(j,terminator:" ")
}
print("")
}
2. Inverted Pyramid
1 2 3 4
1 2 3
1 2
1
for i in 1...4{
for _ in 1...i{
print(terminator:" ")
}
for j in 1...5-i{
print(j,terminator:" ")
}
print("")
}