herrDeng網內搜尋

自訂搜尋

Ads

2016年10月21日 星期五

ex9 猜數字

利用Random()產生1~100的隨機亂數

猜它到猜對為止!

15 則留言:

A10533012 提到...

import java.util.Random;
import java.util.Scanner;

public class bb {
public static void main(String[]args){
Random ran = new Random();
int r=ran.nextInt(100)+1;
int x;
do{
Scanner sc =new Scanner(System.in);
System.out.println("請輸入值:");
x=sc.nextInt();

if(x==r)
System.out.println("猜對了!");
else if(x>r)
System.out.println("太大了!");
else
System.out.println("太小了!");
}while(x>=0);
}

}

A10533010 提到...

import java.util.Random;
import java.util.Scanner;

public class bb {
public static void main(String[]args){
Random ran = new Random();
int r=ran.nextInt(100)+1;
int x;
do{
Scanner sc =new Scanner(System.in);
System.out.println("請輸入值:");
x=sc.nextInt();

if(x==r)
System.out.println("猜對了!");
else if(x>r)
System.out.println("太大了!");
else
System.out.println("太小了!");
}while(x>=0);
}

}

A10533002 提到...

import java.util.Random;
import java.util.Scanner;

public class ex {

public static void main(String[] args) {
Random ran=new Random();
int r=ran.nextInt(100)+1;
long product = 1;
int x;
do{
Scanner sc=new Scanner(System.in);
System.out.println("請輸入值");
x=sc.nextInt();

if(x==r){
System.out.println("猜對了");
break;
}
else if(x>r)
System.out.println("太大了!");
else
System.out.println("太小了!");
}while(x>0 && x<100);




}

A10533001 提到...

import java.util.Random;
import java.util.Scanner;

public class cxvbxcvbxvbcbdf {

public static void main(String[] args) {
Random free=new Random();
int f=free.nextInt(100)+1;//設定終極密碼的數字
//(這個無法重複,所以用do while的時候要移到外面)
int s;
do {
Scanner sc = new Scanner(System.in);//準備要猜的數字
s = sc.nextInt();
if (s == f) {
System.out.println("中獎");
break;//中獎將不會繼續猜下去(終止)
} else if (s > f) {
System.out.println("too big");
} else {
System.out.println("too small");
}
} while (s>0&&s<=100);//(猜的數字大於0)
}

}

A15033016 提到...

import java.util.Random;
import java.util.Scanner;

public class milly2 {

public static void main(String[] args) {
Random ran=new Random();
int r=ran.nextInt(100)+1;
long product = 1;
int x;
do{
Scanner sc =new Scanner(System.in);
System.out.println("請輸入值");
x=sc.nextInt();

if (x==r){
System.out.println("猜對了");
break;

}
else if(x>r)
System.out.println("太大了!");
else
System.out.println("太小了");
}while(x>0 && x<100);

A10533006林柏宇 提到...

public class ex {

public static void main(String[] args) {
int target, guess;
java.util.Scanner sc=
new java.util.Scanner(System.in);
target = (int) (Math.random()*100+1);

do {
System.out.print("輸入猜測值=> ");
guess = sc.nextInt();
if ( guess == target ) {
break;
}
else {
if ( guess > target )
System.out.println("太大!");
else
System.out.println("太小!");
}
} while ( true );
System.out.println("猜中數字" + target);
}
}

A10533011 提到...

import java.util.*;
public class ex2 {
public static void main(String[] args) {

int r,a;
Random ran = new Random();
r = ran.nextInt(100)+1;

do {
Scanner scan = new Scanner(System.in);
System.out.println("輸入數字");
a = scan.nextInt();

if(a<1||a>100)
break;
else if (a == r) {
System.out.println("Bingo");
break;
}
else if (a > r)
System.out.println("太大");
else
System.out.println("太小");
} while (a>=1&&a<=100);

}
}

A10533005 提到...

import java.util.*;

public class ex7 {

public static void main(String[] args) {
Random ran = new Random();
int r=ran.nextInt(100)+1;
int x;
do{
Scanner sc =new Scanner(System.in);
System.out.println("請輸入值:");
x=sc.nextInt();

if(x==r){
System.out.println("猜對了!");
break;
}
else if(x>r)
System.out.println("太大了!");
else
System.out.println("太小了!");
}while(x>0 && x<100);

}

}

A10533007 提到...

import java.util.Random;
import java.util.Scanner;

public class jj
{

public static void main(String[] args)
{
Random rnd=new Random();
Scanner sc = new Scanner(System.in);
int x=rnd.nextInt(100)+1;
int c;

do {
System.out.print("請輸入猜測值=>");
c=sc.nextInt();
if(c==x)
System.out.println("恭喜中獎");
else
if(c>x)
System.out.println("數字太小");
else
System.out.println("數字太大");

}while(x!=c);

}
}

A10533014 提到...

import java.util.Random;
import java.util.Scanner;
public class qqq {

public static void main(String[] args) {

Random ran = new Random();
Scanner sc = new Scanner(System.in);
int guess = ran.nextInt(100)+1;

int target;
do {
System.out.print("請輸入猜測值=> ");
target= sc.nextInt();
if (guess == target)
System.out.print("猜對");

else if (guess > target)
System.out.println("數字太大");
else
System.out.println("數字太小");
} while (guess!=target);

}
}

A10533004易廷軒 提到...

package hello;

import java.util.Scanner;

public class ex1 {

public static void main(String[] args) {
int target, guess;
java.util.Scanner sc =
new java.util.Scanner(System.in);
target = (int)(Math.random()*100 + 1);

do {
System.out.print("請輸入猜測值=> ");
guess = sc.nextInt();
if ( guess == target ) {
break;
}
else {
if ( guess > target )
System.out.println("數字太大");
else
System.out.println("數字太小");
}
} while ( true );
System.out.println("猜中數字 :" + target);
}
}

A10533009林子喬 提到...

package hello;

import java.util.Scanner;

public class ex1 {

public static void main(String[] args) {
int target, guess;
java.util.Scanner sc =
new java.util.Scanner(System.in);
target = (int)(Math.random()*100 + 1);

do {
System.out.print("請輸入猜測值=> ");
guess = sc.nextInt();
if ( guess == target ) {
break;
}
else {
if ( guess > target )
System.out.println("數字太大");
else
System.out.println("數字太小");
}
} while ( true );
System.out.println("猜中數字 :" + target);
}
}

A10533003 提到...

import java.util.Random;
import java.util.Scanner;
public class s3 {
public static void main(String[] args) {

Random ran = new Random();
Scanner sc = new Scanner(System.in);
int Y = ran.nextInt(100)+1;

int S;
do {
System.out.print("請輸入數字=> ");
S= sc.nextInt();
if (Y == S)
System.out.print("猜對");

else if (Y > S)
System.out.println("數字太大");
else
System.out.println("數字太小");
} while (Y!=S);

}
}

A10533013 提到...

import java.util.*;

public class ex1 {

public static void main(String[] args) {
Random ran = new Random();
int x =ran.nextInt(100)+1;

int z;
do {
Scanner aaa = new Scanner(System.in);
System.out.println("請輸入值:");
z = aaa.nextInt();
if (z > x)
System.out.println("太大");
else if (z < x)
System.out.println("太小");
else
System.out.println("得獎囉");
} while (z>0 && z<=100);


}
}

B10233077 提到...

import java.util.Random;
import java.util.Scanner;
public class s3 {
public static void main(String[] args) {

Random ran = new Random();
Scanner sc = new Scanner(System.in);
int Y = ran.nextInt(100)+1;

int S;
do {
System.out.print("請輸入數字=> ");
S= sc.nextInt();
if (Y == S)
System.out.print("猜對");

else if (Y > S)
System.out.println("數字太大");
else
System.out.println("數字太小");
} while (Y!=S);

}
}

Related Posts Plugin for WordPress, Blogger...

熱門文章