我需要对输入值加两个 /* *应用程序读取一个整数,并打印两个整数和输入值之间所有偶数整数的总和 */ 导入java.util.Scanner; 公共类偶数{ 公共静态void main(字符串[]args){ 整数; 扫描仪扫描=新扫描仪(System.in); System.out.println(“输入一个大于1的整数:”); number=scan.nextInt(); 打印号码(数字); }//端干管 /*声明名为number的int变量并在屏幕上显示它*/ 公共静态无效打印编号(整数编号){ 如果(数字

我需要对输入值加两个 /* *应用程序读取一个整数,并打印两个整数和输入值之间所有偶数整数的总和 */ 导入java.util.Scanner; 公共类偶数{ 公共静态void main(字符串[]args){ 整数; 扫描仪扫描=新扫描仪(System.in); System.out.println(“输入一个大于1的整数:”); number=scan.nextInt(); 打印号码(数字); }//端干管 /*声明名为number的int变量并在屏幕上显示它*/ 公共静态无效打印编号(整数编号){ 如果(数字,java,Java,我需要计算2和输入数的和,但是,它只取最后一个数,再加上2。谁能帮我解决这个问题。int sum=0; /* * Application the reads an integer and prints sum of all even integers between two and input value */ import java.util.Scanner; public class evenNumbers{ public static void main(String

我需要计算2和输入数的和,但是,它只取最后一个数,再加上2。谁能帮我解决这个问题。

int sum=0;
/*
 * Application the reads an integer and prints sum of all even integers between two and input    value
 */

import java.util.Scanner;

public class evenNumbers{

  public static void main(String [] args){
    int number;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter an Integer greater than 1:");
    number = scan.nextInt();
    printNumber(number);
  }// end main


  /*declares an int variable called number and displays it on the screen*/
  public static void printNumber(int number){
    if (number < 2){
      System.out.println("Input value must not be less than 2");
    }
    int sum = 2;
     if(number % 2==0){
       sum+= number;
     }
     System.out.println("Sum of even numbers between 2 and " + number + " inclusive is: " + sum);

  }//end printnumber
}
对于(整数电流=2;电流<代码>整数和=0;
对于(int current=2;current您需要一个循环。您的注释提示了正确的方向,但您应该查看Java教程,了解如何正确编写“for”循环。有三个部分:初始声明、终止条件和循环步骤。请记住,++运算符只向变量添加一个。您可以添加其他变量使用+=

输入测试值
可选:拒绝无效的测试值并**如果无效则带消息退出!**
将sum变量初始化为零

对于(将循环变量初始化为2;测试该循环var您需要一个循环。您的注释提示了正确的方向,但您应该查看Java教程以了解如何正确编写“for”循环。有三个部分:初始声明、终止条件和循环步骤。请记住,++运算符只向变量添加一个ble。您可以使用+=”添加其他值。如果使用+=向循环变量添加不同的值(如2),您可以跳过偶数的“如果”测试。您可以使用=比较运算符(对于原语)测试边界。因此,您需要类似的内容(在伪代码中,而不是Java):

输入测试值
可选:拒绝无效的测试值并**如果无效则带消息退出!**
将sum变量初始化为零

for(将循环变量初始化为2;测试该循环变量我是否应该添加类似for的内容(int number=2;number++)?是的-这里肯定需要一个for循环。不是故意给出答案,因为这看起来像是一个家庭作业问题:)我是否应该添加类似for的内容(int number=2;number++)是的-你肯定需要一个for循环。不要故意给出答案,因为这看起来像是一个家庭作业问题:)谢谢添加了这个int-sum=0;for(int-current=2;current谢谢添加了这个int-sum=0;for(int-current=2;current
 int sum = 0;
 for (int current = 2; current <= number; current += 2)
    sum += current;
 input the test value 
 Optional: reject invalid test value and **exit with message if it is not valid!** 
 initialize the sum variable to zero
 for ( intialize loop variable to 2; test that loop var <= test value; add 2 to loop var ) 
 { 
    add 'number' to the sum variable 
 } 
 display the sum