99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

猜四個數字java代碼,四個數字的代碼

java猜4個數字游戲

這位朋友,我給你寫出來了,我覺得猜數字挺好寫的,產生不同的數這個倒是有點難點,我定義了一個填充數組的函數,實現了這個功能。不過5分確實有點少,你自己看看吧。

米東網站建設公司成都創新互聯,米東網站設計制作,有大型網站制作公司豐富經驗。已為米東上1000+提供企業網站建設服務。企業網站搭建\成都外貿網站制作要多少錢,請找那個售后服務好的米東做網站的公司定做!

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.util.Arrays;

public class GuessNum extends JFrame implements ActionListener{

JTextField jtf=new JTextField(16);

JButton b1=new JButton("確定"),b2=new JButton("重玩一局"),b3=new JButton("看答案");

JLabel jl1=new JLabel("請輸入四個不同數字:"),jl2=new JLabel(" ");

int[] right=new int[4];

int count=0,A,B;

public GuessNum(){

super("猜數字");

fill(right,9);

setLayout(new FlowLayout(FlowLayout.CENTER));

add(jl1);add(jtf);add(jl2);add(b1);add(b3);add(b2);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(330,250);

setVisible(true);

}

public void fill(int[] a,int b){//給數組填充不同的1到b的數字

for(int i=0;ia.length;i++){

a[i]=(int)(Math.random()*b+1);

while(true){

if(in_it(a[i],a,i))a[i]=(int)(Math.random()*b+1);

else break;

}

}

}

public boolean in_it(int num,int[] before,int n){//判斷num是否在數組before的前n項內

for(int i=0;in;i++)

if(num==before[i])return true;

return false;

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==b1){

count++;

String s=jtf.getText();

if(s.length()!=4)jl2.setText("格式錯誤");

else {

A=0;B=0;

for(int i=0;i4;i++)

for(int j=0;j4;j++)

if((s.charAt(i)-'0')==right[j])

{

if(i==j)A++;

else B++;

}

if(A==4){jl2.setText("RIGHT!猜了"+count+"次");

count=0;

}

else jl2.setText("提示:"+A+"A"+B+"B");

}

}

if(e.getSource()==b2){

fill(right,9);

jtf.setText("");

jl2.setText(" ");

}

if(e.getSource()==b3){

jl2.setText(Arrays.toString(right));

}

}

public static void main (String[] args) {

new GuessNum();

}

}

用JAVA寫一個程序 求解 題目:猜數字游戲 隨機生成4個0到9的整數,組成一個序列

import?java.util.Random;

import?java.util.Scanner;

/*

*?游戲隨即給出一個0~99(包括0和99)的數字,然后讓你猜是什么數字。你可以隨便猜一個數字,游戲會提示太大還是太小,從而縮小結果范圍。經過幾次猜測與提示后,最終退出答案。在游戲過程中。記錄你最終猜對時所需要的次數。游戲結束后公布結果。見下

次數?????????????????結果

1????????????????????你太有才了!

2~6??????????????????這么快就猜出來了,很聰明么!

大于7????????????????猜了半天才猜出來,小同志,尚需努力啊!

*/

public?class?guessGame?{

/**

*?@param?args

*/

public?static?void?main(String[]?args)?{

int?gameValue?=?(int)(Math.random()()*(100-1)+1);

System.out.println("Rand:"+gameValue);

Scanner?sc?=?new?Scanner(System.in);

System.out.println("請輸入一個數字");

int?num?=?sc.nextInt();

int?guessCorrectNum=1;

while(true){

if(num==gameValue){

if(guessCorrectNum?==?1)

System.out.println("你太有才了!");

else?if((guessCorrectNum?=2)??(guessCorrectNum=6))

System.out.println("這么快就猜出來了,很聰明么");

else?if(guessCorrectNum?7)

System.out.println("猜了半天才猜出來,小同志,尚需努力?。?);

break;

}

else{

if?(guessCorrectNum?=20){

guessCorrectNum?=?guessCorrectNum?+?1;

num?=?sc.nextInt();?

}

else{

System.out.println("20次都猜不出來...,不讓你猜了");

break;

}

}

}

}

}

please tell me your q-number,so I can send it by q.

用JAVA語言編寫一個“猜數字游戲”的程序

int num = (int)(Math.random()*100)+1;

Scanner sc = new Scanner(System.in);? ?

int guessNum = -1;

while (guessNum != num) {

System.out.println("請輸入1-100之間整數");

guessNum = sc.nextInt();

if (guessNum == num) {

System.out.println("中啦");

} elseif (guessNum num) {

System.out.println("小啦");

} else {

System.out.println("大了");

}

}

擴展資料:

編寫思路

1、成1-100之間隨機數

(int)(Math.random()*100)+1;

提示用戶輸入數字,

Scanner??sc=new?Scanner(System.in);

int?guessNum?= sc.nextInt();

需要將隨機數和用戶輸入的數字進行比較。

猜一次:

Scanner sc = new Scanner(System.in);

int num = (int)(Math.random()*100)+1;

System.out.println("請輸入0-100之間整數");

int guessNum = sc.nextInt();

if (guessNum == num) {

System.out.println("中啦");

}?elseif?(guessNum num) {

System.out.println("小啦");

}?else?{

System.out.println("大了");

}

二、使用while循環

publicstaticvoid main(String[] args) {

int num = (int)(Math.random()*100)+1;

Scanner sc = new Scanner(System.in);

while (true) {

System.out.println("請輸入1-100之間整數");

int guessNum = sc.nextInt();

if (guessNum == num) {

System.out.println("中啦");

} elseif (guessNum num) {

System.out.println("小啦");

} else {

System.out.println("大了");

}

}

}

三、最后用while() 括號中的條件表達式,當用戶猜測的數和系統生成的數字不相等時,就需要繼續循環。

如何用Java語言實現猜數字游戲

java實現的簡單猜數字游戲代碼,通過隨機數與邏輯判斷來實現游戲功能 代碼如下: import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // 產生一個隨機數 int n

java如何實現的簡單猜數字游戲代碼

主要通過隨機數與邏輯判斷來實現游戲功能

實現代碼如下:

import java.util.InputMismatchException;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// 產生一個隨機數

int number = (int) (Math.random() * 100) + 1;

// 加入count

int count = 0;

// 在這里加入最大值,和最小值

int max = 100;

int min = 1;

while (true) {

// 鍵盤錄入數據

Scanner sc = new Scanner(System.in);

System.out.println("請輸入你要猜的數據:(" + min + "~" + max + ")");

try {

count++;

int guessNumber = sc.nextInt();

// 判斷

if (guessNumber number) {

max = guessNumber;

System.out.println("你猜大了");

} else if (guessNumber number) {

min = guessNumber;

System.out.println("你猜小了");

} else {

System.out.println("恭喜你,花了" + count + "次就猜中了");

// 問是否繼續

System.out.println("請問還要繼續嗎?(yes)");

sc = new Scanner(System.in);

String str = sc.nextLine();

if ("yes".equals(str)) {

// 重寫賦值隨機數

number = (int) (Math.random() * 100) + 1;

count = 0;

max = 100;

min = 1;

} else {

break;

}

}

} catch (InputMismatchException e) {

System.out.println("你輸入的數據有誤");

}

}

}

java中四個不重復的數字猜數字游戲

int gameNum = num();// 系統生成的四位不重復數字

while(true){

Scanner sc = new Scanner(System.in);// 鍵盤輸入

int userNum = sc.nextInt();

if (gameNum == userNum) {// 如果用戶輸入等于系統生成,執行下面的代碼

System.out.println("恭喜你猜對了O(∩_∩)O哈哈~");

num();// 繼續猜下一個游戲

} else if (userNum gameNum) {

System.out.println("數字有點大噢/(ㄒoㄒ)/~~");

} else if (userNum gameNum) {

System.out.println("數字小了點噢(*^__^*) 嘻嘻……");

}

}

}

public static int num() {

ListInteger list = new ArrayListInteger();// 首先建立個list集合

String str = "";

list.add(0);

list.add(1);

list.add(2);

list.add(3);

list.add(4);

list.add(5);

list.add(6);

list.add(7);

list.add(8);

list.add(9);

Random rand = new Random();

int a = rand.nextInt(list.size());// 隨機獲取list下標

for (int i = 0; i list.size(); i++) {// for循環

if (list.get(a) == list.get(i)) {

str = str + list.get(i);// 拼接字符串

list.remove(i);

}

}

int b = rand.nextInt(list.size() - 1);

for (int i = 0; i list.size(); i++) {

if (list.get(b) == list.get(i)) {

str = str + list.get(i);

list.remove(i);

}

}

int c = rand.nextInt(list.size() - 1);

for (int i = 0; i list.size(); i++) {

if (list.get(c) == list.get(i)) {

str = str + list.get(i);

list.remove(i);

}

}

int d = rand.nextInt(list.size() - 1);

for (int i = 0; i list.size(); i++) {

if (list.get(d) == list.get(i)) {

str = str + list.get(i);

list.remove(i);

}

}

int num = Integer.valueOf(str);// String轉化成int

return num;

}

文章名稱:猜四個數字java代碼,四個數字的代碼
文章來源:http://www.yijiale78.com/article4/hsicoe.html

成都網站建設公司_創新互聯,為您提供網站設計、Google定制網站、面包屑導航、網站建設手機網站建設

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網站建設公司