[英]Program won't let the user input the number of rows in Pascal's triangle - Java
[英]Write a java program that prints a circle, rectangle or triangle depending on the user's choice after input from the user on the dimensions
編寫一個 java 程序,提示用戶選擇要顯示在屏幕上的形狀。 用戶選擇一個三角形、圓形或矩形,並提示用戶提供用於計算的尺寸,然后繼續並將形狀打印在屏幕上。
我試過了但不幸的是失敗了。 這是代碼。
import java.*;
import java.util.Scanner;
public class shape {
public static void main(String[] args) {
int P;
int x, y;
int r;
r = 0;
double shape;
int c;
int height;
int width;
Scanner reader = new Scanner(System.in);
System.out.print("Choose a shape : ");
shape = reader.nextInt();
double circle;
circle = 0;
double rectangle;
rectangle = 0;
double triangle;
triangle = 0;
if (shape == circle) {
P = 2 * r;
for (int i = 0; i <= P; i++) {
for (int j = 0; j <= P; j++) {
x = r - i;
y = r - j;
c = x*x + y*y ;
if (c <= r * r + 1) {
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
else if(shape == rectangle) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of widths: ");
width = scan.nextInt();
// get input from the user for width
System.out.print("Enter the number of widths: ");
height = scan.nextInt();
// get input from the user for column
for (int i = 1; i <= width; i++) {
for (int j = 1; j <= height; j++) {
System.out.print("*");
}
System.out.print("\n");
}
if (shape == triangle) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows to be printed");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
}
}
我想到了。
import java.util.Scanner;
public class shapes2 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter c to calculate area of circle");
System.out.println("Enter s to calculate area of square");
System.out.println("Enter r to calculate area of rectangle");
System.out.print("Enter your choice: ");
char choice = in.next().charAt(0);
switch(choice) {
case 'c':
double distance;
int rad;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any value for radius: ");
rad = sc.nextInt();
System.out.println("Circle Pattern:\n");
for (int row = 0; row <= 2 * rad; row++) {
for (int col = 0; col <= 2 * rad; col++) {
distance = Math.sqrt((row - rad) * (row - rad) + (col - rad) * (col - rad));
if (distance > rad - 0.5 && distance < rad + 0.5)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
break;
case 's':
int side, i, j;
Scanner scs = new Scanner(System.in);
System.out.print(" Please Enter any Side of a Square : ");
side = scs.nextInt();
for(i = 1; i <= side; i++)
{
for(j = 1; j <= side; j++)
{
System.out.print("*");
}
System.out.print("\n");
}
break;
case 'r':
int rows, columns, x, y;
sc = new Scanner(System.in);
System.out.print(" Please Enter Number of Rows : ");
rows = sc.nextInt();
System.out.print(" Please Enter Number of Columns : ");
columns = sc.nextInt();
for(x = 1; x <= rows; x++)
{
for(y = 1; y <= columns; y++)
{
System.out.print("* ");
}
System.out.print("\n");
}
break;
default:
System.out.println("Wrong choice!");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.