[英]Updating Values in ArrayList in Java
尝试更新 ArrayList 中的费用,一旦选择了选项 3,一种是输入 reg no 然后为该特定学生添加费用。 输入附加费用后,应将附加费用添加到初始设置的费用中,然后更新 ArrayList 中的总费用。 请帮忙。 当我运行我的代码并添加多个学生时,我为我输入的最后一个学生设置的初始费用会更新给所有学生
这是我的代码...
package schoolfinance;
import java.util.*;
public class Student
{
private final String m_name;
private final String m_regNo;
private final String m_course;
private final String m_year;
static double m_fees;
final double total_fees = 50000;
public Student(){
this("", "", "", "", 0.0);
}
public Student( String name, String regNo, String course, String year, double fees )
{
m_name = name;
m_regNo = regNo;
m_course = course;
m_year = year;
m_fees = fees;
}
public String getName()
{
return m_name;
}
public String getAge()
{
return m_regNo;
}
public String getCourse()
{
return m_course;
}
public String getYear()
{
return m_year;
}
public double getFees()
{
return m_fees;
}
public void setFees(Double fees)
{
Student.m_fees = fees;
}
/**
*
* @return
*/
@Override
public String toString()
{
double balance = total_fees - m_fees;
return "Name: " + m_name + ",\t Reg No: " + m_regNo +
",\t Course: " + m_course + ",\t Year: " + m_year +
",\t Fees Paid: " + m_fees + ",\t Balance: " + balance;
}
public static void main(String[] args)
{
ArrayList<Student> students = new ArrayList<>();
try (Scanner input = new Scanner(System.in)) {
int menuChoice = 4;
do {
System.out.println("\t\t\tStudent Record Menu");
System.out.println("\t\t1. Add Student\t2. View Students\t3. Add Fees\t4. Exit");
try {
System.out.println("Enter a choice: ");
menuChoice = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
continue;
}
if (menuChoice==1)
{
System.out.println("Full name:");
String name = input.nextLine();
System.out.println("Registration Number:");
String regNo = input.nextLine();
System.out.println("Course:");
String course = input.nextLine();
System.out.println("Year:");
String year = input.nextLine();
float fees = -1;
do {
try {
System.out.println("Fees Paid:");
fees = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
System.out.println("Enter a number!");
}
} while (fees <= 0);
Student student = new Student(name, regNo, course, year, fees);
students.add(student);
} else if (menuChoice==2) {
System.out.println("Students:");
for (Student student : students){
System.out.println(student);
}
} else if (menuChoice==3){
Scanner in = new Scanner(System.in);
System.out.println("Enter Student Registration Number: ");
String id = in.nextLine();
students.forEach((Student student) -> {
if(student.m_regNo.equals(id)){
System.out.println("Found it");
System.out.println(student);
Scanner new_fee = new Scanner(System.in);
System.out.println("Enter Added Fee: ");
double added_fee = new_fee.nextFloat();
double add_fee = added_fee + m_fees;
System.out.println(add_fee);
student.setFees(add_fee);
}
System.out.println("No Student Record Found Matching that Registration Number!!");
});
}else if (menuChoice < 1 || menuChoice > 4) {
System.out.println("Unrecognized Menu Choice, Please re-enter");
}
} while (menuChoice != 4);
}
}
}
当我运行代码并选择选项 1 时,它会提示输入详细信息,例如全名、注册号、课程、年份和费用,即全名:Billy Dan 注册号:BIT100 课程:IT 年份:3 费用:10,000
如果我再次选择选项 1 并添加另一个学生的详细信息,我将在第二个学生中插入的费用值也会为我不想要的第一个学生更新。
现在,如果选择了选项 3,系统将提示您输入要更新其费用的特定学生的注册号。 如果找到注册号,它将吐出该学生的详细信息,然后提示您输入新费用。 所以我想当我输入新费用时,程序会收取这个新费用,将其添加到初始费用中,然后将总费用保存为初始费用。
该程序现在运行良好,尽管最后输入的费用会更新到所有其他学生,例如,如果我的第一个学生的费用为 5000,那么我添加另一个学生并插入 7000 作为他/她的费用,7000 将覆盖第一个学生的5000,所以他们最终有相同的费用
我无法弄清楚您要说什么,但这是如何在ArrayList
中更新java
中的值的简单示例
import java.util.ArrayList;
public class UpdateArrayListValues {
public static void main(String args[]) {
ArrayList<Integer> arraylist = new ArrayList<Integer>();
arraylist.add(1);
arraylist.add(2);
arraylist.add(3);
arraylist.add(4);
arraylist.add(5);
arraylist.add(6);
arraylist.add(7);
System.out.println("ArrayList before update: "+arraylist);
//Updating 1st element
arraylist.set(0, 11);
//Updating 2nd element
arraylist.set(1, 22);
//Updating 3rd element
arraylist.set(2, 33);
//Updating 4th element
arraylist.set(3, 44);
//Updating 5th element
arraylist.set(4, 55);
System.out.println("ArrayList after Update: "+arraylist);
}
}
Here is the output
ArrayList before update: [1, 2, 3, 4, 5, 6, 7]
ArrayList after Update: [11, 22, 33, 44, 55, 6, 7]
要更新 Student 对象的值,您需要提供允许您这样做的 class 方法。 您目前只有 getter 来返回值,例如
public String getName()
{
return m_name;
}
您需要做的是为这些属性添加设置器以及 eG
public void setName(String name)
{
this.m_name = name;
}
请注意,为了能够更新字段的值,您需要从中删除final修饰符!
由于 final 修饰符表明无法重新分配变量,您需要决定是否删除它们并使字段不是最终的,或者您需要接受这样一个事实,即学生 class 的属性无法更改并使用它。
然后你可以在你的代码中使用那些你已经找到你想要修改的学生 object 的设置器:
System.out.println("Found it");
System.out.println(student);
student.setName("some new Name");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.