ÒÔÇ°×öËã·¨Ì⣬¶¼ÊÇʵÏÖÒ»¸ö·½·¨£¬ÐèÒªµÄ²ÎÊý»áÔÚ·½·¨²ÎÊýÖÐÖ±½Ó¸ø³ö£¬¶øÇÒÐèÒªµÄ·µ»ØÖµÖ±½ÓÔÚ·½·¨ÖÐ return ¾ÍºÃÁË¡£µ«ÊÇ°¢ÀïµÄ±ÊÊÔ£¬ÐèÒªµÄ²ÎÊý¾ÓȻҪµ½ÊäÈëÁ÷ÖжÁÈ¡£¬¶øÇÒ·µ»Ø½á¹û¾ÓȻֱ½ÓÊä³öµ½¿ØÖÆ̨ÉÏ¡£
Èç¹ûûÓмû¹ýÕâÖÖÌ×·µÄ£¬µ±Ê¹ÓÃJavaÊäÈëÀànextLine·½·¨¶ÁÈ¡ÊäÈëÁ÷ÖеÄ×Ö·û´®Ê±£¬×Ü»áĪÃûÆäÃîµØÉÙ¶ÁÒ»²¿·Ö£¡ÄÇÔÚÕâÀÈÃÎÒÃÇлªµÄרҵ½²Ê¦À´¸ø´ó¼Ò´øÀ´Ò»³¡×¨ÒµµÄ·ÖÎö¡£
¶ÁÈ¡Ò»¸öÊý×Ö
¼ÈÈ»ÎÒÃÇÒª¶ÁÈ¡ÊäÈ룬¿Ï¶¨µÃÓõ½ Scanner À࣬Õâ¸öÀ൹ÊÇͦ¼òµ¥µÄ£¬ÀýÈ磬ÎÒÃÇÐèÒª¶ÁÈ¡Ò»¸öÊý×Ö£¬¿ÉÒÔÕâÑù×ÓÉè¼Æ³ÌÐò¡£
package test;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
System.out.println("¶ÁÈ¡µ½µÄÊý×ÖÊÇ£º" + i);
}
}
ÎÒÃÇËæÒâÊäÈëÒ»¸öÊý×Ö 100£¬¿ØÖÆ̨µÄÊä³öÒ²Ö¤Ã÷ÁËÎÒÃǵijÌÐò¿ÉÒÔ¶ÁÈ¡Õâ¸öÊý×Ö¡£
ºÃÁË£¬ÖصãÀ´ÁË£¡¶ÁÈ¡Êý×Öµ¹Ã»Ê²Ã´£¬¶ÁÈ¡×Ö·û´®»áÒªÁËÀÏÃü£¡¶ÁÈ¡Êý¾ÝµÄʱºòÈç¹ûÒ»Ö±³¢ÊÔʹÓÃnextLineÕâ¸ö·½·¨À´¶ÁÈ¡×Ö·û´®£¨Èç¹ûÎÒÃDz»ÖªµÀnextÕâ¸ö·½·¨Ê±£©£¬nextLine¶Á³öÀ´µÄÊý¾Ý×ÜÊÇȱ½ïÉÙÁ½£¡
ÎÒÃÇÀ´»¹ÔÒ»ÏÂÏÖ³¡£¬ÐèÇóÊÇÕâÑùµÄ£¬ÐèÒªÎÒÃǶÁÈ¡Ò»¸öÊý×Ö£¬Õâ¸öÊý×Ö±íʾÊäÈë×Ö·û´®µÄÊýÁ¿£¬È»ºóÏÂÃæ¸ú×ŵľÍÊÇһϵÁÐÐèÒª¶ÁÈëµÄ×Ö·û´®ÁË£¬ºÜ¶àÈËÔÚ×öÕâµÀÌâʱÏ붼ûÏ룬´ó±ÊÒ»»Ó£¬Ð´ÏÂÈçÏ´úÂ룺
package test;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();//¶ÁÈ¡µÄ×Ö·û´®ÊýÁ¿
int index = 0;
String[] array = new String [i];//´æ·Å¶ÁÈ¡µÄ×Ö·û´®
while(index < i) {
array[index++] = sc.nextLine();
}
for (String str : array) {//Êä³ö¶ÁÈ¡µÄ×Ö·û´®
System.out.println("ÊäÈëµÄ×Ö·û´®£º" + str);
}
}
}
ÀíÏëºÜÃÀºÃ£¬ÏÖʵºÜ¹Ç¸Ð£¬Ð´´úÂëʱ¹ââÍòÕÉ£¬¼ì²éʱȴԹÌìÓÈÈË¡£ÕâÑùµÄ´úÂëÓÀÔ¶ÎÞ·¨¶ÁÈ¡×îºóÒ»ÐÐÊäÈëµÄ×Ö·û´®£¬ÕâÊÇΪɶÄØ£¿
ÒòΪnextLine¿ÉÒÔ½ÓÊÕ¿Õ¸ñ»òÕßtab¼ü£¬ÆäÊäÈëÒÔenter¼ü½áÊø¡£
³ÌÐòÔÚͨ¹ý nextInt ·½·¨¶ÁÈ¡Êý×ÖÖ®ºó£¬»áÁôÏÂÒ»¸öenter¼ü£¬Õâ¸öenter¼ü»á±»µÚÒ»¸öÖ´ÐеÄnextLineº¯Êý½ÓÊÕ£¬Õâµ¼ÖÂÎÒÃÇÄܹ»¶ÁÈ¡µÄÊý¾ÝÓÀÔ¶ÉÙÒ»ÐУ¨nextLineÉÙÖ´ÐÐÒ»´Î£©£¬Í¬Ê±Ò²ÄܽâÊÍΪʲô×Ö·û´®Êý×éµÚÒ»¸öÔªËØΪ¿Õ¡£
Õâ¸öÎÊÌâÔõô½â¾öÄØ£¿Java ÊäÈëÀà¶ÁÈ¡×Ö·û´®µÄº¯Êý²¢·ÇÖ»ÓÐnextLine£¬Í¨¹ýnextÒ²ÄÜʵÏÖÀàËƵŦÄÜ£¬Ö»²»¹ýnextÓënextLine²»Í¬£¬next²»»á½ÓÊջسµ·û£¬tab»òÕß¿Õ¸ñ¼ü¡£ÔÚÎÒÃÇʹÓà nextLineʱ£¬ÐèҪעÒâÇ°ÃæÊÇ·ñÓÐenter¼ü£¬Èç¹ûÓУ¬¾Í»á±»nextLineÎüÊÕ£¬´Ó¶øÔì³Ébug£¡
¼ÈÈ»Ã÷°×ÁËnextLineÓënextµÄÔÀí£¬½â¾öÎÊÌâ¾Í±äµÃ¼òµ¥ÁË£¬½«¶ÁÈ¡×Ö·û´®µÄº¯ÊýÓÉnextLine¸ÄΪnext±ã¿É£¬ÕâÑù¾Í²»»áÎüÊÕnextIntÁôϵÄenter¼üÁË¡£¸Ä½ø¹ýµÄ³ÌÐòÈçÏ£º
package test;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();//¶ÁÈ¡µÄ×Ö·û´®ÊýÁ¿
int index = 0;
String[] array = new String [i];//´æ·Å¶ÁÈ¡µÄ×Ö·û´®
while(index < i) {
array[index++] = sc.next();
}
for (String str : array) {//Êä³ö¶ÁÈ¡µÄ×Ö·û´®
System.out.println("ÊäÈëµÄ×Ö·û´®£º" + str);
}
}
}
×ܽá
×îºóÎÒÃÇÔÙÀ´×ܽáÒ»ÏÂnextLineÓënextÁ½¸ö·½·¨µÄÇø±ð£º
next²»»á½ÓÊջسµ·û£¬tab»òÕß¿Õ¸ñ¼ü£¬ÔÚ½ÓÊÕÓÐЧÊý¾Ý֮ǰ»áºöÂÔÕâЩ·ûºÅ£¬ÈôÒѾ¶ÁÈ¡ÁËÓÐЧÊý¾Ý£¬Óöµ½ÕâЩ·ûºÅ»áÖ±½ÓÍ˳ö£¬¶ønextLine ¿ÉÒÔ½ÓÊÕ¿Õ¸ñ»òÕßtab¼ü£¬ÆäÊäÈëÒÔ enter ¼ü½áÊø¡£
Èç¹ûÒ²ÏëÏñÒ»Ãû×ÊÉî³ÌÐòÔ±Ò»Ñù½â¾öÈç´ËרҵµÄÎÊÌ⣬¿ìµ½Ê¯¼ÒׯлªµçÄÔѧУÀ´Ñ§Ï°°É£¡