2
我是Java新手。我試圖做一個三角形的乘法表看起來有點像這樣:三角乘法表
輸入行7
1 2 3 4 5 6 7
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
每一行/列的#有編號的,我不知道如何做到這一點。我的代碼看起來好像沒有了,因爲我得到了一個無限循環,它沒有包含正確的值。下面你會發現我的代碼。
public class Prog166g
{
public static void main(String args[])
{
int userInput, num = 1, c, d;
Scanner in = new Scanner(System.in);
System.out.print("Enter # of rows "); // user will enter number that will define output's parameters
userInput = in.nextInt();
boolean quit = true;
while(quit)
{
if (userInput > 9)
{
break;
}
else
{
for (c = 1 ; c <= userInput ; c++)
{ System.out.println(userInput*c);
for (d = 1; d <= c; d++) // nested loop required to format numbers and "triangle" shape
{
System.out.print(EasyFormat.format(num,5,0));
}
}
}
}
quit = false;
}
}
EasyFormat是指應該正確格式化圖表的外部文件,所以忽略它。如果有人可以請指點我正確的方向,至於修復我現有的代碼並添加代碼來對列和行進行編號,那將不勝感激!
太感謝你了,夥計!這正是我需要的。我怎樣才能讓列被編號? :) – Blackout621 2014-10-19 03:13:28
首先打印一個(在循環中):'System.out.printf(「%d \ t」,i);'1-7之間。我會將其添加到答案中。 – alfasin 2014-10-19 03:15:36
完美的工作!再次,謝謝你。這個程序在過去的一個多小時裏我一直在努力掙扎。 – Blackout621 2014-10-19 03:21:20