自己弄的一个幼稚作品,不懂哪里错误百出...
public class Testcup
{
String d;
int w;
static String Test1(String d)
{
if (String d = "塑料的")
{
String r = "Yes";
return r;
}
else if (String d = "玻璃的")
{
String r = "No";
return r;
}
else
{
String r = "Er";
return r;
}
}
static String Test2(int w)
{
if (w <10)
{
Srting o = "Yes";
return o;
}
else
{
String o = "Heavy";
return o;
}
}
public static void main(String[] args)
{
Testcup e = new Testcup();
e.d = "塑料的";
e.w = 9;
if (Test1(e.d)==Test2(e.w))
{
System.out.println("This one is required");
}
else
{
System.out.println("I'm sorry , this one is not required") ;
}
}
}
[解决办法]
else if (String d = "玻璃的") 改为 else if (d .equals( "玻璃的"))
[解决办法]
public class Testcup { String d; int w; static String Test1(String d) { if (d == "塑料的") {// 邏輯判斷用“==”符,(String d ="塑料的")是一個賦值語句,if語句只接受true或false或者邏輯表達式 String r = "Yes"; return r; } else if (d == "玻璃的") {// 同上 String r = "No"; return r; } else { String r = "Er"; return r; } } static String Test2(int w) { if (w < 10) { String o = "Yes";// String錯寫為Srting了 return o; } else { String o = "Heavy"; return o; } } public static void main(String[] args) { Testcup e = new Testcup(); e.d = "塑料的"; e.w = 9; if (Test1(e.d) == Test2(e.w)) { System.out.println("This one is required"); } else { System.out.println("I'm sorry , this one is not required"); } }}
[解决办法]
public class Arrag { String d; int w; static String Test1(String d) { if (d.equals("塑料的")) { String r = "Yes"; return r; } else if (d.equals("玻璃的")) { String r = "No"; return r; } else { String r = "Er"; return r; } } static String Test2(int w) { if (w < 10) { String o = "Yes"; return o; } else { String o = "Heavy"; return o; } } public static void main(String[] args) { Arrag e = new Arrag(); e.d = "塑料的"; e.w = 9; if (Test1(e.d).equals(Test2(e.w))) { System.out.println("This one is required"); } else { System.out.println("I'm sorry , this one is not required"); } }}