如何在Math.floor中使用变量

时间:2019-03-30 23:20:03

标签: javascript bots discord discord.js

我想获得一个人拥有的最大金钱

let targetUser = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0]);
var targetForMath = userData[targetUser.id].money;

,我想使用该变量设置Math.floor的最大数量。我尝试过

let randomStealCoins = Math.floor(Math.random() * targetForMath) + 1;

但是当我使用它时,它会给我NaN。我该怎么做才能给我一个实际的实际数字,而不是奇怪的东西?

2 个答案:

答案 0 :(得分:1)

如果未定义targetForMath,将为您提供NaN: 由于:

Math.random() * undefined -> NaN

我将确保targetForMath是有效数字。

答案 1 :(得分:1)

除了要说您正在做{}}和var,并且您应该注意这一点之外,我不会说您正在混用lettypeof。 我要问的第一个问题是userData[targetUser.id].money null是什么,更具体地说,它的 value 是什么。如果它是数字,布尔值,Math.random()或空数组(?!),则它应该可以工作,或者至少不返回NaN。但是,如果它是字符串,未定义或任何其他值,则JavaScript不会真正说出您的意思,例如,将"fish"乘以console.log

通常,在这种情况下,userData[targetUser.id]userData是个好主意,只是要确保从中得到的对象实际上就是您认为的那样。由targetUser的ID定义的 { //function Future getImage(bool isCamera ) async { print(images); List<File> imagestemp = []; if(isCamera){ // the problem is in this section imagestemp = await MultiMediaPicker.pickImages(source: ImageSource.camera,singleImage: true); print(imagestemp);} else { imagestemp= await MultiMediaPicker.pickImages(source: ImageSource.gallery); } setState(() { images = [images, imagestemp].expand((x) => x).toList(); }); } ERROR: com.example.hackathonstar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference E/AndroidRuntime(22980): at android.app.ActivityThread.deliverResults(ActivityThread.java:4360) E/AndroidRuntime(22980): at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402) E/AndroidRuntime(22980): at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) E/AndroidRuntime(22980): at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) E/AndroidRuntime(22980): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) E/AndroidRuntime(22980): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) E/AndroidRuntime(22980): at android.os.Handler.dispatchMessage(Handler.java:106) E/AndroidRuntime(22980): at android.os.Looper.loop(Looper.java:193) E/AndroidRuntime(22980): at android.app.ActivityThread.main(ActivityThread.java:6669) E/AndroidRuntime(22980): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(22980): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) E/AndroidRuntime(22980): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) E/AndroidRuntime(22980): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference E/AndroidRuntime(22980): at io.flutter.plugins.imagepicker.ImagePickerDelegate$3.getFullImagePath(ImagePickerDelegate.java:145) E/AndroidRuntime(22980): at io.flutter.plugins.imagepicker.ImagePickerDelegate.handleCaptureImageResult(ImagePickerDelegate.java:393) E/AndroidRuntime(22980): at io.flutter.plugins.imagepicker.ImagePickerDelegate.onActivityResult(ImagePickerDelegate.java:354) E/AndroidRuntime(22980): at io.flutter.app.FlutterPluginRegistry.onActivityResult(FlutterPluginRegistry.java:210) E/AndroidRuntime(22980): at io.flutter.app.FlutterActivityDelegate.onActivityResult(FlutterActivityDelegate.java:143) E/AndroidRuntime(22980): at io.flutter.app.FlutterActivity.onActivityResult(FlutterActivity.java:142) E/AndroidRuntime(22980): at android.app.Activity.dispatchActivityResult(Activity.java:7454) E/AndroidRuntime(22980): at android.app.ActivityThread.deliverResults(ActivityThread.java:4353) E/AndroidRuntime(22980): ... 11 more 对象上实际上有一个属性吗?