Cov txheej txheem:

Java Choice Maker: 13 Kauj Ruam
Java Choice Maker: 13 Kauj Ruam

Video: Java Choice Maker: 13 Kauj Ruam

Video: Java Choice Maker: 13 Kauj Ruam
Video: Coding for 1 Month Versus 1 Year #shorts #coding 2024, Kaum ib hlis
Anonim
Java Choice Maker
Java Choice Maker

Cov txheej txheem qhia no yuav qhia yuav tsim lub program java li cas uas yuav ua rau xaiv los ntawm cov npe xaiv uas tau nkag los ntawm tus neeg siv. Kev paub ua haujlwm yooj yim ntawm java thiab IDE los tsim cov haujlwm hauv. Txhua kauj ruam yuav tsum siv sijhawm tsis pub ntau tshaj 2 feeb.

Kauj ruam 1: Ntshuam

Ntshuam
Ntshuam

Ntshuam lub scanner thiab Random chav kawm hauv java

ntshuam java.util. Scanner;

ntshuam java.util. Random;

Kauj Ruam 2: Teeb Txoj Hauv Kev Loj

Teeb Txoj Hauv Kev
Teeb Txoj Hauv Kev

Teeb tsa lub luag haujlwm tseem ceeb hauv java

pej xeem zoo li qub tsis muaj dab tsi tseem ceeb (Txoj hlua args) {}

Kauj Ruam 3: Tshaj tawm lub Scanner

Tshaj tawm lub Scanner
Tshaj tawm lub Scanner

Pib ua ntej thiab tshaj tawm qhov hloov pauv rau lub tshuab luam ntawv hauv qhov no kuv tau hu ua qhov hloov pauv tau

Scanner scan = scanner tshiab (System.in);

Kauj Ruam 4: Tus Zauv Xaiv

Qhia tus neeg siv rau tus lej ntawm kev xaiv.

Kauj Ruam 5: Txheeb Rau Tus Zauv Ntawm Qhov Xaiv

Luam theej duab rau Cov Xaiv
Luam theej duab rau Cov Xaiv

Siv lub tshuab ntsuas qhov ntsuas kom nkag mus rau tus lej ntawm cov kev xaiv thiab khaws cia hauv qhov sib txawv hauv qhov no numChoices

int numChoices = scan.nextInt ();

Kauj Ruam 6: Pib Pib Array

Initializing Array
Initializing Array

Pib ib qho array nrog ntau lub ntsiab lus raws li koj muaj kev xaiv hauv qhov no stringArray

Txoj hlua stringArray = Txoj hlua tshiab [numChoices+1];

Kauj Ruam 7: Ua Lub Loop

Ua lub Loop
Ua lub Loop

Sau rau lub voj siv lub txee pib rau 0 kom dhau los ntawm kab

rau (int kuv = 0; kuv <stringArray.length; kuv ++) {}

Kauj Ruam 8: Xaiv Kev Xaiv

Qhia tus neeg siv rau kev xaiv

Kauj ruam 9: Luam theej duab hauv Xaiv

Luam theej duab hauv Cov Xaiv
Luam theej duab hauv Cov Xaiv

Siv lub tshuab luam ntawv txhawm rau nkag koj cov kev xaiv rau hauv kab

stringArray [kuv] = scanner.nextLine ();

Kauj ruam 10: Tshaj tawm Random

Tshaj tawm Random
Tshaj tawm Random

Tshaj tawm qhov sib txawv rau qhov xwm txheej hauv qhov no nws yog lub npe rand (nco ntsoov ua qhov no sab nraum lub voj)

Random Random = tshiab Random ();

Kauj Ruam 11: Tsim Tus Zauv Random

Tsim Tus Zauv Random
Tsim Tus Zauv Random

Tsim tus lej random siv Rand thiab muab nws qhov sib txawv hauv qhov no randomChoice

int randomChoice = rand.nextInt (numChoices);

Kauj Ruam 12: Luam Ntawv Xaiv

Luam Ntawv Xaiv
Luam Ntawv Xaiv

Siv cov lej uas tsim los nyob rau hauv kab lus thiab luam tawm cov ntsiab lus ntawm qhov ntsuas ntawd

System.out.print (stringArray [randomChoice]);

Kauj Ruam 13: Nrog koj zoo siab

Koj yuav tsum muaj txoj haujlwm uas ntsuas hauv ntau qhov kev xaiv thiab luam tawm ib qho ntawm cov kev xaiv ntawd ntawm qhov xwm txheej.

Yog tias koj tau txais qhov ntsuas ntsuas tawm ntawm qhov tsis raug yuam kev txheeb xyuas koj lub voj voos. Nco ntsoov xyuas koj cov cai rau kev siv cov cim ib nrab. nco ntsoov tias java yog cov ntaub ntawv rhiab!

Pom zoo: