Output of the first Java while statement problem
Roll two dice, how many times do you get doublet?
1st (2,3) 2nd (3,2) 3rd (4,4) 3rd doublet!
import java.util.*;
public class Main{
public static void main(String[] args){
Random rand=new Random();
int count=0;
while(true){
count++;
int dice1=rand.nextInt(6)+1;
int dice2=rand.nextInt(6)+1;
System.out.printf("%dth time(%d,%d)%n",count,dice1,dice2);
if(dice1 == dice2){
break;
}
}
System.out.println(count+"The second time I got doublet!");
}
}
Realize the following processing.
9 8 7 6 5 4 3 2 1 firing!
import java.util.*;
public class Main{
public static void main(String[] args){
int n=9;
while(n>0){
System.out.println(n);
n--;
}
System.out.println("firing!");
}
}
Generate random numbers from 1 to 999 repeatedly and display the number of times until 777 appears for the first time.
1:34 2:432 3:321 ..... 1034: 777 1034 The 777 came out for the third time!
import java.util.*;
public class Main{
public static void main(String[] args){
Random rand=new Random();
int num=-1;
int count=0;
while(num !=777){
count++;
num=rand.nextInt(999)+1;
System.out.printf("%d:%d%n",count,num);
}
System.out.println(count+"I got 777 for the second time!");
}
}
[Execution result] Positive integer> 20 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.print("Positive integer>");
int max=sc.nextInt();
int i=1;
String ans;
while(i<=max){
if(i %3 == 0 && i % 5== 0){
ans="FizzBuzz";
}else if(i % 3==0){
ans="Fizz";
}else if(i % 5==0){
ans="Buzz";
}else{
ans=i+"";
}
System.out.println(ans);
i++;
}
}
}
It is recognition.