gpt4 book ai didi

java - 查找当前索引并打印数组中的下一条记录

转载 作者:行者123 更新时间:2023-12-01 18:10:14 25 4
gpt4 key购买 nike

我有一个程序,有5个按钮。该按钮用于从数组中查找第一个、最后一个、下一个和上一个记录。我已经完成了第一条和最后一条记录,但我对从数组中查找下一条和上一条记录感到困惑。 Output

public void fn(String s2)
{
try
{
BufferedReader br=new BufferedReader(new FileReader("Student.txt"));
String s=br.readLine();
Book[] b=Book.StringToObj(s);

if(s2.equals("Read Data"))
{
l5.setText(String.valueOf(b[0].id));
l6.setText(String.valueOf(b[0].title));
l7.setText(String.valueOf(b[0].price));
l8.setText(String.valueOf(b[0].auth));
}

if(s2.equals("<<"))
{
if(l5.getText().equals(String.valueOf(b[0].id))&&l6.getText().equals(String.valueOf(b[0].title))&&l7.getText().equals(String.valueOf(b[0].price))&&l8.getText().equals(String.valueOf(b[0].auth)))
{
JOptionPane.showMessageDialog(this,"You are already at First Record");
}
else
{
l5.setText(String.valueOf(b[0].id));
l6.setText(String.valueOf(b[0].title));
l7.setText(String.valueOf(b[0].price));
l8.setText(String.valueOf(b[0].auth));
}

}

if(s2.equals(">>"))
{
if(l5.getText().equals(String.valueOf(b[b.length-1].id))&&l6.getText().equals(String.valueOf(b[b.length-1].title))&&l7.getText().equals(String.valueOf(b[b.length-1].price))&&l8.getText().equals(String.valueOf(b[b.length-1].auth)))
{
JOptionPane.showMessageDialog(this,"You are already at Last Record");
}
else
{
l5.setText(String.valueOf(b[b.length-1].id));
l6.setText(String.valueOf(b[b.length-1].title));
l7.setText(String.valueOf(b[b.length-1].price));
l8.setText(String.valueOf(b[b.length-1].auth));
}
}

if(s2.equals("<"))
{


if(l5.getText().equals(String.valueOf(b[0].id))&&l6.getText().equals(String.valueOf(b[0].title))&&l7.getText().equals(String.valueOf(b[0].price))&&l8.getText().equals(String.valueOf(b[0].auth)))
{
JOptionPane.showMessageDialog(this,"This is a First Record, Previous Record is Not Available");
}
else
{
}

}

if(s2.equals(">"))
{

}

}
catch(Exception e)
{
}
}

当我单击“>”按钮时,我想要数组中的下一条记录。我必须做什么?

最佳答案

为什么不将当前页面存储在像 int 这样的变量中,然后对其进行操作?

类似这样的事情:

int page = 0;
Book[] books;

public void init()
{
BufferedReader br = new BufferedReader(new FileReader("Student.txt"));
String s = br.readLine();
books = Book.StringToObj(s);
}

public void show()
{
l5.setText(String.valueOf(books[page].id));
l6.setText(String.valueOf(books[page].title));
l7.setText(String.valueOf(books[page].price));
l8.setText(String.valueOf(books[page].auth));
}

public void fn(String s2)
{
try
{
if(s2.equals("Read Data"))
{
show();
}

if(s2.equals("<<"))
{
if (page == 0)
{
JOptionPane.showMessageDialog(this,"You are already at First Record");
}
else
{
page = 0;
show();
}
}

if(s2.equals(">>"))
{
if (page == books.length - 1)
{
JOptionPane.showMessageDialog(this,"You are already at Last Record");
}
else
{
page = books.length-1;
show();
}
}

if(s2.equals("<"))
{
if (page == 0)
{
JOptionPane.showMessageDialog(this,"This is a First Record, Previous Record is Not Available");
}
else
{
page--;
show();
}
}

if(s2.equals(">"))
{
if (page == books.length - 1)
{
JOptionPane.showMessageDialog(this,"You are already at Last Record");
}
else
{
page++;
show();
}
}
}
catch(Exception e)
{
}
}

请注意,我也将 books 移至全局范围,因为每次都再次读取该文件是没有意义的。

关于java - 查找当前索引并打印数组中的下一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com