[英]Creating a User-Input Asterisk Triangle using Java
使用Java創建一個星號三角形 ,該三角形匹配用戶輸入的任意數字的長度(1-50之間)。
例如,如果用戶輸入3,則輸出將在第一行上帶有一個星號,在第二行上帶有兩個星號,在第三行上帶有三個星號,然后在結束之前返回到下一行上的兩個星號最后一行帶有星號。
我需要使用嵌套的循環。 到目前為止,我嘗試使用下面的練習示例對其進行測試。 我只能在數字輸出上創建。 我也有一些輸出星號三角形的概念。 我如何應用此代碼的概念來遵循用戶的輸入數字?
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int count, index = 0, value, number;
System.out.println("This program creates a pattern of numbers " );
System.out.println("Based on a number you enter." );
System.out.println("Please enter a positive integer. " );
count = keyboard.nextInt();
value = count;
for (index = 1; index <= count; index++)
{
for (number = value; number >= 1; number--)
{
System.out.println(number);
}
value--;
System.out.println();
}
}
}
這就是我將如何進行
printAsterisks
,該方法將int N作為參數並編寫一行N個星號。 您將需要一個for
循環來這樣做。 for
循環中調用printAsterisks
printAsterisks
這應該夠了吧。
另外,作為附帶說明,您應該關閉掃描儀。 這樣做的簡單方法是將ot封裝在try-with-resource中,如下所示:
try (Scanner keyboard = new Scanner(System.in);) {
// your code here
}
讓我們知道程序的版本(或者您還有問題):)
HTH
這是您想要的:
public class Asterisk {
private static final String ASTERISK = "*";
private static final String SPACE = "";
private static int LENGTH;
public static void main(String[] args) {
try{
readLength();
for (int i=1; i<=LENGTH; i++) {
if (i == LENGTH) {
for (int j=LENGTH; j>=1; j--) {
drawLine(j);
}
break;
}
drawLine(i);
}
}catch (Exception e) {
System.out.println("You must enter a number between 1 and 50.");
}
}
static void readLength(){
System.out.println("Enter asterisk's length (1-50)");
LENGTH = Integer.parseInt(System.console().readLine());
if (LENGTH<=0 || LENGTH>50)
throw new NumberFormatException();
}
static void drawLine(int asterisks){
StringBuilder line = new StringBuilder();
int spacesLeft = getLeftSpaceCount(asterisks);
int spacesRight = getRightSpaceCount(asterisks);
for (int i=0; i<spacesLeft; i++) {
line.append(SPACE);
}
for (int i=0; i<asterisks; i++) {
line.append(ASTERISK);
}
for (int i=0; i<spacesRight; i++) {
line.append(SPACE);
}
System.out.println(line.toString()+"\n");
}
static int getLeftSpaceCount(int asterisks){
int spaces = LENGTH - asterisks;
int mod = spaces%2;
return spaces/2 + mod;
}
static int getRightSpaceCount(int asterisks){
int spaces = LENGTH - asterisks;
return spaces/2;
}
}
我需要使用嵌套的for循環
是的,主要邏輯就在那...
for (int i=1; i<=LENGTH; i++) {
if (i == LENGTH) {
for (int j=LENGTH; j>=1; j--) {
drawLine(j);
}
break;
}
drawLine(i);
}
使用5作為輸入的三角形。
*
**
***
****
*****
****
***
**
*
小費:
有一種使用System.console().readLine()
從用戶獲取輸入的簡便方法。
關於印刷部分,我想稍微弄清楚答案:
int input = 3; //just an example, you can hook in user input I'm sure!
for (int i = 1; i < (input * 2); i++) {
int amount = i > input ? i / 2 : i;
for (int a = 0; a < amount; a++)
System.out.print("*");
}
System.out.println();
}
對於我們的循環條件,有一些解釋:
i < (input * 2)
:由於i
從1開始,所以我們可以考慮幾種情況。 如果輸入為1,則需要1行。 輸入2、3行。 4:5行。 簡而言之,長度與行數的關系是row count = (length * 2) - 1
,因此我另外從1而不是0開始偏移了1。
i > input ? i / 2 : i
i > input ? i / 2 : i
:這稱為三元語句,它基本上是一個if語句,您可以在其中以boolean/if ? value_if_true : value_if_false
形式獲取值boolean/if ? value_if_true : value_if_false
boolean/if ? value_if_true : value_if_false
。 因此,如果行數大於您要求的長度(大於一半),則長度除以2。
另外,該循環中的所有內容都可能是一行:
System.out.println(new String(new char[i > input ? i / 2 : i]).replace('\0', '*'));
是的,從技術上講,我們可以使用IntStream將整個過程變成一行,盡管在那一點上,我為了清楚起見將換行
請記住,我不會將其稱為“初學者的解決方案”,但希望它能引起您學習關於編程的其他一些有用的小知識,例如,為什么在我的單行示例中它被替換為\\0
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.