3 回答

TA貢獻1890條經驗 獲得超9個贊
還是一樣。
import java.util.Scanner;
public class Main
{
? ? public static void main(String[] args)
? ? {
? ? ? ? Scanner kbd = new Scanner(System.in);
? ? ? ? System.out.print("Word:");
? ? ? ? String s = kbd.nextLine();
? ? ? ? System.out.println("Input:" +s); // print the string
? ? ? ? System.out.println("Length:" + s.length()); // the length of the string
? ? }
}

TA貢獻1812條經驗 獲得超5個贊
我不明白你想要什么。
import java.util.Scanner;
public class lab
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
System.out.print("1st string: ");
String s = kbd.nextLine();
System.out.print("2nd string: ");
String s1 = kbd.nextLine();
System.out.println(s.length()); // this is for the length
System.out.println(s1); // this if for the input only
}
}

TA貢獻1111條經驗 獲得超0個贊
import java.util.Scanner;
public class lab {
public static void main(String [] args)
{
Scanner kbd = new Scanner(System.in);
String s = kbd.next();
String s1 = kbd.nextLine();
System.out.println(s.length());
System.out.println(s);
System.out.println(s1);
}
}
Scanner沒有lenght(),Length()方法僅適用于字符串變量
您使用哪個 IDE(Eclipse、Netbeans、Intellij?),因為 IDE 通常會向您顯示錯誤
添加回答
舉報