Java 如何初始化一个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20037323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to initialize a string?
提问by v_7
String car;
if (type == 'E')
{fee = rate*50;
car = "Economy";}
else if (type == 'M')
{fee = rate*70;
car = "Midsize";}
else if (type == 'F')
{fee = rate*100;
car = "Fullsize";}
System.out.println("Car type is " + car);
This is the part of my program that i have problem with. I get 'local variable car may not have been initialized ' . What should i do to make 'car' initialized ?
这是我的程序中有问题的部分。我得到“局部变量汽车可能尚未初始化”。我应该怎么做才能初始化“汽车”?
采纳答案by Makoto
Java can't guarantee that car
will ever become initialized. You don't have a guaranteed case (else
) in your if
statement that assigns car
.
Java 不能保证它car
会被初始化。您else
的if
语句中没有保证 case ( )分配car
.
Initialize it to some value, such as an empty string.
将其初始化为某个值,例如空字符串。
String car = "";
回答by Martijn Courteaux
In general, you set your String to null
to solve the compilation error.
通常,您将 String 设置null
为解决编译错误。
String car = null;
That is because Java is trying to protect you from doing something you don't expect. If all the if
statements are not executed, the variable will not get assigned. To tell Java, that you know that this might happen, you just chose explicitly to set it to null
.
那是因为 Java 试图保护您免于做您不期望的事情。如果if
未执行所有语句,则不会分配变量。为了告诉 Java,您知道这可能会发生,您只需明确选择将其设置为null
.
回答by gerosalesc
Java can't tell what's going to happen with your variable reach print, as it's initially pointing nowhere and further initializations are enclosed in conditionals Set your car variable to a default value depending on logic: "" or null are good options but it's really up to you in the end
Java 无法判断变量到达打印会发生什么,因为它最初没有指向任何地方,并且进一步的初始化包含在条件中 根据逻辑将汽车变量设置为默认值:"" 或 null 是不错的选择,但它确实存在最后给你
回答by Peter_James
I would consider to do this another way:
我会考虑以另一种方式做到这一点:
switch (type) {
case 'E':
fee =rate* 50;
car = "Economy";
break;
case 'M':
fee =rate* 70;
car = "Economy";
break;
case 'F':
fee =rate*100;
car = "Economy";
break;
default:
fee = 0;
car = "";
}
回答by Peter_James
How I would of solved this problem using an inner Enum
class.
我将如何使用内部Enum
类解决这个问题。
public class EnumExample {
public enum CarType {
E( 50,"Economy" ),
F( 70, "Midsize" ),
M( 100, "Fullsize");
// Add as many as you want
private int cost;
private String type;
CarType(int theCost, String theType) {
cost = theCost;
type = theType;
}
public int getFee(int rate) {
return rate*cost;
}
public String toString() {
return type;
}
}
public static void main( String[] args ) {
String type = "E";
int rate = 25;
switch( CarType.valueOf( type ) ) {
case E:
System.out.println( "The Car Type is: " + CarType.E );
System.out.println( "The fee is: " + CarType.E.getFee(rate) );
break;
case F:
System.out.println( "The Car Type is: " + CarType.F );
System.out.println( "The fee is: " + CarType.F.getFee(rate) );
break;
case M:
// ETC
default:
// Use a switch/case so you can add 'no car type' e.g Fee = 0;
}
}
}