看看这个问题:编写一个打印9个版权符号的三角形的程序©
控制台应使用这些©
符号打印一个简单的三角形。三角形必须是空的,没有填充©
个东西。
我更改了控制台字符编码:
Console.OutputEncoding = Encoding.UTF8;
所以©
正确打印。我也改变了字体。
我一直试图通过使用 for loop 来找出绘制三角形的方法,但我的新手知识显然仍然不足以完成这类任务。
你能否给我一些关于如何使用 for loop 绘制三角形的提示/提示?
顺便说一下,我发现这个问题解决了这个问题:
Console.OutputEncoding = Encoding.UTF8;
char copyRight = '\u00A9';
Console.WriteLine("{0,4}\n{0,3}{0,2}\n{0,2}{0,4}\n{0}{0,2}{0,2}{0,2}", copyRight);
答案 0 :(得分:10)
你能否给我一些关于如何用for循环绘制三角形的提示/提示?
不确定。开始于:您是否可以编写一个您理解的 绘制三角形而不使用任何循环的程序?
完成上述操作后,您是否可以使用更简单的循环编写该程序,例如while
?
如果没有,请在解决while
之前了解for
的工作原理。您应该能够清楚地回答问题" if(condition) statement
和while(condition) statement
形式的代码之间的区别是什么?"
完成上述操作后,您是否可以将使用while
编写的正确程序转换为使用for
的等效程序? while
和for
之间存在非常明确的关系。
答案 1 :(得分:3)
这是一个简单的例子。它看起来比它更可怕。
©©©©©©©©©
©©©© ©©©©
©©© ©©©
©© ©©
© ©
©©©©©©©©©
输出:
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Render(Draw('\u00A9', 4));
Render(Draw('\u00A9', 8));
Render(Draw('\u00A9', 16));
}
static char[,] Draw(char sym, int height)
{
int width = (height * 2) - 1;
char[,] map = new char[height, width];
int x = width - 1;
int y = height - 1;
for (int i = 0; i < height; i++)
{
map[y, i * 2] = sym;
if (i != 0)
{
map[y - i, i] = sym;
map[y - i, x - i] = sym;
}
}
return map;
}
static void Render(char[,] map)
{
int width = map.GetLength(1);
int height = map.GetLength(0);
for (int i = 0; i < height; i++)
{
if (i != 0)
{
Console.WriteLine();
}
for (int j = 0; j < width; j++)
{
char c = map[i, j];
Console.Write(c == '\0' ? ' ' : c);
}
}
Console.WriteLine();
}
9个符号的替代版本:
©
© ©
© ©
© © © ©
©
© ©
© ©
© ©
© ©
© ©
© ©
© © © © © © © ©
©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© © © © © © © © © © © © © © © ©
输出:
import java.util.Scanner;
public class RationalDriver
{
private static Scanner in = new Scanner(System.in);
private static char choice;
private static String input;
public static void main(String[] args)
{
printHeading();
Rational first = new Rational();
numberOne(first);
Rational second = new Rational();
numberTwo(second);
menu(first, second);
}
public static void menu(Rational first, Rational second)
{
try{
displayMenu();
getValue(choice);
if(choice == '1')
{
System.out.println("\n" + first + " + " + second + " equals ");
first.add(second);
System.out.println(first);
}
else if(choice == '2')
{
System.out.println("\n" + first + " - " + second + " equals ");
first.subtract(second);
System.out.println(first);
}
else if(choice == '3')
{
System.out.println("\n" + first + " * " + second + " equals ");
first.multiply(second);
System.out.println(first);
}
else if(choice == '4')
{
System.out.println("\n" + first + " / " + second + " equals ");
first.divide(second);
System.out.println(first);
}
else if(choice == '5')
{
System.out.println("\nAre " + first + " and " + second + " equal?");
if(first.equals(second))
{
System.out.println("\nYes!");
}
}
else if(choice == '6')
{
System.out.println("Please enter a new rational number: ");
numberOne(first);
}
else if(choice == '7')
{
System.out.println("Please enter a new rational number: ");
numberTwo(second);
}
else if(choice == '8')
{
System.out.println("You chose to exit.");
System.exit(0);
}
else
{
System.out.println("You did not choose a valid entry.");
}
}
catch (Exception e) { e.printStackTrace(); }
}
private static void printHeading() // printHeading method
{
System.out.println("Oliver, James");
System.out.println("CSC201-02PR");
System.out.println("Dr. Java");
System.out.println("Project 7");
System.out.println("Rational Number");
System.out.println();
}
private static void displayMenu()
{
System.out.println("Enter the corresponding number for the desired action:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Test for Equality");
System.out.println("6. Change 1st Rational Number");
System.out.println("7. Change 2nd Rational Number");
System.out.println("8. Exit");
}
public static char getValue(char choice)
{
System.out.println("\nPlease enter your menu option (1 - 8):");
choice = in.next().charAt(0);
return choice;
}
public static void numberOne(Rational first)
{
int numer = 0;
int denom = 1;
boolean breaker = false;
while(!breaker)
{
System.out.println("Please enter your first numerator:");
input = in.next();
numer = Integer.parseInt(input);
System.out.println("Please enter your first denominator:");
input = in.next();
denom = Integer.parseInt(input);
if(denom == 0)
{
System.out.println("Denominator cannot be undefined.");
denom = in.nextInt();
}
else
breaker = true;
}
first.setNumer(numer);
first.setDenom(denom);
first.normalize();
System.out.println("Your rational number is: " + first + "\n");
}
public static void numberTwo(Rational second)
{
int numer = 0;
int denom = 1;
boolean breaker = false;
while(!breaker)
{
System.out.println("Please enter your second numerator:");
input = in.next();
numer = Integer.parseInt(input);
System.out.println("Please enter your second denominator:");
input = in.next();
denom = Integer.parseInt(input);
if(denom == 0)
{
System.out.println("Denominator cannot be undefined.");
denom = in.nextInt();
}
else
breaker = true;
}
second.setNumer(numer);
second.setDenom(denom);
second.normalize();
System.out.println("\nYour rational number is: " + second + "\n");
}
}
答案 2 :(得分:2)