如何生成多个变量。在列表中,只需输入“变量”的特定数量。要求输入

发布于 2025-01-22 07:43:05 字数 277 浏览 1 评论 0原文

这是我在这里的第一篇文章,我也是一个有抱负的程序员,所以当您看到我有点胡说八道时,请不要对我太苛刻。

我遇到了一些问题,可以创建一个我想到的程序,该程序需要向用户询问要标记多少个学生的输入,用户将插入一定的数量(示例10)之后,将要求我们为10名学生中的每个学生都以百分比为单位。

我想到的是创建一个用于生成多个“变量”的循环,但我对每个和其中一个都有的互动方式迷失了方向,因此,如果您可以帮助我尝试解决这个问题问题将很棒,如果有更好的方法可以做到这一点,请尝试向我解释一下,好像您正在尝试向孩子解释一样。 XD

this is my first post here and I'm an aspiring programmer as well, so please when you see that I'm kind of talking nonsense don't go too harsh on me.

I'm having a bit of a problem creating a programme that I had in mind, this programme needs to ask the user for input on how many students are going to be marking, the user will insert a certain amount (example 10) after that is going to ask us to put for each of the 10 students a score in percentages.

what I had in mind was to create a For Loop to generate multiple “Variables” but I'm kind of lost on how I'm going to interact with each and one of them, so if you please could help me try to solve this problem that would be great, and please if there is a better way to do it please try to explain it to me as if you are trying to explain to a child. XD

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱贱哒 2025-01-29 07:43:05

最简单的方法是仅用于编写和阅读的循环。我不知道您为什么需要这个,但我希望它会有所帮助。

Scanner sc= new Scanner(System.in);
       System.out.println("Insert number of students: ");
                String student = sc.nextLine();
                int studenti = Integer.parseInt(student);
                 int[] array = new int[studenti];
                for (int i = 0; i<studenti;i++)
                {
                    System.out.println("Insert a score in procentage: ");
                    String score = sc.nextLine();
                    
                    array[i]= Integer.parseInt(score);
                }
                System.out.println("\n Procentages are: ");  
                for (int i=0; i<studenti;i++)
                {
                    System.out.println(array[i]);
                }

此致!

布拉兹

The most simple way is just to use for loop for writting and for reading. I don't know why you need this but i hope it helps.

Scanner sc= new Scanner(System.in);
       System.out.println("Insert number of students: ");
                String student = sc.nextLine();
                int studenti = Integer.parseInt(student);
                 int[] array = new int[studenti];
                for (int i = 0; i<studenti;i++)
                {
                    System.out.println("Insert a score in procentage: ");
                    String score = sc.nextLine();
                    
                    array[i]= Integer.parseInt(score);
                }
                System.out.println("\n Procentages are: ");  
                for (int i=0; i<studenti;i++)
                {
                    System.out.println(array[i]);
                }

Best regards!

Blaz

青衫儰鉨ミ守葔 2025-01-29 07:43:05

因此,您绝对需要一个阵列。您可以使用几种类型。
对于您的用例,标准阵列可以做到。

如果要遵守面向对象的样式,则应使用Student class。但是我现在将使用字符串来做:

int numberOfStudents = 10;
String[] students = new String[numberOfStudents];

// now you can do:
students[0] = "John";
students[1] = "Michael";
// ...


// print all students
for(String student : students){
   System.out.println(student);
}

另一种做到这一点的方法是列表。它们的性能比数组的成本要高一些,但是您不必知道它将拥有多少条目,因此更具说服力。看一下:

import java.util.ArrayList;
ArrayList<String> students = new ArrayList<Students>();

students.add("John");
students.add("Michael");
// ...


// print all students
for(String student : students){
   System.out.println(student);
}

So you definitely need an Array for this. There are several types you could use.
For you use-case the standard Array will do.

If you want to comply to object-oriented style, you should use a Student class. But I will do it with strings for now:

int numberOfStudents = 10;
String[] students = new String[numberOfStudents];

// now you can do:
students[0] = "John";
students[1] = "Michael";
// ...


// print all students
for(String student : students){
   System.out.println(student);
}

Another way of doing this is with Lists. They are a bit more performance costly than arrays but are way more convienient to work with as you don't have to know how much entries it will have. Look at this:

import java.util.ArrayList;
ArrayList<String> students = new ArrayList<Students>();

students.add("John");
students.add("Michael");
// ...


// print all students
for(String student : students){
   System.out.println(student);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文