我剛開始學習java,我想做一個簡單的程序,它要求人 名稱,輸出名稱然後要求他們最喜歡的數字。然後,它會將它們的編號 與編號6進行比較,並根據編號是大於還是小於6來輸出。如何使用java.util.Scanner和整數
我在Netbeans中得到一個「String to int convert」錯誤,它與掃描器。 希望我正確地問這個問題,但我怎樣才能使掃描儀拾取整數? 感謝
package javaapplication2;
import java.util.Scanner;
import java.lang.String;
public class JavaApplication2 {
public static void main(String[] args) {
// Creating an instance of the scanner class.
// Gets name and numbers.
Scanner getName = new Scanner(System.in);
Scanner getNumber = new Scanner(System.in);
//Holds name and number
String userName;
int userNumber;
// Asks for the users name.
// Holds name in userName.
System.out.println("What is your name?");
userName = getName.nextLine();
//Reponds with the users name.
System.out.println("Hello" + userName + "!");
//Asks for favorite number.
// Holds number in userNumber.
System.out.println("What is your favorite number?");
userNumber = getNumber.nextLine();
// Checks if users number is larger than 6.
if (userNumber > 6) {
// Stuff goes here.
}
}
}
爲了學習Java API,你必須有'documentation' - http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html – adatapost 2014-10-04 08:34:21