Java 连接循环的结果 String lottoNumbers=“”; 扫描仪扫描=新扫描仪(System.in); System.out.println(“要买票,我们需要你的号码…”); 对于(int i=1;i=1&&userNum 49 1.4.6.如有必要,结束 1.5.端环 1.6.打印“您选择的数字”+输出 1.7.打印“已打印车票-请支付2英镑”
在else部分,您要求用户输入一个新号码,然后保存该号码,但不将其添加/连接到字符串lottonumber中Java 连接循环的结果 String lottoNumbers=“”; 扫描仪扫描=新扫描仪(System.in); System.out.println(“要买票,我们需要你的号码…”); 对于(int i=1;i=1&&userNum 49 1.4.6.如有必要,结束 1.5.端环 1.6.打印“您选择的数字”+输出 1.7.打印“已打印车票-请支付2英镑”,java,loops,for-loop,do-while,Java,Loops,For Loop,Do While,在else部分,您要求用户输入一个新号码,然后保存该号码,但不将其添加/连接到字符串lottonumber中 另外,我会注意从else循环添加的地方,这样就不会错误地添加额外的数字。当用户在主循环中输入超出范围的数字时,您的追赶do{}while()循环不会处理输入。它会检查输入的有效性,当然,但不会在任何地方使用它 您需要做的是,一旦用户在catch-up循环中纠正了自己的错误,就将该值附加到字符串中 1.1. Declare a String variable to hold the
另外,我会注意从else循环添加的地方,这样就不会错误地添加额外的数字。当用户在主循环中输入超出范围的数字时,您的追赶
do{}while()
循环不会处理输入。它会检查输入的有效性,当然,但不会在任何地方使用它
您需要做的是,一旦用户在catch-up循环中纠正了自己的错误,就将该值附加到字符串中
1.1. Declare a String variable to hold the output
1.2. Declare scanner variable
1.3. Print “To buy a ticket we need your numbers…”
1.4. Loop 6 times
1.4.1. Print “Please enter lotto number x”
1.4.2. Declare variable to hold users num
1.4.3. Store users num
1.4.4. If number is between 1 and 49 then
1.4.4.1.1. concatenate the users number plus a comma to the output
1.4.5. Else
1.4.5.1.1. Do Loop
1.4.5.1.1.1. Print “numbers must be between 1 and 49 please try again”
1.4.5.1.1.2. Store users num
1.4.5.1.2. Loop While users num < 0 or users num> 49
1.4.6. End If
1.5. End Loop
1.6. Print “You selected numbers “ + output
1.7. Print “Ticket has been printed – please pay £2”
if(userNum>=1&&userNum 49);
//在此处处理有效输入
lottoNumbers=lottoNumbers+userNum;
如果(i<6)lottoNumbers=lottoNumbers+“,”;
}//注意else块周围的额外括号
这不是因为for循环只运行了6次吗?太好了,谢谢。我忘了处理正确的输入。谢谢!你们一如既往地棒极了
1.1. Declare a String variable to hold the output
1.2. Declare scanner variable
1.3. Print “To buy a ticket we need your numbers…”
1.4. Loop 6 times
1.4.1. Print “Please enter lotto number x”
1.4.2. Declare variable to hold users num
1.4.3. Store users num
1.4.4. If number is between 1 and 49 then
1.4.4.1.1. concatenate the users number plus a comma to the output
1.4.5. Else
1.4.5.1.1. Do Loop
1.4.5.1.1.1. Print “numbers must be between 1 and 49 please try again”
1.4.5.1.1.2. Store users num
1.4.5.1.2. Loop While users num < 0 or users num> 49
1.4.6. End If
1.5. End Loop
1.6. Print “You selected numbers “ + output
1.7. Print “Ticket has been printed – please pay £2”
if (userNum >= 1 && userNum <= 49) {
lottoNumbers = lottoNumbers + userNum;
if (i < 6) lottoNumbers = lottoNumbers + ", ";
} else {
do {
System.out.println("Numbers must be between 1 and 49. \nPlease try again.");
userNum = scan.nextInt();
} while (userNum < 1 || userNum > 49);
// Process valid input here
lottoNumbers = lottoNumbers + userNum;
if (i < 6) lottoNumbers = lottoNumbers + ", ";
} // Note the extra brackets around else block