如果输入为';y'; import java.util.Scanner; 导入java.util.Random; 公营部门 { 公共静态void main(字符串[]args) { 扫描仪扫描=新扫描仪(System.in); System.out.println(“是否要看到三角形?插入y以继续”); 字符串输入=scan.next(); 布尔控制=假; 如果((输入等于(“y”)) { cont=真; 双a=(40); 双b=(30); 双倍高度=(Math.random()*(b-a+1)+a); 对于(intx=1;x
将if语句更改为while,在循环中再次请求用户输入,然后删除else如果输入为';y'; import java.util.Scanner; 导入java.util.Random; 公营部门 { 公共静态void main(字符串[]args) { 扫描仪扫描=新扫描仪(System.in); System.out.println(“是否要看到三角形?插入y以继续”); 字符串输入=scan.next(); 布尔控制=假; 如果((输入等于(“y”)) { cont=真; 双a=(40); 双b=(30); 双倍高度=(Math.random()*(b-a+1)+a); 对于(intx=1;x,java,loops,Java,Loops,将if语句更改为while,在循环中再次请求用户输入,然后删除else import java.util.Scanner; import java.util.Random; public class DrawTriangle { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Do you want to see a trian
import java.util.Scanner;
import java.util.Random;
public class DrawTriangle
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println ("Do you want to see a triangle? Insert y to continue");
String input = scan.next();
boolean cont = false;
if ((input.equals("y")))
{
cont = true;
double a = (40);
double b = (30);
double height = (Math.random() * (b - a + 1) + a);
for (int x = 1; x <= height; x++)
{
for (int y = 0; y < height - x; y++)
{
System.out.print(" ");
}
for (int y = 0; y < x; y++)
{
System.out.print("x ");
}
System.out.println();
}
}
else
{
cont = false;
System.out.println();
System.out.println ("Program ended");
}
}
}
while((input.equals(“y”))
{
cont=真;
双a=(40);
双b=(30);
双倍高度=(Math.random()*(b-a+1)+a);
对于(int x=1;x,只需将if语句交换为循环,如下所示:
while ((input.equals("y")))
{
cont = true;
double a = (40);
double b = (30);
double height = (Math.random() * (b - a + 1) + a);
for (int x = 1; x <= height; x++)
{
for (int y = 0; y < height - x; y++)
{
System.out.print(" ");
}
for (int y = 0; y < x; y++)
{
System.out.print("x ");
}
System.out.println();
}
System.out.println ("Do you want to see a triangle? Insert y to continue");
input = scan.next();
}
System.out.println();
System.out.println ("Program ended");
import java.util.Scanner;
导入java.util.Random;
公营部门
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
布尔控制=假;
字符串输入=“y”;
while(input.equals(“y”))
{
System.out.println(“是否要看到三角形?插入y以继续”);
输入=scan.next();
cont=真;
双a=(40);
双b=(30);
双倍高度=(Math.random()*(b-a+1)+a);
对于(int x=1;x)显然,如果要重复,问题和阅读用户输入必须在循环内。当检测到!input.equals(“y”)
时,可以使用while(true)
和break
。
import java.util.Scanner;
import java.util.Random;
public class DrawTriangle
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
boolean cont = false;
String input = "y";
while (input.equals("y"))
{
System.out.println ("Do you want to see a triangle? Insert y to continue");
input = scan.next();
cont = true;
double a = (40);
double b = (30);
double height = (Math.random() * (b - a + 1) + a);
for (int x = 1; x <= height; x++)
{
for (int y = 0; y < height - x; y++)
{
System.out.print(" ");
}
for (int y = 0; y < x; y++)
{
System.out.print("x ");
}
System.out.println();
}
}
cont = false;
System.out.println();
System.out.println ("Program ended");
}
}