使用for循环将一组数组返回到主方法会产生死代码错误。

huangapple go评论75阅读模式
英文:

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(&quot;The time is: &quot;);
System.out.println(nyet.displayClock());
//Class clock----------------------------
//Class setTime--------------------------
System.out.print(&quot;Do you wish to alter time| 1 = Yes, 0 = No:&quot;);
dec = scn.nextInt();
if (dec == 1) {
System.out.print(&quot;Input Hour:&quot;);
int hour = scn.nextInt();
if (hour &lt; 0 || hour &gt; 24) {
System.out.println(&quot;Sorry, there are only 24hrs in one day.&quot;);
System.exit(0);
}
System.out.print(&quot;Input Minute:&quot;);
int minute = scn.nextInt();
if (minute &lt; 0 || minute &gt; 60) {
System.out.println(&quot;Sorry, there are only 60mins in one hour.&quot;);
System.exit(0);
}
System.out.print(&quot;Input Second:&quot;);
int second = scn.nextInt();
if (second &lt; 0 || second &gt; 60) {
System.out.println(&quot;Sorry, there are only 60second in one minute.&quot;);
System.exit(0);
}
nyet.setTime(hour, minute, second);	
scn.close();
System.out.print(&quot;The time is: &quot;);
System.out.println(nyet.displayClock());
}				//Class setTime--------------------------
//Class setAlarm-------------------------
System.out.print(&quot;Do you wish to set an alarm| 1 = Yes, 0 = No:&quot;);
int dec1 = scn.nextInt();
if (dec1 == 1) {
do {
int instc = 1;
System.out.print(&quot;Input alarm number:&quot;);
int aNum = scn.nextInt();
System.out.print(&quot;Input Hour:&quot;);
int aHr = scn.nextInt();
if (aHr &lt; 0 || aHr &gt; 24) {
System.out.println(&quot;Sorry, there are only 24hr in one day.&quot;);
System.exit(0);
}
System.out.print(&quot;Input Minute:&quot;);
int aMin = scn.nextInt();
if (aMin &lt; 0 || aMin &gt; 60) {
System.out.println(&quot;Sorry, there are only 60mins in one hour.&quot;);
System.exit(0);
}
System.out.print(&quot;Do you wish to set another alarm| 1 = Yes, 0 = No:&quot;);
dec2 = scn.nextInt();
if (dec2 == 1)
instc++;
nyet.setAlarm(instc, aNum, aHr, aMin);
}while (dec2 != 0);
}				//Class setAlarm-------------------------
System.out.print(&quot;Show alarm| 1 = Show, 0 = Nothing:&quot;);
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 &lt; 2; i++) {
alarmOn[i] = !alarmOn[i];
}
}
public void setAlarm(int instncs,int aNmbr, int aHr, int aMnt) {
for (int i = 0; i &lt; 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(&quot;%02d:%02d:%02d&quot;, cHour, cMinute, cSecond);
}
public String displayAlarm() {
for (int i = 0; i &lt; instances; i++) { //&lt;&lt;&lt; Dead Code
return String.format(&quot;%02d:%02d:%02d&quot;, aAlarm[i], aHour[i], aMinute[i]);
}
}
}

答案1

得分: 0

以下是翻译好的内容:

当我在我的Eclipse中输入您的类Backend的代码时,方法displayAlarm()显示了一个构建错误,错误消息为:
> 此方法必须返回String类型的结果

以下是方法displayAlarm()的代码(与您的问题中完全相同):

public String displayAlarm() {
    for (int i = 0; i &lt; instances; i++) { //&lt;&lt;&lt; 死代码
        return String.format(&quot;%02d:%02d:%02d&quot;, aAlarm[i], aHour[i], aMinute[i]);
    }
}

方法中的for循环有可能不会被执行,在这种情况下,方法不会返回任何内容。因此,我只是添加了一行代码来解决构建错误:

public String displayAlarm() {
    for (int i = 0; i &lt; instances; i++) { //&lt;&lt;&lt; 死代码
        return String.format(&quot;%02d:%02d:%02d&quot;, aAlarm[i], aHour[i], aMinute[i]);
    }
    return &quot;&quot;;
}

在添加了这行代码之后,我收到了死代码警告。我承认花了一些时间才发现原因。最终我恍然大悟。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 &lt; instances; i++) { //&lt;&lt;&lt; Dead Code
return String.format(&quot;%02d:%02d:%02d&quot;, 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 &lt; instances; i++) { //&lt;&lt;&lt; Dead Code
return String.format(&quot;%02d:%02d:%02d&quot;, aAlarm[i], aHour[i], aMinute[i]);
}
return &quot;&quot;;
}

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(&quot;Do you wish to set an alarm| 1 = Yes, 0 = No:&quot;);
int dec1 = scn.nextInt();
if (dec1 == 1) {
do {
System.out.print(&quot;Input alarm number(Stored = &quot; + amount + &quot;):&quot;);
int aNum = scn.nextInt();
System.out.print(&quot;Input Hour:&quot;);
int aHr = scn.nextInt();
if (aHr &lt; 0 || aHr &gt; 24) {
System.out.println(&quot;Sorry, there are only 24hr in one day.&quot;);
System.exit(0);
}
System.out.print(&quot;Input Minute:&quot;);
int aMin = scn.nextInt();
if (aMin &lt; 0 || aMin &gt; 60) {
System.out.println(&quot;Sorry, there are only 60mins in one hour.&quot;);
System.exit(0);
}
nyet.setAlarm(amount, aNum, aHr, aMin);
System.out.print(&quot;Do you wish to set another alarm(max 3)| 1 = Yes, 0 = No:&quot;);
dec2 = scn.nextInt();
if (dec2 == 1) {
amount++;
}else if (dec2 == 0) {
amount++;
nyet.setAlarm(amount, aNum, aHr, aMin);
}else {
System.out.println(&quot;The only choices are &#39;1&#39; and &#39;0&#39;.&quot;);
System.exit(0);
}
if (amount &gt; 3) {
System.out.println(&quot;You have reached maximum storage.&quot;);
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 &lt; amnt; i++) {
System.out.println(&quot;Alarm #&quot; + aAlarm[i] + &quot; - &quot; + aHour[i] + &quot;:&quot; + aMinute[i]);
}
}

huangapple
  • 本文由 发表于 2020年9月11日 21:36:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63848200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定