如何修复 "variable phraseLength might not have been initialized" (Java)?
How to fix a "variable phraseLength might not have been initialized" (Java)?
我是编程新手 Java,正在处理涉及字符串的作业。我刚开始使用代码模板,我的指示是编译它并查看输出,但出现 "variable phraseLength might not have been initilized" 错误。它指的是我在代码底部附近使用 "phraseLength" 作为变量的时间。有谁知道如何解决这一问题?非常感谢你提前。
import java.util.Scanner;
public class Working_With_Strings
{
public static void main (String[] args)
{
//instantiate the String……
String phrase = new String ("This is a String test.");
int phraseLength;
int middle3;// number of characters in the phrase String
int middleIndex; // index of the middle character in the String
String firstHalf; // first half of the phrase String
String secondHalf; // second half of the phrase String
String switchedPhrase; // a new phrase with original halves switched
// 1-compute the length and middle index of the phrase
//2- get the substring for each half of the phrase
//3- concatenate the firstHalf at the end of the secondHalf
// print information about the phrase
System.out.println();
System.out.println ("Original phrase: " + phrase);
System.out.println ("Length of the phrase: " + phraseLength +
" characters");
System.out.println ("Index of the middle: " + middleIndex);
System.out.println ("Character at the middle index: " +
phrase.charAt(middleIndex));
System.out.println ("Switched phrase: " + switchedPhrase);
System.out.println();
}
}
一开始没看懂报错是什么意思,因为不知道初始化是什么意思。现在我知道我需要用一个值来初始化 "phraseLength" 。再次感谢您的帮助!
int phraseLength;
int middle3;// number of characters in the phrase String
int middleIndex; // index of the middle character in the String
String firstHalf; // first half of the phrase String
String secondHalf; // second half of the phrase String
String switchedPhrase; // a new phrase with original halves switched
至:
int phraseLength = 0;
int middle3 = 0;// number of characters in the phrase String
int middleIndex = 0; // index of the middle character in the String
String firstHalf = ""; // first half of the phrase String
String secondHalf = ""; // second half of the phrase String
String switchedPhrase = ""; // a new phrase with original halves switched
你应该在使用它们之前初始化变量...我向你推荐一本书Head First Java
方法中声明的变量不能自动初始化。
也许你需要阅读一些关于java基础的基础书籍。
int phraseLength = 0;
int middle3 = 0;// number of characters in the phrase String
int middleIndex = 0; // index of the middle character in the String
String firstHalf = null; // first half of the phrase String
String secondHalf = null; // second half of the phrase String
String switchedPhrase = null;
你已经声明了一个变量但没有初始化变量
int phraseLength;
这里你试图访问一个未初始化的变量。
System.out.println ("Length of the phrase: " + phraseLength +
" characters");
您必须先定义变量的值,然后才能访问该值:
您的情况有两种解决方案:
1.You 应该在如下声明后初始化变量:
`int phraseLength;//Declaration
pharaseLength = 0;`//Initialization
2.Assign这个变量经过一些计算:
这里我取字符串的长度赋值给变量
pharaseLength = pharaseString.length();
您需要对其余变量执行相同的操作
供您参考:
Initialization , assignment ,declaration:
之间的区别
` import java.util.Scanner;
public class Working_With_Strings
{
public static void main (String[] args)
{
//instantiate the String……
String phrase = new String ("This is a String test.");
int phraseLength;
phraseLength=phrase.length();
System.out.println ("Original phrase: " + phrase);
System.out.println ("Length of the phrase: " + phraseLength +
" characters");
}
}
`
我是编程新手 Java,正在处理涉及字符串的作业。我刚开始使用代码模板,我的指示是编译它并查看输出,但出现 "variable phraseLength might not have been initilized" 错误。它指的是我在代码底部附近使用 "phraseLength" 作为变量的时间。有谁知道如何解决这一问题?非常感谢你提前。
import java.util.Scanner;
public class Working_With_Strings
{
public static void main (String[] args)
{
//instantiate the String……
String phrase = new String ("This is a String test.");
int phraseLength;
int middle3;// number of characters in the phrase String
int middleIndex; // index of the middle character in the String
String firstHalf; // first half of the phrase String
String secondHalf; // second half of the phrase String
String switchedPhrase; // a new phrase with original halves switched
// 1-compute the length and middle index of the phrase
//2- get the substring for each half of the phrase
//3- concatenate the firstHalf at the end of the secondHalf
// print information about the phrase
System.out.println();
System.out.println ("Original phrase: " + phrase);
System.out.println ("Length of the phrase: " + phraseLength +
" characters");
System.out.println ("Index of the middle: " + middleIndex);
System.out.println ("Character at the middle index: " +
phrase.charAt(middleIndex));
System.out.println ("Switched phrase: " + switchedPhrase);
System.out.println();
}
}
一开始没看懂报错是什么意思,因为不知道初始化是什么意思。现在我知道我需要用一个值来初始化 "phraseLength" 。再次感谢您的帮助!
int phraseLength;
int middle3;// number of characters in the phrase String
int middleIndex; // index of the middle character in the String
String firstHalf; // first half of the phrase String
String secondHalf; // second half of the phrase String
String switchedPhrase; // a new phrase with original halves switched
至:
int phraseLength = 0;
int middle3 = 0;// number of characters in the phrase String
int middleIndex = 0; // index of the middle character in the String
String firstHalf = ""; // first half of the phrase String
String secondHalf = ""; // second half of the phrase String
String switchedPhrase = ""; // a new phrase with original halves switched
你应该在使用它们之前初始化变量...我向你推荐一本书Head First Java
方法中声明的变量不能自动初始化。
也许你需要阅读一些关于java基础的基础书籍。
int phraseLength = 0;
int middle3 = 0;// number of characters in the phrase String
int middleIndex = 0; // index of the middle character in the String
String firstHalf = null; // first half of the phrase String
String secondHalf = null; // second half of the phrase String
String switchedPhrase = null;
你已经声明了一个变量但没有初始化变量
int phraseLength;
这里你试图访问一个未初始化的变量。
System.out.println ("Length of the phrase: " + phraseLength + " characters");
您必须先定义变量的值,然后才能访问该值:
您的情况有两种解决方案:
1.You 应该在如下声明后初始化变量:
`int phraseLength;//Declaration
pharaseLength = 0;`//Initialization
2.Assign这个变量经过一些计算:
这里我取字符串的长度赋值给变量
pharaseLength = pharaseString.length();
您需要对其余变量执行相同的操作
供您参考:
Initialization , assignment ,declaration:
之间的区别
` import java.util.Scanner;
public class Working_With_Strings
{
public static void main (String[] args)
{
//instantiate the String……
String phrase = new String ("This is a String test.");
int phraseLength;
phraseLength=phrase.length();
System.out.println ("Original phrase: " + phrase);
System.out.println ("Length of the phrase: " + phraseLength +
" characters");
}
}
`