![](/img/trans.png)
[英]using a for loop in javascript write a code that prompts the user to enter any amount of numbers the print the largest and smallest of those numbers
[英]Write a program that prompts the user to input two values, using loop & Add up all even numbers between the initial value and the finish value [on hold]
有想法嗎? 編寫一個程序,提示用戶輸入兩個值——開始值和結束值。 第一個值是該范圍內的初始/較低值,第二個值是該范圍內的結束/較大值。 將初始值和結束值之間的所有偶數相加。
這是一項非常簡單的任務,您應該自己嘗試。使用掃描儀 class 進行用戶輸入(您必須導入: import java.util.Scanner; )並使用模數來查找偶數(例如:i % 2 == 0 )。在這種情況下,我會使用 for 循環。
這應該讓你開始:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please input your first number");
long first = scanner.nextLong();
System.out.println("Please input your second number");
long second = scanner.nextLong();
long answer = 0;
while (first < second) {
if (first % 2 == 0) {
answer += first;
}
first++;
}
System.out.println("answer = " + answer);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.