英文:
Storing the alphabet through a queue
问题
以下是翻译好的内容:
我正在尝试使用长度为100的队列来存储字母表,并使用另一个长度为4的小队列生成一些错误。如果小队列已满,正如小队列应该是满的,它会打印出 "The queue is full"。这是代码:
class Queue {
char q[];
int putloc, getloc;
Queue(int size) {
q = new char[size];
putloc = getloc = 0;
}
void put(char ch) {
if (putloc == q.length) {
System.out.println(" - 队列已满。");
return;
}
q[putloc++] = ch;
}
char get() {
if (getloc == putloc) {
System.out.println(" - 队列为空。");
return (char) 0;
}
return q[getloc++];
}
}
class name {
public static void main(String args[]) {
Queue bigQ = new Queue(100);
Queue smallQ = new Queue(4);
char ch;
int i;
System.out.println("使用 bigQ 存储字母表。");
for (i = 0; i < 5; i++) {
ch = 'A';
System.out.println("尝试存储 " + (char) ('A' + i));
smallQ.put((char) ('A' + i));
System.out.print(ch);
System.out.println("\n");
}
System.out.println("使用 smallQ 生成错误。");
for (i = 0; i < 5; i++) {
ch = 'Z';
System.out.println("尝试存储 " + (char) ('Z' - i));
smallQ.put((char) ('Z' - i));
System.out.println();
}
System.out.println();
System.out.print("smallQ 的内容:");
for (i = 0; i < 5; i++) {
ch = smallQ.get();
if (ch != (char) 0) System.out.println(ch);
}
}
}
smallQ 的输出是正常的,但 bigQ 的输出只打印了字母 'A' 一次,我应该怎么办?
英文:
I'm trying to store the alphabet with a queue which length is 100, and to generate some errors with another small queue of value 4, if then the queue is full, as it should be in small queue, it prints "The queue is full" this is the code:
class Queue {
char q[];
int putloc, getloc;
Queue(int size) {
q = new char[size];
putloc = getloc = 0;
}
void put(char ch) {
if(putloc == q.length) {
System.out.println(" - Queue is full.");
return;
}
q[putloc++] = ch;
}
char get() {
if(getloc == putloc) {
System.out.println(" - Queue is empty.");
return (char) 0;
}
return q[getloc++];
}
}
class name {
public static void main(String args[]) {
Queue bigQ = new Queue(100);
Queue smallQ = new Queue(4);
char ch;
int i;
System.out.println("Using bigQ to store the alphabet.");
for(i=0; i < 5; i++) {
ch = 'A';
System.out.println("Attemping to store " + (char) ('A' + i));
smallQ.put((char) ('A' - i));
System.out.print(ch);
System.out.println("\n");
System.out.println("Using smallQ to generate errors.");
for(i=0; i < 5; i++) {
ch = 'Z';
System.out.println("Attemping to store " + (char) ('Z' - i));
smallQ.put((char) ('Z' - i));
System.out.println();
}
System.out.println();
System.out.print("Contents of smallQ: ");
for(i=0; i < 5; i++) {
ch = smallQ.get();
if(ch != (char) 0) System.out.println(ch);
}
}
}
}
The output of smallQ works, but the output of bigQ just prints the letter 'A' once, what can I do?
答案1
得分: 2
你在这段代码中有两个错误:
for
循环:当我们使用for
循环、if
、while
或do while
时,如果只有一条语句,例如:
for(int i = 0; i <= 5 ; i++)
System.out.println("Running"+i);
System.out.println("Stop");
输出:
Running0
Running1
Running2
Running3
Running4
Running5
Stop
上面的代码只会执行紧随其后的代码行 6
次,然后打印 Stop
。因此,如果你没有在循环周围放置大括号,它将只执行其下一行代码,然后继续下一行。但是,当你使用带有大括号的 for
循环时,它会执行大括号内的所有内容,例如:
for(int i = 0 ; i <= 5 ;i++) {
System.out.println("Running"+i);
System.out.println("Stop");
}
输出:
Running0
Stop
Running1
Stop
Running2
Stop
Running3
Stop
Running4
Stop
Running5
Stop
你的 for
循环出现了相同的问题,你没有在必要的地方关闭循环。
- 你的代码中完全没有使用
bigQ
,只有smallQ
出现在你的第一个
和第二个
循环中。
修复后的代码如下:
class Test {
public static void main(String args[]) {
Queue bigQ = new Queue(100);
Queue smallQ = new Queue(4);
char ch;
int i;
System.out.println("Using bigQ to store the alphabet.");
for(i=0; i < 5; i++) {
ch = 'A';
System.out.println("Attempting to store " + (char) ('A' + i));
bigQ.put((char) ('A' + i));
}
System.out.println("\n");
System.out.println("Using smallQ to generate errors.");
for(i=0; i < 5; i++) {
ch = 'Z';
System.out.println("Attempting to store " + (char) ('Z' - i));
smallQ.put((char) ('Z' - i));
System.out.println();
}
System.out.println();
System.out.print("Contents of smallQ: ");
for(i=0; i < 5; i++) {
ch = smallQ.get();
if(ch != (char) 0) System.out.println(ch);
}
}
}
输出:
Using bigQ to store the alphabet.
Attempting to store A
Attempting to store B
Attempting to store C
Attempting to store D
Attempting to store E
Using smallQ to generate errors.
Attempting to store Z
Attempting to store Y
Attempting to store X
Attempting to store W
Attempting to store V
- Queue is full.
Contents of smallQ: Z
Y
X
W
- Queue is empty.
英文:
You had 2 bugs in this code which were:
- For loop
- You haven't used bigQ at all.
1.For loop: When we right for loop
or if
or while
or do while
if there's only one statement that is:
for(int i = 0; i <= 5 ; i++)
System.out.println("Running"+i);
System.out.println("Stop");
output:
Running0
Running1
Running2
Running3
Running4
Running5
Stop
the above code just executes the below line of code
for 6
times and then print Stop
so if you didnt put brackets around loop it will just execute the line below it and go to the next line.
<br>
But when you write for loop
with brackets
it executes everything that's in the { }
example:
for(int i = 0 ; i <= 5 ;i++) {
System.out.println("Running"+i);
System.out.println("Stop");
}
Output:
Running0
Stop
Running1
Stop
Running2
Stop
Running3
Stop
Running4
Stop
Running5
Stop
Your for loop had the same issue you haven't closed the loop where it was necessary<br>
2: You hadn't used bigQ at all in your code there was only samllQ in your first
for loop and second
for loop.
Fixed class name
code below:
class Test {
public static void main(String args[]) {
Queue bigQ = new Queue(100);
Queue smallQ = new Queue(4);
char ch;
int i;
System.out.println("Using bigQ to store the alphabet.");
for(i=0; i < 5; i++) {
ch = 'A';
System.out.println("Attemping to store " + (char) ('A' + i));
bigQ.put((char) ('A' - i));// <--changed
} // <--changed
System.out.println("\n");
System.out.println("Using smallQ to generate errors.");
for(i=0; i < 5; i++) {
ch = 'Z';
System.out.println("Attemping to store " + (char) ('Z' - i));
smallQ.put((char) ('Z' - i));
System.out.println();
}
System.out.println();
System.out.print("Contents of smallQ: ");
for(i=0; i < 5; i++) {
ch = smallQ.get();
if(ch != (char) 0) System.out.println(ch);
}
}
}
Output:
Using bigQ to store the alphabet.
Attemping to store A
Attemping to store B
Attemping to store C
Attemping to store D
Attemping to store E
Using smallQ to generate errors.
Attemping to store Z
Attemping to store Y
Attemping to store X
Attemping to store W
Attemping to store V
- Queue is full.
Contents of smallQ: Z
Y
X
W
- Queue is empty.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论