Yuav ua li cas koj ntes ib txoj hlua hauv Java?
Yuav ua li cas koj ntes ib txoj hlua hauv Java?

Video: Yuav ua li cas koj ntes ib txoj hlua hauv Java?

Video: Yuav ua li cas koj ntes ib txoj hlua hauv Java?
Video: YUAV COJ LI CAS TU NEEG KOJ HLUB THIAJ NCO KOJ 2024, Tej zaum
Anonim
  1. ntshuam java ua . siv. Lub tshuab luam ntawv;
  2. chav kawm Demo {
  3. public static void main( Txoj hlua args) {
  4. Txoj hlua s;
  5. Scanner sc = tshiab Scanner(System. in);
  6. Qhov system. tawm. println("Enter a txoj hlua ");
  7. s = sc. nextLine();
  8. Qhov system. tawm. println("Koj nkag mus Txoj hlua "+s);

Raws li txoj cai, koj ua li cas tso cov hlua hauv Java?

Sau ib kab hauv Java Programming, ua ntej koj yuav tsum nug tus neeg siv nkag mus rau hauv txoj hlua thiab qhov chaw ntawd txoj hlua nyob rau hauv txhua qhov sib txawv hais tias str, thiab tam sim no tso qhov sib txawv str nyob rau hauv lub bracket ntawm System. tawm. luam().

Ib tug kuj nug, yuav ua li cas kuv thiaj li tau txais txoj hlua scanner? Nyeem ib txoj hlua los ntawm console input hauv Java

  1. import java. siv. Lub tshuab luam ntawv;
  2. public static void main(String args) {
  3. Qhov system. tawm. print("Sau ib txoj hlua: ");
  4. Scanner scanner = tshiab Scanner(System. in);
  5. Txoj hlua inputString = scanner. nextLine();
  6. Qhov system. tawm. println("Txoj hlua nyeem los ntawm console yog: " + inputString);

Ib yam li ntawd, koj yuav ua li cas thiaj li lees txais txoj hlua los ntawm lub scanner hauv Java?

  1. import java. siv. Lub tshuab luam ntawv;
  2. chav ScannerDemo{
  3. public static void main(String args){
  4. Scanner sc = tshiab Scanner(System. in);
  5. kab lus = sc. tom ntej ();
  6. //String lo lus = sc.nextLine();
  7. Qhov system. tawm. println(lo lus);
  8. }

Kuv yuav rho tawm ib lo lus tshwj xeeb los ntawm ib txoj hlua hauv Java li cas?

Txoj kev yooj yim tshaj rau rho tawm tus lus yog siv split() ntawm txoj hlua chav kawm raws li hauv qab no piv txwv: /**

Piv txwv li:

  1. chav piv txwv.
  2. {
  3. pej xeem static void main(String args)
  4. {
  5. String str = "Hav no yog Ram";
  6. String lus = str. split(" ", 3);
  7. rau (String word: words)
  8. Qhov system. tawm. println(lo lus);

Pom zoo: