无法访问的Java代码
当我试图编译这段代码时,第41-45行给出了一个“无法访问的代码”语句。当我输入几行代码来处理异常时,也会发生同样的情况。我做错什么了吗?这是SAMS 24小时自学Java一书中修改的示例代码。用它来复习无法访问的Java代码,java,exception,Java,Exception,当我试图编译这段代码时,第41-45行给出了一个“无法访问的代码”语句。当我输入几行代码来处理异常时,也会发生同样的情况。我做错什么了吗?这是SAMS 24小时自学Java一书中修改的示例代码。用它来复习 import java.util.*; import java.util.concurrent.TimeUnit; public class Clock { public static void main(String[] arguments) { Calendar now = Ca
import java.util.*;
import java.util.concurrent.TimeUnit;
public class Clock {
public static void main(String[] arguments) {
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
//Display greeting
if (hour < 12){
System.out.println("Good Morning! \n");
}else if (hour < 17){
System.out.println("Good afternoon! \n");
} else {
System.out.println("Good evening! \n");
}
//Time message start
while(1 < 2){
try
{
final String os = System.getProperty("os.name");
if (os.contains("Windows"))
{
Runtime.getRuntime().exec("cls");
}
else
{
Runtime.getRuntime().exec("clear");
}
}
catch (final Exception e)
{
// Handle any exceptions.
}
}
//Errors occur here
try {
TimeUnit.SECONDS.sleep(100);
} catch (InterruptedException e) {
//Handle exception
}
//Errors end here
System.out.println("The time currently is:" + hour + ":" + minute);
System.out.println("Date: " + month + "/" + day + "/" + year);
}
}
import java.util.*;
导入java.util.concurrent.TimeUnit;
公课钟{
公共静态void main(字符串[]参数){
Calendar now=Calendar.getInstance();
int hour=now.get(Calendar.hour\u OF_DAY);
int minute=now.get(Calendar.minute);
int month=now.get(Calendar.month)+1;
int day=now.get(Calendar.day\u/u月);
int year=now.get(Calendar.year);
//显示问候语
如果(小时<12){
System.out.println(“早上好!\n”);
}否则,如果(小时<17){
System.out.println(“下午好!\n”);
}否则{
System.out.println(“晚上好!\n”);
}
//时间信息开始
而(1<2){
尝试
{
最后一个字符串os=System.getProperty(“os.name”);
如果(操作系统包含(“Windows”))
{
Runtime.getRuntime().exec(“cls”);
}
其他的
{
Runtime.getRuntime().exec(“清除”);
}
}
捕获(最终异常e)
{
//处理任何异常。
}
}
//这里发生错误
试试{
时间单位。秒。睡眠(100);
}捕捉(中断异常e){
//处理异常
}
//错误到此为止
System.out.println(“当前时间为:“+hour+”:“+minute”);
系统输出打印项次(“日期:“+月+”/“+日+”/“+年”);
}
}
那里的代码是不可访问的,因为while循环条件1<2
始终为真,因此您始终处于while循环中。要避免这种情况,您可以:
- 将
while
循环条件更改为可能为false的内容
- 在while循环的某个地方添加一个
break
语句以退出它
那里的代码是不可访问的,因为while循环条件
1<2
始终为真,因此您始终处于while循环中。要避免这种情况,您可以:
- 将
while
循环条件更改为可能为false的内容
- 在while循环的某个地方添加一个
break
语句以退出它
while
循环将永远不会中断,因为1<2
始终为真。因此,while循环之后的部分永远不会到达,因此编译器错误。while
循环永远不会中断,因为1<2
始终为真。因此,while循环后面的部分永远不会到达,因此编译器错误。您有一个无限循环,没有break语句。您有一个无限循环,没有break语句。因为while循环是无限循环,所以您的代码永远不会到达您的this块
//Errors occur here
try {
TimeUnit.SECONDS.sleep(100);
} catch (InterruptedException e) {
//Handle exception
}
//Errors end here
System.out.println("The time currently is:" + hour + ":" + minute);
System.out.println("Date: " + month + "/" + day + "/" + year);
}
作为你的代码
while(1 < 2){
try
{
final String os = System.getProperty("os.name");
if (os.contains("Windows"))
{
Runtime.getRuntime().exec("cls");
}
else
{
Runtime.getRuntime().exec("clear");
}
}
catch (final Exception e)
{
// Handle any exceptions.
}
} // from here again start from loop staring point and looping this again and again
while(1<2){
尝试
{
最后一个字符串os=System.getProperty(“os.name”);
如果(操作系统包含(“Windows”))
{
Runtime.getRuntime().exec(“cls”);
}
其他的
{
Runtime.getRuntime().exec(“清除”);
}
}
捕获(最终异常e)
{
//处理任何异常。
}
}//从这里开始,从循环起始点开始,反复循环
所以在编译时,编译器警告您第二个try块和rest语句不可访问。您的程序将永远无法执行此块。这就是为什么它说不可访问。因为while循环是一个无限循环,所以代码永远不会到达this块
//Errors occur here
try {
TimeUnit.SECONDS.sleep(100);
} catch (InterruptedException e) {
//Handle exception
}
//Errors end here
System.out.println("The time currently is:" + hour + ":" + minute);
System.out.println("Date: " + month + "/" + day + "/" + year);
}
作为你的代码
while(1 < 2){
try
{
final String os = System.getProperty("os.name");
if (os.contains("Windows"))
{
Runtime.getRuntime().exec("cls");
}
else
{
Runtime.getRuntime().exec("clear");
}
}
catch (final Exception e)
{
// Handle any exceptions.
}
} // from here again start from loop staring point and looping this again and again
while(1<2){
尝试
{
最后一个字符串os=System.getProperty(“os.name”);
如果(操作系统包含(“Windows”))
{
Runtime.getRuntime().exec(“cls”);
}
其他的
{
Runtime.getRuntime().exec(“清除”);
}
}
捕获(最终异常e)
{
//处理任何异常。
}
}//从这里开始,从循环起始点开始,反复循环
所以在编译时,编译器警告您第二个try块和rest语句不可访问。您的程序将永远无法执行此块。这就是为什么它说不可到达。你有一个无限循环。1总是小于2 建议:
你有一个无限循环。1总是小于2 建议:
while(1<2){
上次我检查时始终为真?代码是否比您拥有的更多?为什么要在(1<2)时执行?循环将永远不会发生terminated@scrappedcola上次我检查了lol'd,您可以简单地使用while(true)
。但是请确保有某种方法可以摆脱循环。while(1<2){
上次检查时始终为真?代码是否比您拥有的更多?为什么您在(1<2)
时执行?循环将永远不会发生terminated@scrappedcola上次我选中了
lol'd,您可以简单地使用while(true)
。但请确保有某种方法可以摆脱循环。这是可行的,尽管对于这一点,最好使用for
循环,如下所示:for(int x=1;x<2000000;x++)
可行,尽管对于这一点可能是p