为什么这样做:
Scanner keyboard = new Scanner(System.in);
int i;
int beerBottles = 100;
//Asks the user for the number of beer bottles on the wall
System.out.println("How many bottles of beer are on the wall?");
while (!keyboard.hasNextInt())
{
System.out.println("Make sure you enter an integer.");
keyboard.next();
}
//Sets beerBottles to the user entered value
beerBottles = keyboard.nextInt();
Run Code Online (Sandbox Code Playgroud)
我偶然发现这一点,同时试图确保用户输入是一个整数而不使用try/catch,我不知道它为什么会起作用,或者如果我错过了一些可怕的错误.它似乎工作得很好,这将是伟大的,但我不明白为什么"确保你输入一个整数"文本并不总是显示.谁能解释一下?
每次我尝试启动我的应用程序时,它都会立即崩溃,因为它收到此错误。
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simpleweatherandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="INTERNET"/>
<uses-permission android:name="ACCESS_NETWORK_STATE"/>
<application
android:exported = "true"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.simpleweatherandroid.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
我绝对获得了那里的许可,并且我已经用完了我可以在互联网上找到/找到的解决方案。我无法弄清楚这一点。有谁知道出了什么问题吗?我对此很陌生,所以这可能是一些基本的东西。