JAVA 問題2 (已解決)

import java.util.Scanner;
public class Ex1
{
        public static void main(String[] args)
        {
                Scanner scan = new Scanner (System.in);
                String surname, firstname, country;
                int year;
                final int THISYEAR = 2008;
               
                System.out.print("Please enter your surname: ");
                surname = scan.nextLine();
                System.out.print("Please enter your first name: ");
                firstname = scan.nextLine();
                System.out.print("Please enter the year you were born: ");
                year = scan.nextInt();
                System.out.print("Please enter your country of birth: ");
                country = scan.nextLine();
               
                System.out.print("Hi "+ firstname +" "+ surname +", you're "+ (THISYEAR-year) +" this year and you were born in "+ country +".");
        }
}


紅色果part
點解我寫scan.next()就有得比user輸入
寫scan.nextLine()就會跳過左, 直接print out下面:funk:

[ 本帖最後由 silzai 於 2008-11-10 17:15 編輯 ]

原帖由 silzai 於 2008-11-10 14:00 發表
import java.util.Scanner;
import java.util.Random;

public class StarWarsNameGenerator
{
        public static void main(String[] args)
        {
                Scanner sc = new Scanner (System.in);
               
                String fi ...



點解你 firstname 同 familyname 未有內容, 就計 substring 呢 ??

TOP

method local variable does not have default values like instance variable does, so you must give them a initial value, before using it.

TOP

回覆 3# 的帖子

更新問題

[ 本帖最後由 silzai 於 2008-11-10 15:33 編輯 ]

TOP

原帖由 silzai 於 2008-11-10 14:00 發表
import java.util.Scanner;
public class Ex1
{
        public static void main(String[] args)
        {
                Scanner scan = new Scanner (System.in);
                String surname, firstname, country;
                int year;
                final int THISYEAR ...


因為scanInt 只係讀左你個integer, 無讀埋你個carriage-return. nextLine一見到你個carriage-retrun 就走左.
試下call 多一次nextLine.

TOP

原帖由 thinkpanda 於 2008-11-10 16:21 發表


因為scanInt 只係讀左你個integer, 無讀埋你個carriage-return. nextLine一見到你個carriage-retrun 就走左.
試下call 多一次nextLine.

thanks

TOP