java语法基础练习题
时间: 2023-09-22 12:13:16 浏览: 176
Java基本语法练习
Java语法基础练习题包括九九乘法表、数组小代码练习、运算符练习和分支结构练习。以下是这些练习题的示例代码:
九九乘法表:
```
package day05;
public class MultiTable {
public static void main(String[] args) {
for(int num=1;num<=9;num++) { //控制行
for(int i=1;i<=num;i++) { //控制列
System.out.print(i + "*" + num + "=" + i*num + "\t");
}
System.out.println(); //换行
}
}
}
```
数组小代码练习:
```
int[] array1 = {1, 2, 3, 4, 5};int[] array2 = new int = 6;
array2 = 7;
array2 = 8;
```
运算符练习:
```
int a = 5, b = 10, c = 5;
boolean b1 = b > a;
System.out.println(b1); //true
System.out.println(c < b); //true
System.out.println(a >= c); //true
System.out.println(b <= a); //false
System.out.println(b == c); //false
System.out.println(a != c); //false
System.out.println(a > 10); //false
System.out.println(b % 2 == 0); //true
System.out.println(c++ > 5); //false------c自增1变为6
System.out.println(c++ > 5); //true-------c自增1变为7
```
分支结构练习:
```
int num = 5;
int flag = num > 0 ? 1 : -1;
System.out.println(flag); //1
int a = 8, b = 5;
int max = a > b ? a : b;
System.out.println("max=" + max);
int number = 10;
if(number % 2 == 0) {
System.out.println(number + "是偶数");
} else {
System.out.println(number + "是奇数");
}
double price = 600;
if(price >= 500) {
double discount = price * 0.8;
System.out.println("满500打8折,优惠后价格为" + discount);
} else {
System.out.println("未满500,无优惠");
}
```
希望以上代码可以帮助到您进行Java语法基础练习。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [java基础练习题(含答案共39道)-语言基础篇](https://blog.csdn.net/u013488276/article/details/124110443)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文