英文:
Returning a set of array to the main method using for loop, gives error deadcode
问题
目前正在编写一个带有闹钟功能的时钟,并遇到了第一个死代码错误。
用户输入已经将数据存储到以下变量中:aAlarm、aHour和aMinute,但是我似乎无法将它们显示到主方法中。我已经尝试搜索其他关于死代码错误的问题,但没有一个能解决我的问题。以下是代码,变量“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());
// ... 省略其余的代码 ...
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++) {
return String.format("%02d:%02d:%02d", aAlarm[i], aHour[i], aMinute[i]);
}
}
}
英文:
Currently programming a Clock with an alarm and met my first Dead Code error.
User input have already stored data into the following variables; aAlarm, aHour, and aMinute.. but I can't seem to get them to display into the main method. I have tried searching other problems regarding dead error and none seem to solve my problem. Below is the code, the variable 'instances' equals to 1 and will increment for the amount of times the user creates an alarm.
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]);
}
}
}
答案1
得分: 0
以下是翻译好的内容:
当我在我的Eclipse中输入您的类Backend
的代码时,方法displayAlarm()
显示了一个构建错误,错误消息为:
> 此方法必须返回String类型的结果
以下是方法displayAlarm()
的代码(与您的问题中完全相同):
public String displayAlarm() {
for (int i = 0; i < instances; i++) { //<<< 死代码
return String.format("%02d:%02d:%02d", aAlarm[i], aHour[i], aMinute[i]);
}
}
方法中的for
循环有可能不会被执行,在这种情况下,方法不会返回任何内容。因此,我只是添加了一行代码来解决构建错误:
public String displayAlarm() {
for (int i = 0; i < instances; i++) { //<<< 死代码
return String.format("%02d:%02d:%02d", aAlarm[i], aHour[i], aMinute[i]);
}
return "";
}
在添加了这行代码之后,我收到了死代码警告。我承认花了一些时间才发现原因。最终我恍然大悟。for
循环体中唯一的内容就是return
语句。因此,只会有一次循环迭代,为什么要增加i
呢?
英文:
When I entered the code for your class Backend
in my Eclipse, it showed a build error for method displayAlarm()
, namely...
> This method must return a result of type String
Here is the code for method displayAlarm()
(exactly as it appears in your question).
public String displayAlarm() {
for (int i = 0; i < instances; i++) { //<<< Dead Code
return String.format("%02d:%02d:%02d", aAlarm[i], aHour[i], aMinute[i]);
}
}
It is possible that the for
loop in the method will not be entered and in that case the method does not return anything. So I just added a line to get rid of the build error.
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 "";
}
After adding the line, I got the dead code warning. I admit that it took me a while to discover the reason. Finally it dawned on me. The only thing in the for
loop body is return
. Hence there will only ever be precisely one loop iteration, so why increment i
?
答案2
得分: 0
以下是翻译好的内容:
前端代码:
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("是否要设置闹钟 | 1 = 是, 0 = 否:");
int dec1 = scn.nextInt();
if (dec1 == 1) {
do {
System.out.print("输入闹钟编号(已存储 = " + amount + "):");
int aNum = scn.nextInt();
System.out.print("输入小时:");
int aHr = scn.nextInt();
if (aHr < 0 || aHr > 24) {
System.out.println("抱歉,一天只有24小时。");
System.exit(0);
}
System.out.print("输入分钟:");
int aMin = scn.nextInt();
if (aMin < 0 || aMin > 60) {
System.out.println("抱歉,一小时只有60分钟。");
System.exit(0);
}
nyet.setAlarm(amount, aNum, aHr, aMin);
System.out.print("是否要设置另一个闹钟(最多3个) | 1 = 是, 0 = 否:");
dec2 = scn.nextInt();
if (dec2 == 1) {
amount++;
} else if (dec2 == 0) {
amount++;
nyet.setAlarm(amount, aNum, aHr, aMin);
} else {
System.out.println("只能选择'1'或'0'。");
System.exit(0);
}
if (amount > 3) {
System.out.println("已达到最大存储数量。");
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("闹钟 #" + aAlarm[i] + " - " + aHour[i] + ":" + aMinute[i]);
}
}
英文:
Guess the problem was the class had no 'static' in it.
Front end code:
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);
}
Back end code:
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]);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论