class number
{
public static void main(int a, int b, char c)
{ int r=0;
if(c==' ')
r= a b;
System.out.println("result is " r);
else if(c=='-')
r= a-b;
System.out.println("result is " r);
else if(c=='/')
r= a/b;
System.out.println("result is " r);
else if(c=='%')
r= a%b;
System.out.println("result is " r);
else if(c=='*')
r= a*b;
System.out.println("result is " r);
else
System.out.println("wrong operator");
}}
uj5u.com熱心網友回復:
對您的問題的直接回答是,您需要在 if 中的任何多行陳述句周圍加上大括號
if (c == ' ') {
r = a b;
System.out.println("result is " r)
} else if (c == '-') {
r = a - b;
System.out.println("result is " r);
}
您的代碼還有其他問題:
Java 強烈建議類名以大寫字母開頭
class Number {
并且您的主要簽名錯誤,必須是
public static void main(String[] args) {
然后,您需要從 args[0]、args[1] 和 args[2] 中獲取 a、b 和 c 值。您將需要轉換字串引數。
強烈建議您閱讀 Java 教程,該教程將詳細解釋所有這些。
uj5u.com熱心網友回復:
您只能在沒有大括號的“if”條件中撰寫一個陳述句。由于您已經撰寫了 2 個陳述句(即計算和列印),它不會編譯。
除此之外,主方法簽名也不正確,并且還要確保在命名類時使用 Camel 大小寫命名約定。
查看https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html了解更多詳情。
class Number {
public static void main(String[] args) {
// get inputs for c, a and b
int r=0;
if(c ==' '){
r= a b;
System.out.println("result is " r);
}
else if(c=='-') {
r= a-b;
System.out.println("result is " r);
}
else if(c=='/') {
r= a/b;
System.out.println("result is " r);
}
else if(c=='%') {
r= a%b;
System.out.println("result is " r);
}
else if(c=='') {
r= ab;
System.out.println("result is " r);
}
else
System.out.println("wrong operator");
}
}
uj5u.com熱心網友回復:
請參閱此參考:
class number {
public static void main(int a, int b, char c) {
int r = 0;
if (c == ' ') {
r = a b;
} else if (c == '-') {
r = a - b;
} else if (c == '/') {
r = a / b;
} else if (c == '%') {
r = a % b;
} else if (c == '') {
r = ab;
} else {
System.out.println("wrong operator");
}
System.out.println("result is " r); //This line is commonly used, so we put it here.
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/429407.html
下一篇:請求物件未正確過濾
