Java基本語法
迴圈
判斷一個數(小於10位)的位數。輸入999,則輸出“它是個3位的數!”
//S1
public class HelloWorld{
public static void main(String[] args){
int num = 999;
int count = 0;
for (int i = 1; num / i != 0; i *= 10) {
count++;
}
System.out.println("它是個" + count + "位的數!");
}
}
//S2
public class HelloWorld{
public static void main(String[] args){
int num = 99;
int count = 0;
while(num>=10) {
count+=1;
num=num/10;
}
count=count+1;
System.out.println("它是個"+count+"位的數!");
}
}
陣列
package com.imooc;
//一維陣列
public class HelloWorld{
public static void main(String[] args) {
int[] stu=new int[5];
stu[0]=33;
System.out.println(stu[0]);
int[] stud= {1,2,3,4,5}; //整體賦值
System.out.println(stud[0]);
int stuu[]; //宣告
stuu=new int[5]; //分配空間
System.out.println(stuu[0]);
int stut[]=new int[]{111,121,222};
System.out.println(stut[0]);
}
}
//二維陣列
public class HelloWorld{
public static void main(String[] args) {
int[][] two=new int[2][4];
System.out.println(two[1][3]);
int[][] test= {{3,2,4},{5,3,6}};
System.out.println(test[0][1]);
int[][] numb=new int[3][];
numb[0]=new int[2];
numb[1]=new int[4];
numb[2]=new int[2];
}
}
相關文章
- java基本語法--運算子Java
- Java基本語法回顧Java
- Java基礎知識篇02——Java基本語法Java
- Java 語法介紹(一):Java語言的基本組成(轉)Java
- Markdown 基本語法
- javascript基本語法JavaScript
- PHP基本語法PHP
- Redux基本語法Redux
- React基本語法React
- ajax 基本語法
- VBS基本語法
- mongoDB基本語法MongoDB
- factory基本語法
- SQL基本語法SQL
- VB基本語法
- shell基本語法
- TCP基本語法TCP
- markdown基本語法
- Day01-java基本語法(程式導向)Java
- 【菜鳥學Java】7:JSP的基本語法JavaJS
- VUE的基本語法Vue
- Thymeleaf的基本語法
- Python 基本語法Python
- JSP基本語法JS
- C++基本語法C++
- HTML基本語法和語義HTML
- php語法同java語法的基本區別(例項專案需求,php才能熟)PHPJava
- orcale 語句基本語法縮寫
- 詳解Dockerfile基本語法Docker
- Scala基本語法學習
- Python的基本語法Python
- MarkDown 11種基本語法
- [PY3]——基本語法
- ORACLE的基本語法(轉)Oracle
- C++ 的基本語法C++
- day02 Java基本語法( 上 ): 變數與運算子Java變數
- Dart語言詳解(二)——基本語法Dart
- Perl語法的基本規則