使用for循环将一组数组返回到主方法,给出错误死区



当前正在用闹钟编程时钟,遇到了我的第一个死代码错误。


用户输入已经将数据存储到以下变量中;a报警、一小时和一分钟。。但我似乎无法将它们显示到主方法中。我试过搜索其他关于死错误的问题,但似乎没有一个能解决我的问题。下面是代码,变量"instances"等于1,并且将随着用户创建警报的次数而增加。

import java.util.*;
public class Frontend {

public static void main(String args[]) {
Backend nyet = new Backend();
Scanner scn = new Scanner(System.in);
int dec, dec2;
System.out.print("The time is: ");
System.out.println(nyet.displayClock());
//Class clock----------------------------

//Class setTime--------------------------
System.out.print("Do you wish to alter time| 1 = Yes, 0 = No:");
dec = scn.nextInt();
if (dec == 1) {
System.out.print("Input Hour:");
int hour = scn.nextInt();
if (hour < 0 || hour > 24) {
System.out.println("Sorry, there are only 24hrs in one day.");
System.exit(0);
}
System.out.print("Input Minute:");
int minute = scn.nextInt();
if (minute < 0 || minute > 60) {
System.out.println("Sorry, there are only 60mins in one hour.");
System.exit(0);
}
System.out.print("Input Second:");
int second = scn.nextInt();
if (second < 0 || second > 60) {
System.out.println("Sorry, there are only 60second in one minute.");
System.exit(0);
}
nyet.setTime(hour, minute, second); 
scn.close();
System.out.print("The time is: ");
System.out.println(nyet.displayClock());
}               //Class setTime--------------------------

//Class setAlarm-------------------------
System.out.print("Do you wish to set an alarm| 1 = Yes, 0 = No:");
int dec1 = scn.nextInt();
if (dec1 == 1) {
do {
int instc = 1;
System.out.print("Input alarm number:");
int aNum = scn.nextInt();
System.out.print("Input Hour:");
int aHr = scn.nextInt();
if (aHr < 0 || aHr > 24) {
System.out.println("Sorry, there are only 24hr in one day.");
System.exit(0);
}
System.out.print("Input Minute:");
int aMin = scn.nextInt();
if (aMin < 0 || aMin > 60) {
System.out.println("Sorry, there are only 60mins in one hour.");
System.exit(0);
}
System.out.print("Do you wish to set another alarm| 1 = Yes, 0 = No:");
dec2 = scn.nextInt();
if (dec2 == 1)
instc++;
nyet.setAlarm(instc, aNum, aHr, aMin);
}while (dec2 != 0);
}               //Class setAlarm-------------------------

System.out.print("Show alarm| 1 = Show, 0 = Nothing:");
int z = scn.nextInt();
if (z == 1)
nyet.displayAlarm();
}
}
import java.time.OffsetTime;
public class Backend {

OffsetTime nyet = OffsetTime.now();
private int cHour, cMinute, cSecond, instances;
private int[] aAlarm, aHour, aMinute;
private boolean[] alarmOn;

public Backend() {

cHour = nyet.getHour();
cMinute = nyet.getMinute();
cSecond = nyet.getSecond();
aHour = new int[2];
aMinute = new int[2];
aAlarm = new int[2];
alarmOn = new boolean[2];
for (int i = 0; i < 2; i++) {
alarmOn[i] = !alarmOn[i];
}
}
public void setAlarm(int instncs,int aNmbr, int aHr, int aMnt) {
for (int i = 0; i < instncs; i++) {
aAlarm[i] = aNmbr;
aHour[i] = aHr;
aMinute[i] = aMnt;
instances = instncs;
}
}

public void setTime(int hr, int min, int sec) {
cHour = hr;
cMinute = min;
cSecond = sec;
}

public String displayClock() {
return String.format("%02d:%02d:%02d", cHour, cMinute, cSecond);
}

public String displayAlarm() {
for (int i = 0; i < instances; i++) { //<<< Dead Code
return String.format("%02d:%02d:%02d", aAlarm[i], aHour[i], aMinute[i]);
}
}
}

当我在Eclipse中输入类Backend的代码时,它显示了方法displayAlarm()的构建错误,即。。。

此方法必须返回字符串类型的结果

这是方法displayAlarm()的代码(与您的问题中出现的代码完全一样(。

public String displayAlarm() {
for (int i = 0; i < instances; i++) { //<<< Dead Code
return String.format("%02d:%02d:%02d", aAlarm[i], aHour[i], aMinute[i]);
}
}

方法中的for循环可能不会被输入,在这种情况下,方法不会返回任何内容。所以我只是添加了一行来消除构建错误。

public String displayAlarm() {
for (int i = 0; i < instances; i++) { //<<< Dead Code
return String.format("%02d:%02d:%02d", aAlarm[i], aHour[i], aMinute[i]);
}
return "";
}

添加行之后,我得到了死代码警告。我承认我花了一段时间才发现原因。我终于明白了,for循环体中唯一的东西就是return。因此,只有一次循环迭代,那么为什么要增加i呢?

我猜问题出在类中没有"static"。

前端代码:

public static void main(String args[]) {
int dec, dec2, amount = 0, deci0;
Backend nyet = new Backend();
Scanner scn = new Scanner(System.in);
System.out.print("Do you wish to set an alarm| 1 = Yes, 0 = No:");
int dec1 = scn.nextInt();
if (dec1 == 1) {
do {
System.out.print("Input alarm number(Stored = " + amount + "):");
int aNum = scn.nextInt();
System.out.print("Input Hour:");
int aHr = scn.nextInt();
if (aHr < 0 || aHr > 24) {
System.out.println("Sorry, there are only 24hr in one day.");
System.exit(0);
}
System.out.print("Input Minute:");
int aMin = scn.nextInt();
if (aMin < 0 || aMin > 60) {
System.out.println("Sorry, there are only 60mins in one hour.");
System.exit(0);
}
nyet.setAlarm(amount, aNum, aHr, aMin);
System.out.print("Do you wish to set another alarm(max 3)| 1 = Yes, 0 = No:");
dec2 = scn.nextInt();
if (dec2 == 1) {
amount++;
}else if (dec2 == 0) {
amount++;
nyet.setAlarm(amount, aNum, aHr, aMin);
}else {
System.out.println("The only choices are '1' and '0'.");
System.exit(0);
}
if (amount > 3) {
System.out.println("You have reached maximum storage.");
dec2 = 0;
}
}while (dec2 != 0);
}

后端代码:

public class Backend {

OffsetTime nyet = OffsetTime.now();
private int cHour, cMinute, cSecond;
private static int[] aAlarm, aHour, aMinute;
private boolean[] alarmOn;
private static int amnt;
public void setAlarm(int instncs,int aNmbr, int aHr, int aMnt) {
int i = instncs;
aAlarm[i] = aNmbr;
aHour[i] = aHr;
aMinute[i] = aMnt;
amnt = instncs;
}

public static void displayAlarm() {
for (int i = 0; i < amnt; i++) {
System.out.println("Alarm #" + aAlarm[i] + " - " + aHour[i] + ":" + aMinute[i]);
}
}

相关内容

最新更新