简体   繁体   English

如何在一个变量中添加不同的数字?

[英]how can i add different numbers in one variable?

int courses = input.nextInt();

for (int i = 1; i <= coses; i++) {

    System.out.println("Course No" + i);

    System.out.println("Enter course title : ");

    String title = input.next();

    System.out.println("Enter course grade : ");

    String grade = input.next();

    System.out.println("Enter course unit : ");

    int unit = input.nextInt();

    System.out.println("");

}

from the code above, i want to add all the values store in the "int unit" variable, into the number of times specified by the inputter. 从上面的代码中,我想将存储在“ int unit”变量中的所有值添加到输入程序指定的次数中。

Use This: 用这个:

int courses = input.nextInt();
        int unit =0;

    for(int i = 1;i<= courses ;i++)
    {

        System.out.println("Course No" + i);

        System.out.println("Enter course title : ");

        String title = input.next();

        System.out.println("Enter course grade : ");

        String grade = input.next();

        System.out.println("Enter course unit : ");

        unit = unit + input.nextInt();

        System.out.println("");

    }

Please clarify, you want to take all the keyboard inputs and store them all within a variable called "int unit"? 请澄清一下,您是否要接受所有键盘输入并将它们全部存储在一个名为“ int unit”的变量中?

Or you do you want to do some algebra to get a number (like a class average) and store that number in an variable? 还是您想做一些代数运算以获得一个数字(如班级平均值)并将该数字存储在变量中?

Or do you want separate variables for each thing they would input that would store it for every iteration of the for loop? 还是您想为它们输入的每件事分配单独的变量,以将其存储在for循环的每次迭代中? (So like a 'courseNumber', 'courseTitle', 'courseGrade', 'courseUnit' variables) (因此类似于“ courseNumber”,“ courseTitle”,“ courseGrade”,“ courseUnit”变量)

If you can give me a little more clarification I can help you. 如果您能给我更多的说明,我可以为您提供帮助。

Non-static way. Non-static方式。 Initialized a variable before the for loop as int unit = 0; for循环之前将变量初始化为int unit = 0; .

int courses = input.nextInt();
int unit = 0;

for (int i = 1; i <= courses ; i++) {
   System.out.println("Course No" + i);       
   unit += input.nextInt();
}

System.out.println(unit);

Or you can implement static variable as static int unit; 或者,您可以将static变量实现为static int unit; .

class Sum{
  static int unit;
  public static void main (String[] args){

     Scanner input= new Scanner(System.in);
     int courses = input.nextInt();

     for (int i = 1; i <= courses ; i++) {
       System.out.println("Course No" + i);       
       unit += input.nextInt();
     }

     System.out.println(unit);
  }
}

To view the out-put add System.out.println(unit); 要查看输出,请添加System.out.println(unit); outside of the for loop. for循环之外。

unit += input.nextInt(); will add all unit results. 将添加所有单位结果。 This is same as unit = unit + input.nextInt(); 这与unit = unit + input.nextInt();相同unit = unit + input.nextInt();

public class add{
    static int unit;
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        int courses = input.nextInt();

        for (int i = 1; i <= courses; i++) {

            System.out.println("Course No" + i);
            System.out.println("Enter course title : ");
            String title = input.next();

            System.out.println("Enter course grade : ");
            String grade = input.next();

            System.out.println("Enter course unit : ");
            unit = unit + input.nextInt();

            System.out.println("");

        }
        System.out.println("Unit:" + unit);
    }
}

You should try some thing like this 你应该尝试这样的事情

  int unit = unit + input.nextInt();

It will be wise to bring all the variable declarations outside the for loop.. 将所有变量声明带到for循环之外是明智的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM