[英]Trying convert do-while loop to while loop
I trying convert do-while to while loop and program even compile, but nothing happens. 我尝试将do-while转换为while循环,程序甚至编译,但没有任何反应。 Original do-while loop code:
原始的do-while循环代码:
import java.util.Scanner;
public class CoinFlip
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String coin, again;
int flip, streak = 0;
do
{
flip = 1 + (int)(Math.random()*2);
if ( flip == 1 )
coin = "HEADS";
else
coin = "TAILS";
System.out.println( "You flip a coin and it is... " + coin );
if ( flip == 1 )
{
streak++;
System.out.println( "\tThat's " + streak + " in a row...." );
System.out.print( "\tWould you like to flip again (y/n)? " );
again = keyboard.next();
}
else
{
streak = 0;
again = "n";
}
} while ( again.equals("y") );
System.out.println( "Final score: " + streak );
}
}
How it looks like after I trying to do while loop from do-while loop: 我尝试从do-while循环执行while循环后的样子:
import java.util.Scanner;
public class CoinFlip2
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String coin, again;
int flip, streak = 0;
again = keyboard.next();
//flip = 1 + (int) (Math.random()*2);
while ( again.equals("y") )
{
flip = 1 + (int) (Math.random()*2);
if ( flip == 1)
{
coin = "HEADS";
}
else
{
coin = "TAILS";
System.out.println( "You flip a coin and it is..." + coin );
}
if ( flip == 1)
{
streak++;
System.out.println("\tThat's " + streak + " in a row....");
System.out.print( "\tWould you like to flip again (y/n)? ");
//again = keyboard.next();
}
else
{
streak = 0;
again = "n";
}
System.out.println( "Final score: " + streak);
}
}
}
Code compile without problem but I see blank output after running it. 代码编译没有问题,但运行后我看到空白输出。
That's because you never initialize again
variable to y
, so it never matches condition to enter while
那是因为你从来没有初始化
again
可变y
,所以它永远不匹配状态进入while
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String coin, again = "y";
int flip, streak = 0;
// flip = 1 + (int) (Math.random()*2);
while (again.equals("y")) {
flip = 1 + (int) (Math.random() * 2);
if (flip == 1) {
coin = "HEADS";
} else {
coin = "TAILS";
System.out.println("You flip a coin and it is..." + coin);
}
if (flip == 1) {
streak++;
System.out.println("\tThat's " + streak + " in a row....");
System.out.print("\tWould you like to flip again (y/n)? ");
again = keyboard.next();
// again = keyboard.next();
} else {
streak = 0;
again = "n";
}
}
System.out.println("Final score: " + streak);
keyboard.close();
}
OUTPUT: OUTPUT:
That's 1 in a row....
Would you like to flip again (y/n)? y
You flip a coin and it is...TAILS
Final score: 0
Initialize your again
with "y"
: 用
"y"
again
初始化你:
String coin, again;
int flip, streak = 0;
again = "y";
...
and ask for keyboard entry in your while loop: 并在while循环中询问键盘输入:
if ( flip == 1)
{
streak++;
System.out.println("\tThat's " + streak + " in a row....");
System.out.print( "\tWould you like to flip again (y/n)? ");
again = keyboard.next();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.