钢材网站模板,wordpress大气全屏主题,桐庐建设局网站,北京优质网站制作卡码网Java基础课| 4. AB问题IV#xff0c;5. AB问题VIII 三元运算符4. AB问题IV5. AB问题VIII 三元运算符
普通if
int a 10;
int b 20;
int c;
if (a b) {c a;
} else {c b;
}而三元运算符的结构如下#xff1a; 会先求expression的值#xff0c;如果为 true ,则… 卡码网Java基础课| 4. AB问题IV5. AB问题VIII 三元运算符4. AB问题IV5. AB问题VIII 三元运算符
普通if
int a 10;
int b 20;
int c;
if (a b) {c a;
} else {c b;
}而三元运算符的结构如下 会先求expression的值如果为 true ,则取值 if-true-statement,否则取值 if-false-statement
{expression} ? if-true-element : if-false-statement;也就是说
c a b ? a : b;a b是expression表达式如果这个表达式的判定结果是 true 的话取a如果判断结果为 false 的话则取得 : 后面的值即 b 的值。
4. AB问题IV
import java.util.Scanner;public class Main{public static void main(String[] args){Scanner sc new Scanner(System.in);while(sc.hasNextInt()){int N sc.nextInt();if(N0){break;}int sum 0;for(int i 0; i N; i){sum sc.nextInt();}System.out.println(sum);}sc.close();}
}5. AB问题VIII
import java.util.Scanner;public class Main{public static void main(String[] args){Scanner sc new Scanner(System.in);while(sc.hasNextInt()){int m sc.nextInt();int i 0;while(im){int n sc.nextInt();int sum 0;for(int j0; jn; j){sum sc.nextInt();}System.out.println(sum);if(im){System.out.println();}}}}
}