我如何检查一个给定的数字是否是#34;完美的方形" if 比较?
答案 0 :(得分:1)
假设我理解你在问什么,你可以先通过Math.sqrt
请求数字的平方根,然后在除法后得到其余部分以查看它是否等于0,在这种情况下,数字可以是除以平方根,所以是一个“完美”的方形
public void setAlarm () throws java.text.ParseException {
RealmResults<Reminder> realmResults = getAllLatest();
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Reminder reminder = getLatestReminder(realmResults);
int hour = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getHours();
int minutes = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getMinutes();
PendingIntent pendingIntent = getPendingIntentFromReminder(reminder);
Log.v("Reminder is: ", String.valueOf(reminder));
Calendar calendar = Calendar.getInstance();
//calendar.setTimeZone(TimeZone.getTimeZone());
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent);
}
private PendingIntent getPendingIntentFromReminder(Reminder reminder) throws ParseException {
int hour = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getHours();
int minutes = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getMinutes();
Intent intent = new Intent(this, MyReceiver.class);
intent.setAction(reminder.getName());
Uri data = Uri.withAppendedPath(Uri.parse("TYS"),
String.valueOf(reminder.getId()));
intent.setData(data);
Calendar calendar = Calendar.getInstance();
//calendar.setTimeZone(TimeZone.getTimeZone());
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
//Log.v("Test", calendar.getTimeZone().toString());
// Log.v("Hours to show", String.valueOf(hour));
// Log.v("Minutes to show", String.valueOf(minutes));
intent.putExtra("reminderTitle", reminder.getName());
// Log.v("Reminder Name", reminder.getName());
intent.putExtra("notificationType", 1);
intent.putExtra("reminderId", String.valueOf(reminder.getId()));
intent.putExtra("reminderSubtitle", reminder.getSubTitle());
intent.setAction(Long.toString(System.currentTimeMillis()));
Log.v("set Alarm", String.valueOf(intent));
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
return alarmIntent;
}
public void deleteReminderIntent(Reminder reminder) throws ParseException {
Log.v("deleteReminderIntent", String.valueOf(reminder));
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent =getPendingIntentFromReminder(reminder);
alarmMgr.cancel(pendingIntent);
pendingIntent.cancel();
//alarmMgr.cancel(temp);
// alarmMgr.cancel(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
要求用户年龄,这实际上取决于您的用例。提示是一种可能的方法,虽然您也可以通过提供表单来实现,这样可以获得html5输入类型function isSquare( number ) {
// get the square root of the number in question
var sqrt = Math.sqrt(number);
// check if the modulo (rest after dividing is 0)
return ((number % sqrt) === 0);
}
console.log(isSquare(9)); // true
console.log(isSquare(5)); // false
的好处,如下所示:
number
function checkAge() {
var inputElement = document.getElementById('ageInput'),
value = parseInt( inputElement.value ),
outputElement = document.getElementById('output');
if ( Number.isNaN( value ) ) {
output.innerHTML = '<error>Please enter a valid number</error>';
return;
}
if (value % 2 === 0) {
output.innerHTML = 'Your age is even';
} else {
output.innerHTML = 'Your age is odd';
}
}
error {
color: red;
}
答案 1 :(得分:0)
&#34; best&#34;检查数字是否为奇数的方法是主观的。显示的方式肯定会奏效。但是,在实际代码中,您需要确保捕获无效年龄,方法是检查age % 2
是NaN
还是使用parseInt(prompt("what is your age?"), 10)
之类的内容,然后检查Number.isNaN
中的输出}。
答案 2 :(得分:0)
if(age % Math.sqrt(age) === 0) {
console.log("Your age is a perfect square!");
}
我找到答案..谢谢大家(: