【发布时间】:2017-11-08 04:44:14
【问题描述】:
你好,我被困在课堂上的编程作业上。任务是制作一个可以计算不同循环的程序。我有正确的提示用户输入代码我想我只是不知道如何让它打印我的消息,我输入的次数。
我希望第二个 while 循环打印类似
的内容第 1 行 - 你好
第 2 行 - 你好
第 3 行 - 你好
第 4 行 - 你好
对于我输入的数字。
提前感谢您的帮助。
import java.util.Scanner;
public class CountingLoops
{
public static void main(String[] args)
{
String message; // First message prompt
String inputString; // For reading input.
double numRows; // Number to be asked.
double rowNumber = 1;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the message you want to print? ");
message = keyboard.nextLine();
System.out.print("Enter how many times you want your message to be printed. Enter the value from 1-20:" );
numRows = keyboard.nextDouble();
while (numRows > 20 || numRows < 1)
{
System.out.print("Number was not between 1-20. Try Again.");
System.out.print("Enter how many times you want your message to be printed. Enter the value from 1-20: ");
numRows = keyboard.nextDouble();
}
while (numRows <= 20)
{
System.out.println("Row " + rowNumber++ + " - " + message);
rowNumber = keyboard.nextDouble();
rowNumber++;
}
}
}
【问题讨论】:
-
它目前在做什么?无论如何,你的最后一个循环是一个无限循环,因为你永远不会更新 numRows 的值
-
这是我编译和运行程序时当前打印的内容。输入您要打印的消息?您好 输入您希望您的信息被打印多少次。输入 1-20:5 Row 1.0 - Hello I want it to print Row 1.0 - Hello Row 2.0 - Hello Row 3.0 - Hello etc. 直到输入 numRows 的金额。
标签: java loops while-loop