Java 类不会使用while循环生成ID
我正在尝试用Java做一个简单的增量,我在我的netbeans编译器中得到了这个“不可访问的语句”,我似乎不明白真正的担忧是什么 我的代码看起来是这样的Java 类不会使用while循环生成ID,java,jakarta-ee,Java,Jakarta Ee,我正在尝试用Java做一个简单的增量,我在我的netbeans编译器中得到了这个“不可访问的语句”,我似乎不明白真正的担忧是什么 我的代码看起来是这样的 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Thanos
*/
public class PersistenceClass {
public static String getPersistenceID()
{
int startNum = 1;
while(true)
{
startNum++;
}
String finalNum = String.valueOf(startNum); //<----- Says unreachable statement here
return finalNum;
}
public static void main(String[]args)
{
getPersistenceID();
}
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
/**
*
*@作者塔诺斯
*/
公共类PersistenceClass{
公共静态字符串getPersistenceID()
{
int startNum=1;
while(true)
{
startNum++;
}
String finalNum=String.valueOf(startNum);//while(true)使循环下面的语句不可访问。您应该有一个条件,该条件将在所需的迭代次数后退出循环。例如:while(startNum您的循环中没有exit语句。此while循环将永远执行。它不会在while循环之后到达任何对象。因为您的循环是无限的。它永远不会到达在循环之后声明的任何对象。
int static startNum = 1;
public static String getPersistenceID()
{
startNum+=1;
return startNum+"";
}