这些是全局声明和初始化的数组,每次运行程序时都会变为null
。
public class Inter_pro {
static String r1[] = new String[11];
static String r2[] = new String[11];
static String r3[] = new String[11];
static String r4[] = new String[11];
static String r5[] = new String[11];
String room = null;
调用此函数时,会将适当的数组值从null
更改为'res'。但它只发生一次,并在程序终止后被遗忘。
String room_allot(int from, int to) {
int i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0;
int a = (to - from) + 1;
for (int i = from; i <= to; i++) {
System.out.println(r1[i]);
}
for (int i = from; i <= to; i++) {
if (r1[i] == null)
i1++;
else if (r2[i] == null)
i2++;
else if (r3[i] == null)
i3++;
else if (r4[i] == null)
i4++;
else if (r5[i] == null)
i5++;
}
if (i1 == ((to - from) + 1)) {
for (int i = from; i <= to; i++) {
r1[i] = new String("hello");
}
room = "room1";
System.out.println("in loop1 room:" + room);
}
if (i2 == ((to - from) + 1)) {
for (int i = from; i <= to; i++) {
r1[i] = "res";
}
room = "room2";
}
if (i3 == ((to - from) + 1)) {
for (int i = from; i <= to; i++) {
r1[i] = "res";
}
room = "room3";
}
if (i4 == ((to - from) + 1)) {
for (int i = from; i <= to; i++) {
r1[i] = "res";
}
room = "room4";
}
if (i5 == ((to - from) + 1)) {
for (int i = from; i <= to; i++) {
r1[i] = "res";
}
room = "room5";
}
for (int i = from; i <= to; i++) {
System.out.println("ye baad me:" + r1[i] + "\n");
// r1[i]="res";
}
System.out.println(room);
return room;
}
抱歉长代码没有其他方法可以解释你。
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Inter_pro a1 = new Inter_pro();
System.out.println("Enter the day from(between 1 to 12):\n");
a1.setFrom(s.nextInt());
System.out.println("Enter the day to(between 1 to 12):\n");
a1.setTo(s.nextInt());
String room = a1.room_allot(a1.getFrom(), a1.getTo());
System.out.println("room alloted:" + room);
main(null);
}
我希望你们帮助我摆脱这个逻辑错误。 我想记住我的数组,每次程序调用的旧值都会对它们进行更改。
我该怎么办?
答案 0 :(得分:2)
这不是&#34;逻辑错误&#34;。
让我们看看你在做什么:
public class Inter_pro {
static String r1[] = new String[11];
static String r2[] = new String[11];
// ...
}
这意味着r1
,r2
,其他人将在加载类Inter_pro
时初始化一次,并将在此类的所有实例之间共享。
但并不意味着必须在运行之间保存此数据。每次启动程序时都会加载该类。
如果需要在运行之间保留数据,则需要手动存储它们。您可以搜索不同的解决方案,找到最适合您案例的解决方案:
在任何情况下,您都需要手动加载并存储到数据存储库:
Inter_pro
类(如果存在)。一旦你想到了,你应该尝试自己写。如果你没有做到,你可以用你的尝试,失败等问一个新的问题。
请记住编写我的代码&#39;问题不是StackOverflow的目的,除非你能证明你的尝试(我的意思是,认真尝试过),否则你将被投票。
次要说明:Java建议使用CamelCase表示法(您的类应该称为InterPro
)。应用这一点以及最重要的格式化将使协作变得更容易。
答案 1 :(得分:0)
您已使用main(null);
使您的计划继续进行。我没有看到它在正常流程中终止。
请参阅下面的代码可能是您要实现的目标:
import java.util.Scanner;
public class InterPro {
private int to = 0;
private int from = 0;
static String rooms[] = new String[12];
private String room = null;
private String delim = ", ";
String room_allot(int from, int to) {
for (int i = from - 1; i <= to - 1; i++) {
if (rooms[i] == null) {
rooms[i] = new String("Res");
if (room == null)
room = "room" + (i + 1);
else
room += delim + "room" + (i + 1);
}
}
return room;
}
public static void main(String[] args) {
try {
Scanner s = new Scanner(System.in);
InterPro ip = new InterPro();
System.out.println("Enter the day from(between 1 to 12):\n");
ip.setFrom(s.nextInt());
System.out.println("Enter the day to(between 1 to 12):\n");
ip.setTo(s.nextInt());
String roomAlloted = ip.room_allot(ip.getFrom(), ip.getTo());
if (roomAlloted == null)
System.out
.println("Requested rooms are reserved. Enter other choice");
else
System.out.println("rooms alloted:" + roomAlloted);
if (!ip.checkRoomAvailability()) {
System.out.println("No rooms available!!!"
+ " Program terminating... ");
System.exit(0);
}
} catch (Exception e) {
e.printStackTrace();
}
main(null);
}
boolean checkRoomAvailability() {
for (String string : rooms)
if (string == null)
return true;
return false;
}
private int getTo() {
return this.to;
}
private int getFrom() {
return this.from;
}
private void setTo(int to) {
this.to = to;
}
private void setFrom(int from) {
this.from = from;
}
}