英文:
Getter and setters and arrays in java
问题
我目前遇到了一个Java任务的问题。我正在尝试回答第3和第4个问题,这两个问题基于下面所示的代码。
-
下面的代码已经回答了第1和第2个问题,他们要求你修改应用程序,以便输出的每一行都带有编号,从1开始。
-
通过这样的方式进行修改,即使你插入了另一个println/nextLine,你也不需要重新为其后的所有行编号。一种做法是使用一个整数变量来存储当前行号,并在每次输出一行时将其增加一。
运行程序后,输出窗口应该如下所示:
1: 输入一行
输入一些内容
2: 输入另一行
输入更多内容
3: 输入最后一行
结束
结束,输入更多内容,输入一些内容
import java.util.Scanner;
public class Practical1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String msg1, msg2, msg3;
System.out.println("输入一行");
msg1 = in.nextLine();
System.out.println("输入另一行");
msg2 = in.nextLine();
System.out.println("输入最后一行");
msg3 = in.nextLine();
System.out.println(msg3 + "," + msg2 + "," + msg1);
}}
上面的代码回答了第1和第2个问题。
对于第3个问题,它要求我创建一个名为Line的新Java类,然后向该类添加适当的实例变量(字段)和构造函数,以便可以存储文本行(一个名为text的字符串)和序列号(一个名为seqNum的整数)。这些实例变量应对其他类是“只读”的。也就是说,它们应该声明为“private”,并提供一个“getter”方法来返回值。我在下面的代码中完成了这个要求:
public class Line {
public String getText() {
return text;
}
private String text;
public int getSeqNum() {
return seqNum;
}
private int seqNum;
}
然后它要求修改类Practical1的main方法,以便将每行文本及其序列号存储在Line对象中,而不是String对象中。根据需要修改变量类型(如msg1等)。注意,要打印出一行,你将需要调用getter方法。你的程序的输出应与之前完全相同。
最终输出应为:
3: 结束,2: 输入更多内容,1: 输入一些内容
你要如何完成这个问题?
对于第4个问题,最后一个问题,它要求我修改程序,以便使用循环重复执行以下操作:
- 提示输入(使用行seqNum和提示短语:“输入一行”)
- 读取输入的行
- 将输入的行存储在Line数组的下一个位置或Line的ArrayList中。
一旦用户输入STOP,循环应该终止,并且另一个循环应该按照相反的顺序打印每一行。例如(你应该键入的输入已用粗体标出):
1: 输入一行
输入一些内容
2: 输入一行
输入更多内容
3: 输入一行
结束
4: 输入一行
STOP
3: 结束
2: 输入更多内容
1: 输入一些内容
你要如何完成这个问题?
英文:
I am having an issue with a java task I'm doing at the moment. I am trying to answer question 3 and 4 which is based on the following code you see below .
-
The following code below was answered to question 1 and 2 where they
wanted you to Modify the application so that each line of output is
numbered, starting from 1. -
Do this in such a way that if you inserted another println/nextLine
you would not need to re-number all the lines after that. One way to
do this is to use an integer variable to store the current line
number and increase it by one each time you output a line.
After running the program, the output window should look like:
1: Enter a line
Some input
2: Enter another line
More input
3: Enter the last line
The end
The end,More input,Some input
import java.util.Scanner;
public class Practical1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String msg1, msg2, msg3;
System.out.println("Enter a line");
msg1 = in.nextLine();
System.out.println("Enter another line");
msg2 = in.nextLine();
System.out.println("Enter the last line");
msg3 = in.nextLine();
System.out.println(msg3 + "," + msg2 + "," + msg1) ; }}
The code above ansered question 1 and 2.
For question 3, its asking me to create a new java class as i did called Line and then Add appropriate instance variables (fields) and a constructor to the class so that a
line of text (a String, called text) and a sequence number (an int, called seqNum)
can be stored. These instance variables should be "read only" for other classes. That
is, they should be declared "private" and a "getter" method provided to return the
value. This is what i did in the code below:
public class Line {
public String getText() {
return text;
}
private String text;
public int getSeqNum() {
return seqNum;
}
private int seqNum;
}
It then asks to Modify the main method of the class Practical1 so that it stores each line of text
and its sequence number in a Line object, rather than a String object. Modify the
type of variables (msg1 etc.) as required. Note that to print out a line, you will need
to call the getter method. The output of your program should be exactly as before.
The end,More input,Some input
Finally it asks you to Now change the program so that the sequence number is printed with the line. The
final line of output should be:
3: The end,2: More input,1: Some input
How would i do this question?
For question 4 the last question, it wants me to
Modify the program so that it uses a loops repeatedly doing:
- prompt for input (using line seqNum and the prompt phrase: "Enter a line")
- reads the line of input
- stores the line of input in the next position of an array of Line or
an ArrayList of Line.
Once the user enters STOP as input, the loop should terminate and another loop should
print each line in reverse order. For example (the input you should type in is in bold):
1: Enter a line
Some input
2: Enter a line
More input
3: Enter a line
The end
4: Enter a line
STOP
3: The end
2: More input
1: Some input
How would I do this question ?
答案1
得分: 1
你可以采用以下方法来解决这个问题:
首先是 Line 类
public class Line
{
private int seq_num;
private String text;
Line(int seq_num, String text)
{
this.seq_num = seq_num;
this.text = text;
}
public String getText()
{
return this.text;
}
public int getSeq()
{
return this.seq_num;
}
}
用于接受无限输入直到提供 "STOP" 文本的 Demo 类
class Demo
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Line> list = new ArrayList<>();
int counter = 0;
while(true)
{
String scanned_line = sc.nextLine();
if(scanned_line.equals("STOP"))
break;
list.add(new Line(counter, scanned_line));
counter++;
}
// 按照 seq num 进行逆序排序
list.sort(Comparator.comparingInt(Line::getSeq).reversed()); // Java 8 comparator
list.forEach(e -> System.out.println(e.getSeq() + " " + e.getText())); // Java 8 forEach
}
}
另外,
- 需要强调的是,
while(true)
可以被替换为while(scanner.hasNextLine())
。这样它会扫描直到没有下一行输入为止。 - 另外,Java 8 的
forEach
可以被传统的 for 循环替代。
英文:
You can to following approach, to solve this problem
>First the Line class
public class Line
{
private int seq_num;
private String text;
Line(int seq_num, String text)
{
this.seq_num = seq_num;
this.text = text;
}
public String getText()
{
return this.text;
}
public int getSeq()
{
return this.seq_num;
}
}
> Demo class for taking infinite input until STOP text is provided
class Demo
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Line> list = new ArrayList<>();
int counter =0;
while(true)
{
String scanned_line = sc.nextLine();
if(scanned_line.equals("STOP"))
break;
list.add(new Line(counter,scanned_line));
counter++;
}
//sort the list as per the seq num in reverse order
list.sort(Comparator.comparingInt(Line::getSeq).reversed()); // java 8 comparator
list.forEach(e-> System.out.println(e.getSeq()+" "+e.getText()));//java 8 forEach
}
}
Also ,
- important to mention that
while(true)
can be replace withwhile(scanner.hasNextLine())
. so it will scan until it has next line input provided. - Also java 8
forEach
can be replaced with traditional for loop.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论