简体   繁体   English

您如何在Java中创建数学序列?

[英]How do you create a mathematical sequence in Java?

I want to declare integers, while the program is running. 我想在程序运行时声明整数。

I run the program, and then I give it via System.in.println an integer and repeat this as long as I want. 我运行该程序,然后通过System.in.println给它一个整数,并根据需要重复此操作。

I want the program to give those integers a name of a certain type for, for example a(i) or a[i] , dunno, (it should be handy) and then a(i) represents the the i 'th integer I gave the program. 我希望程序为这些整数指定某种类型的名称,例如a(i)a[i] ,dunno(应该很方便),然后a(i)代表第i个整数I给出了程序。
My idea is then that I can use those elements by their name just like, if I had declared them in the first place. 我的想法是,如果我首先声明了这些元素,则可以按其名称使用这些元素。
For example add two integers together. 例如,将两个整数相加。
For example I defined a method add+, which waits for 2 integer and then adds them. 例如,我定义了一个方法add +,它等待2个整数然后将它们相加。
For example I write: 例如我写:

add

a(2)

a(47) 

(then I would get here the result.) (然后我将得到结果。)

I don't think implementing the add function is difficult. 我认为实现add函数并不困难。 However I don't know, how to let the program count the number of inputs or how to let it declare and use variables. 但是我不知道如何让程序计算输入的数量或如何让它声明和使用变量。

First: Welcome to programming java; 第一:欢迎使用Java编程; it will be a long road. 这将是一条漫长的道路。

Here are some hints: 这里有一些提示:

  1. Use a List<Integer> to hold the sequence of numbers entered by the user. 使用List<Integer>保留用户输入的数字序列。
  2. Actually instanciate a concreate List class, for example LinkedList<Integer>'. If you need to access the elements by index, use an 实际上实例化一个concreate List类,例如LinkedList<Integer>'. If you need to access the elements by index, use an LinkedList<Integer>'. If you need to access the elements by index, use an ArrayList`.\\ LinkedList<Integer>'. If you need to access the elements by index, use an ArrayList`。
  3. Each time the user enters a number, create a new Integer and userList.add(newInteger); 每次用户输入数字时,创建一个新的Integer和userList.add(newInteger);

Simple sample 简单样本

List<Integer> userList = new LinkedList<Integer>();

for (index = 0; index < 9; ++index)
{
    Integer newInteger = new Integer(index);

    userList.add(newInteger);
}

for (Integer current : userList)
{
     System.out.println(current);
}

Yeah, I am following the conversation. 是的,我正在关注对话。 I am just a bit frustrated, because I can't really write any interesting or practical java programs (yet), because my knowledge isn't that big yet. 我有点沮丧,因为我还不能真正编写任何有趣或实用的Java程序,因为我的知识还不那么广。 First I tried to find out, if there was a way to add elements to array, because arrays seemed to me very useful, because each element of an array already has an address. 首先,我试图找出是否有一种向数组添加元素的方法,因为在我看来数组似乎非常有用,因为数组的每个元素都已经有一个地址。 I googled, and it seems that is not possible. 我用谷歌搜索,看来这是不可能的。 I might be able to use the idea with the list, but it seems to be that the length of the list has to have a limit and actually I wanted to avoid that. 我也许可以在列表中使用这个想法,但是似乎列表的长度必须有限制,实际上我想避免这种情况。

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

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